Friday, 15 February 2013

c++ - String gets cut off during RC4 decryption -



c++ - String gets cut off during RC4 decryption -

edit:

it seems error lies within readresource() function. when printing output before decryption code cutting off.

old text:

i storing rc4 encrypted string within application's resource. because rc4 encryption has maximum string size split big string substrings , split them delimiter.

now, app supposed read resource. split them using delimiter, decrypt each substring , combine them again.

when trying 'testestestestest........test' string works, if utilize illustration 'lorem ipsum dolor sit down amet, consetetur sadipscing elitr' cuts of part of string during decryption.

this rc4-code:

char* rc4_crypt( char *s, size_t datalen, char *d, size_t keylen) { unsigned char s[size]; char *data = s; char *key = d; int i,j,k,tmp; for(i = 0; < size;i++) s[i] = i; for(j = = 0; < size;i++) { j = (j + s[i] + key[i%keylen]) % size; tmp = s[i]; s[i] = s[j]; s[j] = tmp; } for(i = j = k = 0; k < datalen;k++) { = (i + 1) % size; j = (j + s[i]) % size; tmp = s[i]; s[i] = s[j]; s[j] = tmp; tmp = s[(s[i] + s[j]) % size]; data[k] ^= tmp; } homecoming data; }

this code handling splitting:

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { std::stringstream ss(s); std::string item; while(std::getline(ss, item, delim)) { elems.push_back(item); } homecoming elems; } std::vector<std::string> split(const std::string &s, char delim) { std::vector<std::string> elems; homecoming split(s, delim, elems); } void print (std::string &elem) { //this because somehow 'ul' still included //as element, sorts out if(elem.length() !=2) { cout << elem << '\n'; char *output = rc4_crypt((char *)elem.c_str(), strlen(elem.c_str()),key, strlen(key)); cout << output << '\n'; finalstring = finalstring + output; } }

and main function:

int main() { char *output; output = readresource(); std::string stringy; stringy = output; std::vector<std::string> splitted = split(stringy,'+ul+'); for_each (splitted.begin(), splitted.end(), print); cout << endl; cout << finalstring << '\n'; }

does have thought why happening?

edit:

i adding rc4 function utilize in vb.net encrypt code. maybe can help:

private shared function proper_rc4(byval input byte(), byval key byte()) byte() dim i, j, swap uinteger dim s uinteger() = new uinteger(255) {} dim output byte() = new byte(input.length - 1) {} = 0 255 s(i) = next = 0 255 j = (j + key(i mod key.length) + s(i)) , 255 swap = s(i) 'swapping of s(i) , s(j) s(i) = s(j) s(j) = swap next = 0 : j = 0 c = 0 output.length - 1 = (i + 1) , 255 j = (j + s(i)) , 255 swap = s(i) 'swapping of s(i) , s(j) s(i) = s(j) s(j) = swap output(c) = input(c) xor s((s(i) + s(j)) , 255) next homecoming output end function

edit2:

here readresource() function:

char *readresource() { tchar buffer[max_path]; getmodulefilename(null,buffer,sizeof(buffer)); hmodule moduleh = getmodulehandle(buffer); hrsrc res = findresource(moduleh, l"1", l"data"); hglobal globalh = loadresource(moduleh,res); void * ptr = lockresource(globalh); size = sizeofresource(moduleh,res); char *m_presourcebuffer = new char[size]; if (m_presourcebuffer != null) { memcpy(m_presourcebuffer, ptr, size); } homecoming m_presourcebuffer; }

if understand correctly, readresource reading encrypted data. if so, info have 0 in middle of it. 0 treated null terminator when assigned string. decrypt until first zero. first illustration did not end 0 in encrypted result, sec illustration did.

but others have pointed out in comments, there should no need rc4 break pieces. should able encrypt in 1 call. , in reality, bad (insecure) maintain encrypting same key while resetting s-box each time. result vulnerable. described in wikipedia article.

if encrypt single stream, logic becomes much simpler deal with. when read info (e.g., via readresource) need length. function need homecoming length. utilize length in phone call decrypt data.

c++ encryption

No comments:

Post a Comment