encryption - C++ AES How to encrypt data block by block -
aes has maximum block size of 128, , key sizes 128, 196 & 256.
i have implemented aes algorithm so:
int main() { unsigned char key[key_128] = "very strong key"; unsigned char plaintext[16] = "this test"; unsigned char ciphertext[16]; unsigned char decptext[16]; aes_ctx_t *ctx; virtualaes::initialize(); ctx = virtualaes::allocatectx(key, sizeof(key)); virtualaes::encrypt(ctx, plaintext, ciphertext); cout << "encrypted: " << ciphertext << endl; virtualaes::encrypt(ctx, ciphertext, decptext); cout << "decrypted: " << decptext << endl; return 0; }
but want encrypt larger data 128bits, example string that's 512 bits long. need somekind of loop splits strings 128bit blocks , encrypts & joins them again, have hard time doing this. provide example?
i more familiar c#, has several modes of encryption exposed through system.security.cryptography namespace. know how cipher block chaining works. i'll explain you, keep in mind easy mess crypto, informational only, , hope find library need done.
with cipher block chaining (cbc) here do. take data , break block sizes. 128 bits 16 bytes, there go. if have less 16 bytes in last block, must pad. commonest way know of pkcs7 padding, means example if need 3 bytes of padding @ end of last block, add 0x03, 0x03, 0x03 make full block.
so ready encrypt. should have initialization vector (iv) start off with. bitwise xor iv first block of plain text. encrypt result way encrypt single block of data (ecb mode). result first block of cipher text. equivalent iv next block want encrypt. bitwise xor second block , encrypt. take encrypted block, record it, , use xor third block. , on.
this process makes exact same text appearing, let's 5 times in document totally different each time appears. adds more security. despite this, iv not need kept secret @ all. passwords , salts do, ivs not.
Comments
Post a Comment