|
If the result of Base64 decode have NULL character ==> the final result Base64+RC4 will corrupt.
I fixed it by replace: "char *Encrypt(char *pszText, const char *pszKey)" by "char *Encrypt(char *pszText, const char *pszKey, int real_length)". "real_length" is the length(include NULL characters but exclude the last NULL) of base64_decode result. And change "ilen = (int)strlen(pszText)" to "ilen = real_length".
|
|
|
|
|
Just one comment regarding your swap:
<pre>
#define SWAP(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
</pre>
I have used your code, and the swap is fine, but if you are swapping sbox[i] and sbox[j], where i == j, you are zeroing the values out.
Because of this, I think you need to add into your code:
<pre>
if (i != j) SWAP(sbox[i], sbox[j])
</pre>
Or modify the swap to use a temp variable. I'm not sure if the VS C++ compiler will optimize the trivial swap with a temp variable.
I used this to generate millions of bits, and I noticed the sbox elements began zeroing out, and this fixed the problem 
-- modified 4-Sep-12 14:13pm.
|
|
|
|
|
Great! Thanks for your fixing
|
|
|
|
|
I'm using your class to transfer data over HTTP. On the receiving end, the decryption gets messed up on a NULL character. I've debugged it and narrowed it down to when '0' is added to pszText. That's when the encryption/decryption problems begin. Any ideas on how to solve this?
|
|
|
|
|
After taking a closer look at your code, it seems like you account for a NULL '\0' in the encrypted text. The receiving end's RC4 implementation is in PHP and may not account for it the same way...which is why I'm having decryption issues. Can you provide any details on the changes you make or check for to eliminate the '\0' from the text.
|
|
|
|
|
Is it possible to modify the PHP codes to make the decryption?
|
|
|
|
|
Hi Jerry,
Nice code I must say. Never seen anything like this.
Just a small query. I am intersted in generating the key randomly without the use of any password ("Key" in this case) in RC4. Moreover I would like to extract the encryption key for a different type of processing. Can you please guide me and give the code.
Thanking you,
-Snehamoy
|
|
|
|
|
Hi Snehamoy,
RC4 is a Symmetric-key algorithm which usually means the same key for both encryption of plaintext and decryption of ciphertext. If you want to randomly create password for the encryption, you may consider using a kind of asymmetric-key algorithm for encryption with which you can use a specific algorithm to randomly create a pair of private / public key for different plaintext. one to lock or encrypt the plaintext, and one to unlock or decrypt the cyphertext.
|
|
|
|
|
char *Encrypt(char *pszText,const char *pszKey)
{
i=0, j=0,n = 0;
ilen = (int)strlen(pszKey);
for (m = 0; m < 256; m++) /* Initialize the key sequence */
{
*(key + m)= *(pszKey + (m % ilen));
*(sbox + m) = m;
}
for (m=0; m < 256; m++)
{
n = (n + *(sbox+m) + *(key + m)) &0xff;
SWAP(*(sbox + m),*(sbox + n));
}
ilen = (int)strlen(pszText);
for (m = 0; m < ilen; m++)
{
i = (i + 1) &0xff;
j = (j + *(sbox + i)) &0xff;
SWAP(*(sbox+i),*(sbox + j)); /* randomly Initialize
the key sequence */
k = *(sbox + ((*(sbox + i) + *(sbox + j)) &0xff ));
if(k == (unsigned char)*(pszText + m)) /* avoid '\0' among the
encoded text; */
k = 0;
*(pszText + m) ^= k;
}
return pszText;
}
|
|
|
|
|
no need for another base64, why not base46?
|
|
|
|
|
I will write one if I need it.thanks.
modified on Thursday, April 16, 2009 1:48 AM
|
|
|
|
|
I haven't look on RC4 but base64 is highly inefficient and poorly written. For an example base64_map should be static member because it's not changed in the class. The main thing why it shouldn't be used for real decoding is B64_offset.
|
|
|
|
|
Declare Base64_map static can not wrap it into the class or need to be declared inside functions. about Base64_offset. you are right ,declare
an array like this will be more efficient.
static const unsigned char Base64_Offset[256] =
{
/* ASCII table */
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};
I updated the code.
Thanks,Nick.
modified on Thursday, April 16, 2009 12:20 AM
|
|
|
|
|
Or you could use the one that comes with Visual Studio, in file: atlenc.h, I believe...
Cheers,
|
|
|
|
|
Thanks.But there are a lot of codes there.
modified on Thursday, April 16, 2009 12:18 AM
|
|
|
|
|
SO many ready made rountines available why a wrapper
|
|
|
|
|
You missed reading the Backgroud part I think. you are a critic,I hope I am lucky to get good rating from you one day .
Thanks.
modified on Monday, April 13, 2009 2:01 AM
|
|
|
|
|