RC4AlgorithmIt is a very common encryption algorithm with high efficiency.
With reference to the http://en.wikipedia.org/wiki/RC4, you can easily implement the RC4 algorithm by yourself. 1. First, both parties need to specify a key for encryption and decryption. It should be a string with a length in the [1,256] range (in bytes. Moreover, it should be unsigned char rather than char, which is required by the KSA algorithm. 2. KSA
ForIFrom 0To255S [I]:=Iendforj:=0ForIFrom 0To255J:= (J + S [I] + key [I mod keylength]) mod256Swap values of S [I] And s [J] endfor
The KSA algorithm is used to set an initial value to 0 ~ 255 of the sbox is disrupted, and the result after the disruption depends on the previously determined key. Here we can see that the value of key [] should be 0 ~ 255. Otherwise, a negative value is generated. 3. prga
I: =0J:=0WhileGeneratingoutput: I:= (I +1) Mod256J:= (J + S [I]) mod256Swap values of S [I] And s [J] K:= S [(S [I] + s [J]) mod256] Output kendwhile
The process here is the key Stream Generated by byte. Each byte generates K, and a byte of plain text waiting for encryption is bitwise OR. The encrypted string is obtained.
The decryption process of RC4 is exactly the same as that of encryption. It is like 1/0 two different or later. Therefore, RC4 is also called symmetric encryption algorithm. =================================== Self-written RC4 ==== ==============================================
# Include <stdio. h> # Include < String > # Define Max_length 1024 Int Sbox [ 256 ]; // Char key [16] = "cdefgihicds2lca "; // It can be any string with a length of <= 256 // Unsigned char key [4] = "key "; Unsigned Char Key [5 ] = " Wiki " ;
Int Keylength = strlen (( Char * ) Key );
Void KSA (){ Int I; Int J; Int Temp;
For(I =0; I <256; I ++)
{Sbox [I] = I ;}
J=0;
For (I = 0 ; I < 256 ; I ++) {J = (J + sbox [I] + key [I % keylength]) % 25 6 ; Temp = Sbox [I]; sbox [I] = Sbox [J]; sbox [J] = Temp ;}}
Void Prga (){ Int I; Int J; Int M; Int Temp; Char Plain_text [max_length]; Char Cipher_text [max_length]; Char Printable_cipher_text [ 2 * Max_length] = { 0 }; Printf ( " Input the plaintext: \ n " ); Scanf ( " % S " , Plain_text); printf ( " % S \ n " , Plain_text); m = 0 ; While (M <= Strlen (plain_text) {printf ( " % X " ,( Int ) Plain_text [m]); m ++;}
Printf ("\ N");
Char Key [strlen (plain_text) + 1 ]; Char Printable_key [ 2 * Strlen (plain_text) + 1 ]; Memset (printable_key, 0 , Sizeof (Printable_key); m =0 ; I = 0 ; J = 0 ;
// Here, all K is generated first, and then it is different from plain text by byte or. You can also generate K by byte and it is different from plain text or. While (M < Strlen (plain_text) {I = (I + 1 ) % 6 ; J = (J + sbox [I]) % 6 ; Temp = Sbox [I]; sbox [I] = Sbox [J]; sbox [J] = Temp; key [m] = Sbox [(sbox [I] + sbox [J]) % 6 ]; M ++ ;}
Printf ( " M = % d \ n " , M); m = 0 ; While (M <Strlen (key) {snprintf ( & Printable_key [strlen (printable_key)], Sizeof (Printable_key)-strlen (printable_key ), " X " , (Unsigned Char ) Key [m]); m ++ ;}
Printf ( " Keystream is % s \ n " , Printable_key); printf ( " M = % d \ n " , M); I = 0 ; While (I < Strlen (plain_text) {cipher_text [I] = Plain_text [I] ^ Key [I]; printf ( " X = x ^ x \ n " , (Unsigned Char ) Cipher_text [I], (unsigned Char ) Plain_text [I], (unsigned Char ) Key [I]); I ++ ;}
M = 0 ; While (M < Strlen (cipher_text) {snprintf ( & Printable_cipher_text [strlen (printable_cipher_text)], Sizeof (Printable_cipher_text)-strlen (printable_cipher_text ), " X " , (Unsigned Char ) Cipher_text [m]); m ++;}
Printf ( " Ciphterstream is % s \ n " , Printable_cipher_text );}
Int Main () {KSA (); prga (); Return 1 ;}