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
| #include <openssl/aes.h> #include <string.h> #include <iostream> using namespace std; int main() { const char *pt = "data1, data2, data3, data4, data5, data6, data7, data8, data9, data10"; const char *key = "123456787654321"; AES_KEY encKey; AES_set_encrypt_key((const unsigned char*)key, 128, &encKey); int length = 0; int len = strlen((char *)pt) + 1; if (len % 16 != 0) { length = ((len / 16) + 1) * 16; } else { length = len; } unsigned char *out = new unsigned char[length]; unsigned char ivec[16]; memset(ivec, 9, sizeof(ivec)); AES_cbc_encrypt((const unsigned char*)pt, out, length, &encKey, ivec, AES_ENCRYPT); unsigned char* data = new unsigned char[length]; AES_KEY deckey; AES_set_decrypt_key((const unsigned char*)key, 128, &deckey); memset(ivec, 9, sizeof(ivec)); AES_cbc_encrypt((const unsigned char*)out, data, length, &deckey, ivec, AES_DECRYPT); cout<<data<<endl; delete[] out; delete[] data; }
|