1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| static uint8_t commonIV[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
std::ifstream is (filename, std::ifstream::binary); std::ofstream os (filename + ".enc", std::ifstream::binary); if (is) { is.seekg (0, is.end); int length = is.tellg(); is.seekg (0, is.beg); uint8_t in[16] = {0}; uint8_t out[16] = {0}; memcpy(out,commonIV,sizeof(commonIV));
while(length > 0) { is.read ((char*)in,(length>=16?16:length)); if (!is) break; if(length < 16) { int num = 16 - length; for (int i= length;i<16;i++) { in[i] = num; } }
for (int i=0;i<4;i++) { in[4*0+i] ^= out[4*0+i]; in[4*1+i] ^= out[4*1+i]; in[4*2+i] ^= out[4*2+i]; in[4*3+i] ^= out[4*3+i]; }
cipher(in , out , w ); os.write((char*)out, sizeof(out)); if (length == 16) { for(int i=0;i<16;i++) in[i] = 16; cipher(in,out,w); os.write((char*)out, sizeof(out)); }
length = length - is.gcount(); }
is.close(); os.close(); }
|