Hey, I'm trying to decrypt some data via RC4 (Data is sent by a Webserver). But if I try to decrypt it sometimes the string is cut off at some random location! I thought it maybe is a unicode null terminator when the RC4 output is generated so i put a base64 function over the Webserver result.
I use following decrypt functions:
void rc4( const wchar_t *data, const wchar_t *key, wchar_t **output )
{
wchar_t *temp;
unsigned int i,j=0,t,tmp,tmp2,s[256], k[256];
for (tmp=0;tmp<256;tmp++)
{
s[tmp]=tmp;
k[tmp]=key[(tmp % wcslen(key))];
}
for (i=0;i<256;i++)
{
j = (j + s[i] + k[i]) % 256;
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
}
temp = new wchar_t[wcslen(data) + 1];
i=j=0;
for (tmp=0;tmp<wcslen(data);tmp++)
{
i = (i + 1) % 256;
j = (j + s[i]) % 256;
tmp2=s[i];
s[i]=s[j];
s[j]=tmp2;
t = (s[i] + s[j]) % 256;
if (s[t]==data[tmp])
temp[tmp]=data[tmp];
else
temp[tmp]=s[t]^data[tmp];
}
temp[tmp] = '\0';
*output = temp;
}
static const std::wstring base64_chars =
L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
L"abcdefghijklmnopqrstuvwxyz"
L"0123456789+/";
static inline bool is_base64(wchar_t c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
void base64_decode(const wchar_t *encoded_string, wchar_t **output)
{
unsigned int in_len = wcslen( encoded_string );
unsigned int i = 0;
unsigned int j = 0;
unsigned int in_ = 0;
unsigned int r = 0;
wchar_t char_array_4[4], char_array_3[3];
wchar_t *ret;
ret = new wchar_t[in_len + 1];
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++, r++)
ret[r] = char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++, r++)
ret[r] = char_array_3[j];
}
ret[r++] = '\0';
*output = ret;
//return ret;
}
As you can see I'm using unsigned chars here. So my question is: Can a unsigned char hold any byte produced by a RC4 function?
Is there a safe and easy crypting method to crypt stuff server side (PHP implementation) and decrypt it clientside (C++ implementation)??
If i need to use widestrings here, how would i need to change the above functions?
---------- Post added at 09:25 AM ---------- Previous post was at 08:05 AM ----------
I have changed everything to wchar_t now. Still no result.


Sign In
Create Account

Back to top









