1 char encrypt_string[4096] = { 0 }; 2 AES_KEY aes; 3 char key[17] = "quck7295abvdefgh"; 4 char iv[17] = "abcdefgh3762quck"; 5 std::string input_string = "45b00417-f7ca-4f53-bced-c1743d85604"; 6 int nLen = input_string.length(); 7 8 int nBei = nLen / AES_BLOCK_SIZE + 1; 9 int nTotal = nBei * AES_BLOCK_SIZE; 10 char *enc_s = (char*)malloc(nTotal); 11 int nNumber; 12 if (nLen % 16 > 0) 13 nNumber = nTotal - nLen; 14 else 15 nNumber = 16; 16 memset(enc_s, nNumber, nTotal); 17 memcpy(enc_s, input_string.data(), nLen); 18 19 if (AES_set_encrypt_key((unsigned char*)key, 128, &aes) < 0) { 20 fprintf(stderr, "Unable to set encryption key in AES "); 21 exit(-1); 22 } 23 24 AES_cbc_encrypt((unsigned char *)enc_s, (unsigned char*)encrypt_string, nBei * 16, &aes, (unsigned char*)iv, AES_ENCRYPT); 25 26 27 std::string enstr = boost::lexical_cast<std::string>(encrypt_string); 28 std::string base64 = base64_encode(enstr); 29 30 std::cout << base64 << std::endl;
KCS5Padding:填充的原则是,如果长度少于16个字节,需要补满16个字节,补(16-len)个(16-len)例如:
huguPozhen这个节符串是9个字节,16-9= 7,补满后如:huguozhen+7个十进制的7
如果字符串长度正好是16字节,则需要再补16个字节的十进制的16。