RC4 algorithm N = 3 (C language)

Source: Internet
Author: User

Grand Prix
# Include <stdio. h>
# Include <string. h>

Int m1 [200]; // plaintext binary
Int s [8] = {0, 1, 2, 3, 4, 5, 6, 7 };
Int k3 [200]; // binary key
Char c [200]; // ciphertext
Int count;

Int main (void)
{
Int I, j, n;
Char m0 [100];

Int T [8];
Printf ("Input key length: n ");
Scanf ("% d", & n );
Printf ("initial key: n ");
For (I = 0; I <n; I ++)
Scanf ("% d", & T );
Getchar ();
If (n <8)
{
For (I = 0; I <8; I ++)
T = T [I % n];
Printf ("Initial T box: n ");
For (I = 0; I <8; I ++)
Printf ("% d", T );
Printf ("n ");

}
Else
{
Printf ("input error, exit program n ");
Exit (1 );
}

 

Void mingwenchuli (char m0 [100]); // plaintext Processing
Mingwenchuli (m0 );
Void miyao (int T [8]); // Key Generation
Miyao (T );
Void jiami (int m1 [200], int k3 [200]); // Encryption
Jiami (m1, k3 );
}

Void mingwenchuli (char m0 [100]) // plaintext Processing
{
Int I, j, r, n, temp;
Int m [200];
Printf ("Enter the plaintext character: n ");
I = 0;
While (m0 = getchar ())! = N)
I ++;
M0 =;
J = 0;
Count = 0;
Int z = strlen (m0 );
Int y [200];
For (I = 0; I <z; I ++)
{
R = 0;
Int k = 0;
Y = int (m0 );
Do {
M [r] = y % 2; // convert to binary
Y = y/2;
M1 [count] = m [r];
R ++;
Count ++;
} While (r! = 7 );
For (j = count-1; j> = count-3; j --)
{
Temp = m1 [j];
M1 [j] = m1 [j-6 + k];
M1 [j-6 + k] = temp;
K = k + 2;
}
}

M1 [count] =;

Printf ("output plaintext binary format: n ");
For (r = 0; r <count; r ++)
Printf ("% d", m1 [r]);
Printf ("n ");
}
Void miyao (int T [8])
{
Int j, p;
Int I, temp, t, l;
Int k [100];
Int k2 [200];
J = 0;
For (I = 0; I <8; I ++) // key scheduling
{
J = (j + s + T) % 8;
Temp = s;
S = s [j];
S [j] = temp;
}
Printf ("output s box array: n ");
For (I = 0; I <8; I ++)
Printf ("% d", s); // outputs the S Array
Printf ("n ");

I = 0;
J = 0;
For (t = 0; t <(count/3 + 1); t ++) // pseudo-random Scheduling Algorithm
{
I = (I + 1) % 8;
J = (j + s) % 8;
Temp = s;
S = s [j];
S [j] = temp;
L = (s + s [j]) % 8;
K [t] = s [l];
}
Printf ("output key in decimal format: n ");
For (t = 0; t <(count/3 + 1); t ++)
Printf ("% d", k [t]); // output key ----- decimal
Printf ("n ");

P = 0;
For (I = 0; I <(count/3 + 1); I ++)
{
J = 0;
Do {
K2 [j] = k % 2; // Binary Conversion
K = k/2;
K3 [p] = k2 [j];
P ++;
J ++;
} While (j! = 3 );

Temp = k3 [P-1];
K3 [P-1] = k3 [P-3];
K3 [P-3] = temp;
}
K3 [p] =;

Printf ("output key binary form: n ");
For (j = 0; j <p; j ++)
Printf ("% d", k3 [j]);
Printf ("n ");
}

Void jiami (int m1 [200], int k3 [200])
{
Int I, j;
For (I = 0; I <count; I ++) // unique or encrypted
C = m1 ^ k3;
Printf ("output encrypted ciphertext: n ");
For (I = 0; I <count; I ++)
Printf ("% d", c );
Printf ("n ");
}

Related Article

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.