Implementation of RC4 Encryption Algorithm in C ++

Source: Internet
Author: User

In some cases, some simple encryption algorithms are often used. Here, RC4 is the simplest one. You only need to set a strong enough password for some very simple scenarios. I used to encrypt the data transmitted over HTTP.

RC4 functions (encryption/Decryption), in fact, RC4 only encrypts the ciphertext once again, that is, decryption.

The getkey function is a random string generator. For convenience, most encryption algorithms have a random password generator.

The bytetohex function converts the bytecode into a hexadecimal code. one byte is in two hexadecimal notation. The study found that the hexadecimal string is very suitable for transmission in HTTP, and some characters in base64 may cause escape, which is quite troublesome.

The hextobyte function converts a hexadecimal string into a bytecode. The server also transmits the data back in the form of a hexadecimal string, which can be decoded here. At the same time, the hexadecimal string is used for transmission, avoiding the problem of multi-country language during transmission.

After the encrypt function encrypts the string with RC4, it then converts the ciphertext to a hexadecimal string and returns it, which can be directly used for transmission.

The decrypt function directly encrypts the hexadecimal string ciphertext and decrypts it. The plaintext of the string is returned.

 

The source code is as follows:

Encrypt. h file:

# Ifndef _ encrypt_rc4 _
# DEFINE _ encrypt_rc4 _

# Include <string. h>

# Define box_len 256

Int getkey (const unsigned char * pass, int pass_len, unsigned char * out );
Int RC4 (const unsigned char * data, int data_len, const unsigned char * Key, int key_len, unsigned char * Out, int * out_len );
Static void swap_byte (unsigned char * a, unsigned char * B );

Char * encrypt (const char * szsource, const char * szpassword); // encrypted, return the encrypted result
Char * decrypt (const char * szsource, const char * szpassword); // decrypt and return the decryption result

Char * bytetohex (const unsigned char * vbyte, const int vlen); // converts the bytecode pbbuffer into a hexadecimal string for convenient Transmission
Unsigned char * hextobyte (const char * szhex); // convert the hexadecimal string into a bytecode pbbuffer and decode it.

# Endif // # ifndef _ encrypt_rc4 _

Encrypt. cpp file:

# Include "encrypt. H"

Char * encrypt (const char * szsource, const char * szpassword) // encrypted, return the encrypted result
{
If (szsource = NULL | szpassword = NULL) return NULL;

Unsigned char * ret = new unsigned char [strlen (szsource)];

Int ret_len = 0;

If (RC4 (unsigned char *) szsource,
Strlen (szsource ),
(Unsigned char *) szpassword,
Strlen (szpassword ),
RET,
& Ret_len) = NULL)
Return NULL;

Char * ret2 = bytetohex (Ret, ret_len );

Delete [] ret;
Return ret2;
}

Char * decrypt (const char * szsource, const char * szpassword) // decrypt and return the decryption result
{
If (szsource = NULL | (strlen (szsource) % 2! = 0) | szpassword = NULL)
Return NULL;

Unsigned char * src = hextobyte (szsource );

Unsigned char * ret = new unsigned char [strlen (szsource)/2 + 1];

Int ret_len = 0;

Memset (Ret, strlen (szsource)/2 + 1, 0 );

If (RC4 (SRC, strlen (szsource)/2, (unsigned char *) szpassword, strlen (szpassword), RET, & ret_len) = NULL)
Return NULL;

RET [ret_len] = '/0 ';

Return (char *) ret;
}

Int RC4 (const unsigned char * data, int data_len, const unsigned char * Key, int key_len, unsigned char * Out, int * out_len)
{
If (Data = NULL | key = NULL | out = NULL)
Return NULL;

Unsigned char * mbox = new unsigned char [box_len];

If (getkey (Key, key_len, mbox) = NULL)
Return NULL;

Int I = 0;
Int x = 0;
Int y = 0;
 
For (int K = 0; k <data_len; k ++)
{
X = (x + 1) % box_len;
Y = (mbox [x] + Y) % box_len;
Swap_byte (& mbox [X], & mbox [y]);
Out [k] = data [k] ^ mbox [(mbox [x] + mbox [y]) % box_len];
}

* Out_len = data_len;
Delete [] mbox;
Return-1;
}

Int getkey (const unsigned char * pass, int pass_len, unsigned char * out)
{
If (pass = NULL | out = NULL)
Return NULL;

Int I;

For (I = 0; I <box_len; I ++)
Out [I] = I;

Int J = 0;
For (I = 0; I <box_len; I ++)
{
J = (pass [I % pass_len] + out [I] + J) % box_len;
Swap_byte (& out [I], & out [J]);
}

Return-1;
}

Static void swap_byte (unsigned char * a, unsigned char * B)
{
Unsigned char swapbyte;
 
Swapbyte = *;

* A = * B;

* B = swapbyte;
}

// Convert the bytecode into a hexadecimal code. one byte is in two hexadecimal notation and space is allocated for the character string.
Char * bytetohex (const unsigned char * vbyte, const int vlen)
{
If (! Vbyte)
Return NULL;

Char * TMP = new char [vlen * 2 + 1]; // one byte, two hexadecimal codes, and one more '/0'

Int tmp2;
For (INT I = 0; I <vlen; I ++)
{
Tmp2 = (INT) (vbyte [I])/16;
TMP [I * 2] = (char) (tmp2 + (tmp2> 9 )? 'A'-10: '0 '));
Tmp2 = (INT) (vbyte [I]) % 16;
TMP [I * 2 + 1] = (char) (tmp2 + (tmp2> 9 )? 'A'-10: '0 '));
}

TMP [vlen * 2] = '/0 ';
Return TMP;
}

// Convert the hexadecimal string into a bytecode. Every two hexadecimal characters are used as one byte.
Unsigned char * hextobyte (const char * szhex)
{
If (! Szhex)
Return NULL;

Int ilen = strlen (szhex );

If (ilen <= 0 | 0! = Ilen % 2)
Return NULL;

Unsigned char * pbbuf = new unsigned char [ilen/2]; // data buffer

Int tmp1, tmp2;
For (INT I = 0; I <ilen/2; I ++)
{
Tmp1 = (INT) szhex [I * 2]-(INT) szhex [I * 2]> = 'A ')? 'A'-10: '0 ');

If (tmp1> = 16)
Return NULL;

Tmp2 = (INT) szhex [I * 2 + 1]-(INT) szhex [I * 2 + 1]> = 'A ')? 'A'-10: '0 ');

If (tmp2> = 16)
Return NULL;

Pbbuf [I] = (tmp1*16 + tmp2 );
}

Return pbbuf;
}

Main. cpp File

# Include <iostream>
# Include <string>
# Include <stdio. h>

# Include "encrypt. H"

Int main (INT argc, char * argv [])
{
Int size = 0;

Char source [] = "Chenli ";
Char pass [] = "123456 ";
Char * result1 = NULL;
Char * result2 = NULL;
 
Result1 = encrypt (source, pass );

Printf ("result1 = % s/n", result1 );

Result2 = decrypt (result1, pass );

Printf ("result2 = % s/n", result2 );

Delete [] result1;
Delete [] result2;

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.