openssl AES-CBC 加密解密问题 IV 如何保存下来。
用户密码加密,解密我用到了 OPENSSL AES-CBC 加密模式。不管是加密还是解密,我的密钥都是固定的。但是我加密以后的iv是不是需要保存下来?因为我把加密后的数据进行解密,不能正确的解密,解密出来是乱码。
加密:
解密:
加密:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/aes.h>
int main(int argc, char** argv) {
AES_KEY aes;
unsigned char* key = "www.qqqqqqq.com "; // AES_BLOCK_SIZE = 16
unsigned char iv[AES_BLOCK_SIZE]; // init vector
unsigned char* input_string;
unsigned char* encrypt_string;
unsigned int len; // encrypt length (in multiple of AES_BLOCK_SIZE)
unsigned int i;
// check usage
if (argc != 2) {
fprintf(stderr, "%s <plain text>\n", argv[0]);
exit(-1);
}
// set the encryption length
len = 0;
if ((strlen(argv[1]) + 1) % AES_BLOCK_SIZE == 0) {
len = strlen(argv[1]) + 1;
} else {
len = ((strlen(argv[1]) + 1) / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
}
// set the input string
input_string = (unsigned char*)calloc(len, sizeof(unsigned char));
if (input_string == NULL) {
fprintf(stderr, "Unable to allocate memory for input_string\n");
exit(-1);
}
strncpy((char*)input_string, argv[1], strlen(argv[1]));
// Generate AES 128-bit key
/*for (i=0; i<16; ++i) {
key[i] = 32 + i;
}*/
// Set encryption key
for (i=0; i<AES_BLOCK_SIZE; ++i) {
iv[i] = 0;
}
if (AES_set_encrypt_key(key, 128, &aes) < 0) {
fprintf(stderr, "Unable to set encryption key in AES\n");
exit(-1);
}
// alloc encrypt_string
encrypt_string = (unsigned char*)calloc(len, sizeof(unsigned char));
if (encrypt_string == NULL) {
fprintf(stderr, "Unable to allocate memory for encrypt_string\n");
exit(-1);
}
// encrypt (iv will change)
AES_cbc_encrypt(input_string, encrypt_string, len, &aes, iv, AES_ENCRYPT);
// print
for (i=0; i<len; ++i) {
printf("%X%X", (encrypt_string[i] >> 4) & 0xf,
encrypt_string[i] & 0xf);
}
printf("\n");
return 0;
}
解密:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/aes.h>
int main(int argc, char** argv) {
AES_KEY aes;
unsigned char* key = "www.qqqqqqq.com "; // AES_BLOCK_SIZE = 16
unsigned char iv[AES_BLOCK_SIZE]; // init vector
unsigned char* input_string;
unsigned char* decrypt_string;
unsigned int len; // encrypt length (in multiple of AES_BLOCK_SIZE)
unsigned int i;
// check usage
if (argc != 2) {
fprintf(stderr, "%s <plain text>\n", argv[0]);
exit(-1);
}
// set the encryption length
len = 0;
if ((strlen(argv[1]) + 1) % AES_BLOCK_SIZE == 0) {
len = strlen(argv[1]) + 1;
} else {
len = ((strlen(argv[1]) + 1) / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;
}
// set the input string
input_string = (unsigned char*)calloc(len, sizeof(unsigned char));
if (input_string == NULL) {
fprintf(stderr, "Unable to allocate memory for input_string\n");
exit(-1);
}
strncpy((char*)input_string, argv[1], strlen(argv[1]));
// alloc decrypt_string
decrypt_string = (unsigned char*)calloc(len, sizeof(unsigned char));
if (decrypt_string == NULL) {
fprintf(stderr, "Unable to allocate memory for decrypt_string\n");
exit(-1);
}
// Set decryption key
for (i=0; i<AES_BLOCK_SIZE; ++i) {
iv[i] = 0;
}
if (AES_set_decrypt_key(key, 128, &aes) < 0) {
fprintf(stderr, "Unable to set decryption key in AES\n");
exit(-1);
}
// decrypt
AES_cbc_encrypt(input_string, decrypt_string, len, &aes, iv,
AES_DECRYPT);
printf("%s", decrypt_string);
printf("\n");
return 0;
}
...全文
相关推荐
php aes加密类 128,192,256位数加密多位数
手写的aes加密类,可实现,128,192,256位数加密
PHP加密扩展库Mcrypt安装和实例
PHP加密扩展库有Mcrypt和Mhash,其中,Mcrypt扩展库可以实现加密解密功能,今天我我们讲的就是Mcrypt的功能和实例
使用openssl中aes加密算法实现文件的加密解密(源代码)
实现EVP_aes_128(256)_ecb模式,适合初学者熟悉openssl中的加密解密函数。可运行..
基于PHP和JS的AES相互加密解密方法详解(CryptoJS)_PHP_JS_AES源码
基于PHP和JS的AES相互加密解密方法详解(CryptoJS)_PHP_JS_AES源码
C++ 利用openssl实现DES的CBC模式加解密
利用C++ openssl库实现DES加密算法的CBC模式加解密。源码直接可以利用。 供初学者参考。


