c# - ICryptoTransform.TransformBlock : What's going on? -


i'm learning icryptotransform.

and encrypted data , decrypted them using icryptotransform.transformblock. no exception throwed, found data move right 1 bolck.

please looks below codes:

//init var aes = new aescryptoserviceprovider(); var key = new byte[16]; var iv = new byte[16]; var rand = new random(); rand.nextbytes(key); rand.nextbytes(iv); var dev = aes.createencryptor(key, iv); var invdev = aes.createdecryptor(key, iv); const int bufsize = 16 * 2; var input = new byte[bufsize]; var output = new byte[bufsize]; var backbuf = new byte[bufsize]; rand.nextbytes(input);  // start caculate (int = 0; < bufsize; += 16)     dev.transformblock(input, i, 16, output, i); backbuf[0] = 10; // seems invdev didn't touch backbuf[0 ~ 15] (int = 0; < bufsize; += 16)     invdev.transformblock(output, i, 16, backbuf, i);  // output (int = 0; < bufsize; ++i) { console.write(input[i]); console.write(' '); } console.writeline(); (int = 0; < bufsize; ++i) { console.write(output[i]); console.write(' '); } console.writeline(); (int = 0; < bufsize; ++i) { console.write(backbuf[i]); console.write(' '); } console.writeline(); console.readkey(); 

i think input should equal backput

but program outputs :

83 202 85 77 101 146 91 55 90 194 242 26 118 40 46 218 196 202 75 234 228 232 146 156 169 250 72 130 78 185 52 14  219 44 184 142 192 20 222 199 39 232 160 115 254 18 250 70 43 81 149 152 140 4 249 193 248 57 18 59 149 30 41 23  10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 83 202 85 77 101 146 91 55 90 194 242 26 118 40 46 218 

it's funny , confusing... what's wrong icryptotransform.transformblock , program? or can use icryptotransform.transformblock directly ?

thanks. ( @ last please excuse poor english... )

well, may find solution in fact, there integer return value in icryptotransform.transformblock, means number of bytes transformed succedded codesinchaos said. , correct usage of icryptotransform.transformblock is:

int transed = 0; (int = 0; < bufsize; += transed )     transed = invdev.transformblock(output, i, 16, backbuf, i); 

and amusing, first transformblock method returns 0... , it's why program wrong.

it seems aes may need prepare before transform first transformblock returns 0.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -