Introduction
This Crypt routine uses an algorithm created by Rodney Thayer.
This algorithm can use keys of various sizes. With a 40-bit key (5 characters)
it can be freely exported from the U.S. The cipher is considered robust with 128
bits of key material but can use up to 2048 bits. It is compatible with RSA�s
RC4 algorithm.
void Crypt(TCHAR *inp, DWORD inplen, TCHAR* key = "", DWORD keylen = 0)
{
TCHAR Sbox[257], Sbox2[257];
unsigned long i, j, t, x;
static const TCHAR OurUnSecuredKey[] = "www.systweak.com" ;
static const int OurKeyLen = _tcslen(OurUnSecuredKey);
TCHAR temp , k;
i = j = k = t = x = 0;
temp = 0;
ZeroMemory(Sbox, sizeof(Sbox));
ZeroMemory(Sbox2, sizeof(Sbox2));
for(i = 0; i < 256U; i++)
{
Sbox[i] = (TCHAR)i;
}
j = 0;
if(keylen)
{
for(i = 0; i < 256U ; i++)
{
if(j == keylen)
{
j = 0;
}
Sbox2[i] = key[j++];
}
}
else
{
for(i = 0; i < 256U ; i++)
{
if(j == OurKeyLen)
{
j = 0;
}
Sbox2[i] = OurUnSecuredKey[j++];
}
}
j = 0 ;
for(i = 0; i < 256; i++)
{
j = (j + (unsigned long) Sbox[i] + (unsigned long) Sbox2[i]) % 256U ;
temp = Sbox[i];
Sbox[i] = Sbox[j];
Sbox[j] = temp;
}
i = j = 0;
for(x = 0; x < inplen; x++)
{
i = (i + 1U) % 256U;
j = (j + (unsigned long) Sbox[i]) % 256U;
temp = Sbox[i];
Sbox[i] = Sbox[j] ;
Sbox[j] = temp;
t = ((unsigned long) Sbox[i] + (unsigned long) Sbox[j]) % 256U ;
k = Sbox[t];
inp[x] = (inp[x] ^ k);
}
}
The only moral here is that we are generating a 256 bit key given from the
user. To encrypt we always pick a random byte from the key and encrypt the text
with that key. If we have a large pattern of repeating text with this algorithm
the key repeat interval (distance between n repetitions) will be approximately
of 21700 (2 to the 1700th power).
For very good security I recommend using 128 bits (16
characters) of key material. If you want optimal security you could use 2048
bits (256 characters) of key material (but this is usually considered an
overkill).
This routine is constructed from a draft taken from Internet
Engineering Task Force (IETF) at http://www.ietf.cnri.reston.va.us/home.htm
I have tested the routine on a 27 MB file to test that it
produce valid decrypted output from input and get the valid results.
Using this routine it is fairly simple.
TCHAR HelloCrypt[] = "Hello Crypt";
Crypt(HelloCrypt, _tcslen(HelloCrypt));
Crypt(HelloCrypt, _tcslen(HelloCrypt));
TCHAR Data[] = "abcdefghijklmnopqrstuvwxyz"
TCHAR Key[] = "Strong key";
Crypt(Data, _tcslen(Data), Key, _tcslen(Data));
Crypt(Data, _tcslen(Data), Key, _tcslen(Data));
Please visit My Web Site for more
tutorials, tweaks, reference.