decryption of block cipher?
Hi i have encrypted a data of size 196,662 bytes, using block cipher method AES-128 of mode electronic code book (ecb), and now the encrypted data size became 196,672 bytes. And now I have to decrypt, since the data size i have to decrypt is a multiple of 16, I tried decrypting the encrypted data at a single stretch without dividing it, but its not decrypting. Tell me if i want to change anything in my encryption and decryption method or else tell me any other equivale开发者_如何学JAVAnt solution. Thanks in advance.
Edit: Encryption code:
bufferlen = filesize;
buffer = new BYTE [ bufferlen ];
feof = false;
do {
count = fread(buffer, 1, 16, filepointer);
if ( count < 16 ) {
feof = true;
}
cout << count;
result = CryptEncrypt(hGenKey,NULL,feof,0,buffer,&count,bufferlen);
if ( result == 0 ) {
cout << "\nencrypt failed";
}
else {
cout << "encrypt passed";
}
if(fwrite(buffer, 1, count , filepointer1)!=count) {
cout << "\nwrite failed";
}
} while (feof != true);
Decryption code:
bufferlen = filesize;
buffer = new BYTE [ bufferlen ];
feof = false;
do {
count = fread(buffer, 1, 16, filepointer);
if ( count < 16 ) {
feof = true;
}
cout << count;
result = CryptEncrypt(hGenKey,NULL,feof,0,buffer,&count,bufferlen);
if ( result == 0 ) {
cout << "\nencrypt failed";
}
else {
cout << "encrypt passed";
}
if(fwrite(buffer, 1, count , filepointer1)!=count) {
cout << "\nwrite failed";
}
} while ( feof != true );
In the second to last iteration of your loop, while you are doing decryption, count
will be 16, however you have to set the Final
flag because it contains the padding (your feof
is false). Then in the next iteration count
will be 0, in which case you set feof
but you actually have no data to decrypt any more.
I would check the wikipedia article on ecb http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
Without at least having the pseudo code for your project there isn't much else we'll be able to say.
This is really an answer to a comment, not the original question. In pseudo code, CBC mode looks something like:
prev_block = IV;
write_output(prev_block);
current_block = first_block;
while (more blocks to process) {
encrypted_block = encrypt_block(current_block XOR prev_block);
write_output(encrypted_block);
prev_block = encrypted_block;
current_block = next_block;
}
精彩评论