RC4 encryption principle and code

Source: Internet
Author: User
Tags crypt

Recently, RC4 has been used for reference and implementation, as listed below:
RC4 algorithm Introduction
RC4 encryption algorithm Ron
Rivest (one of the most famous RSA Algorithms) was designed in 1987 to provide a stream encryption algorithm cluster with variable key lengths. It is called a cluster because the S-box length of its core part can be arbitrary, but generally 256 bytes. The algorithm is about 10 times faster than DES encryption, and has a high level of non-linearity. RC4 was initially used to protect trade secrets. But in September 1994, its algorithms were published on the Internet, so there was no commercial secrets. RC4 is also called ARC4 (Alleged
RC4-the so-called RC4), because RSA has never officially released this algorithm.
Note: RC4 is a symmetric key encryption algorithm, while RSA is a non-symmetric encryption algorithm. Because the RC4 algorithm uses xor for encryption, once the sub-Key sequence is repeated, the ciphertext may be cracked. So will the subkey sequence generated by the RC4 algorithm be repeated? Due to some weak keys, the sub-Key sequence is completely repeated within less than 1 million bytes. If it is partially repeated, it may be repeated within less than 0.1 million bytes. Therefore, when using the RC4 algorithm, we recommend that you test the encryption key to determine whether it is a weak key. According to the current analysis results, no analysis is effective for RC4 with a key length of 128 bits. Therefore, RC4 is currently one of the most secure encryption algorithms.

 

From: http://wzgyantai.blogbus.com/logs/31867065.html

 

To put it bluntly, you can call the same method twice to restore the first encrypted data to the original data. The length of the original data and the private key are important here.

 

# Include <iostream>
# Include <cstring>
Using namespace std;

Void crypt (char cipher []); //// // decryption process function, generate another secret stream based on the input key
Void main ()
{
Cout <"*************************** RC4 encryption and decryption program ****** * ******************* "<Endl;
Cout <****** * ********************** "<Endl;
Char choose1, choose2;
Do {
Int s [256], t [256];
Char K [256]; // The Key entered by the user
Char plaintext [1024], ciphertext [1024];
Cout <"Input key :";
Cin> K;
For (INT I = 0; I <256; I ++) // assign values to the byte state vector and variable length key Array
{
S [I] = I;
T [I] = K [I % strlen (k)];
}
Int J = 0;
For (INT I = 0; I <256; I ++) ///// use a variable-length key array to initialize the byte State Vector Array s
{
Int temp;
J = (j + s [I] + t [I]) % 256;
Temp = s [I];
S [I] = s [j];
S [j] = temp;
}
Cout <"/n enter the string to be encrypted:" <endl;
Cin> plaintext;
Int m, n, key [256], q;
M = n = 0;
Int I;
Cout <"/n returns the ciphertext:" <endl;
For (I = 0; I <strlen (plaintext); I ++) /////////// the encrypted stream is generated by the byte state vector array transformation and the plaintext characters are encrypted.
{
Int temp;
M = (m + 1) % 256;
N = (n + s [n]) % 256;
Temp = s [m];
S [m] = s [n];
S [n] = temp;
Q = (s [m] + s [n]) % 256;
Key [I] = s [q];
Ciphertext [I] = plaintext [I] ^ key [I];
Cout <ciphertext [I];

}
Ciphertext [I] = '/0 ';
Cout <endl;
/* Cout <"/n dense stream :";
For (INT I = 0; I <strlen (plaintext); I ++)
Cout <key [I] <"";
Cout <Endl ;*/

//////////////////////////////////////// //////////////////////////////////////
//////////////////////////////////////// /////////////////////////////////////
// Decrypt
Cout <"/n indicates whether to decrypt the ciphertext above? (Y/n )";
Cin> choose2;
While (choose2 = 'y' | choose2 = 'y ')
{
/* Cout <"/N: the plaintext obtained after decryption is:" <Endl;
For (INT p = 0; P <strlen (plaintext); P ++) /////////// decrypt the encrypted ciphertext through the encrypted ciphertext stream recorded during encryption
{
Plaintext [p] = ciphertext [p] ^ key [p];
Cout <plaintext [p];
}
Cout <Endl ;*/
Crypt (ciphertext); // decryption process function
Choose2 = 'n ';
}
Cout <Endl;
Cout <"/n do you want to continue using the program? (Y/n )";
Cin> choose1;
} While (choose1 = 'y' | choose1 = 'y ');

Cout <"/n ****************************** end of the program **** * ************************ "<endl;
System ("pause ");
}

//////////////////////////////////////// //////////////////////////////////////// /////////////
//////////////////////////////////////// ///////////////////////////
// Decryption function. The generation and encryption of the encrypted stream are the same.

Void decrypt (char cipher [])
{
Int s [256], t [256];
Char k [256]; // The Key entered by the user
Char plaintext [1024];
Cout <"/n input key :";
Cin> k;
For (int I = 0; I <256; I ++) // assign values to the byte state vector and variable length key Array
{
S [I] = I;
T [I] = K [I % strlen (k)];
}
Int J = 0;
For (INT I = 0; I <256; I ++) ///// use a variable-length key array to initialize the byte State Vector Array s
{
Int temp;
J = (J + S [I] + T [I]) % 256;
Temp = s [I];
S [I] = s [J];
S [J] = temp;
}

Int m, n, key [256], Q;
M = n = 0;
Int I;
After cout <"/N is decrypted, the obtained plaintext is:" <Endl;
For (I = 0; I <strlen (Cipher); I ++) /////////// the encrypted stream is generated by byte status Vector Array conversion and ciphertext characters are decrypted.
{
Int temp;
M = (m + 1) % 256;
N = (n + s [N]) % 256;
Temp = s [m];
S [m] = s [N];
S [N] = temp;
Q = (s [m] + s [N]) % 256;
Key [I] = s [Q];
Plaintext [I] = cipher [I] ^ key [I];
Cout <plaintext [I];

}

Cout <endl;
/* Cout <"/n dense stream :";
For (int I = 0; I <strlen (cipher); I ++)
Cout <key [I] <"";*/
}

 

 

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.