基于aes的数据加解密需要提供两个额外的数据:密钥、初始向量
CBC的加密从左往右看,初始化IV只有在第一个块加密的时候才会用到,而第N个块的加密IV则是用的N-1(N>1)个加密后的二进制数组。
CBC的解密则也是从左往右看,但是加密时IV在解密时候,只会用于对第一个块进行解密,其他块的解密则是使用上一块的加密二进制作为IV进行解密操作。
ps:通过加解密图解可知,初始向量IV在加密时针对整个待加密数据都起作用,但在解密时IV向量只针对第一个数据块起作用,因此在解密时,IV向量的重要度较低。
切记,在每次调用完加解密函数后,对应发IV向量的值会同步被修改
#include
#include
#include
#include
#include
#define AESKEY "df98b715d5c6ed2b25817b6f255411a1" //HEX密钥
#define AESIV "2841ae97419c2973296a0d4bdfe19a4f" //HEX初始向量
//将文本形式的HEX串进行转换
unsigned char* str2hex(char *str)
{
unsigned char *ret = NULL;
int str_len = strlen(str);
int i = 0;
assert((str_len%2) == 0);
ret = (char *)malloc(str_len/2);
for (i =0;i < str_len; i = i+2 )
{
sscanf(str+i,"%2hhx",&ret[i/2]);
}
return ret;
}
int main()
{
AES_KEY encryptkey;
AES_KEY decryptkey;
unsigned char *key;
unsigned char *stdiv;
key = str2hex(AESKEY);
stdiv = str2hex(AESIV);
AES_set_encrypt_key(key,128,&encryptkey);
AES_set_decrypt_key(key,128,&decryptkey);
unsigned char plain_text [32];
memcpy(plain_text, "AES encrypt in openssl demo", 27);
memset(plain_text + 27, 0, 5);
//需要将加密区块长度填充为16字节整数倍,此处使用zero-padding,即末尾全用0填充
printf("raw_text: ");
for(int i = 0; i < 32; i++)
{
printf("%02X ", plain_text[i]);
}
printf("\n" );
unsigned char encrypted_text [32];
memset(1, 0, 32);
unsigned char tmpiv[16];
memcpy(tmpiv, stdiv, 16);
AES_cbc_encrypt(plain_text, encrypted_text, 32, &encryptkey, tmpiv, AES_ENCRYPT);
//初始向量这个参数每次使用都会将其改变,所以如果要多次加密且每次使用固定的初始向量,可以先用tmpiv接收
printf("encrypted_text: " );
for(int i = 0; i < 32; i++)
{
printf("%02X ", encrypted_text[i]);
}
printf("\n" );
unsigned char decrypted_text [32];
memset(decrypted_text, 0, 32);
memcpy(tmpiv, stdiv, 16);
AES_cbc_encrypt(encrypted_text, decrypted_text, 32, &decryptkey, tmpiv, AES_DECRYPT);
printf("decrypted_text:");
for(int i = 0; i < 32; i++)
{
printf("%02X ", decrypted_text[i]);
}
printf("\n");
return 0;
}
@ubuntu:~/aes_source$ gcc test.c -o test -lssl -lcrypto
@ubuntu:~/aes_source$ ./test
raw_text: 41 45 53 20 65 6E 63 72 79 70 74 20 69 6E 20 6F 70 65 6E 73 73 6C 20 64 65 6D 6F 00 00 00 00 00
encrypted_text: 33 B7 65 49 2C 7E AB 0B 9C 90 2B 3A 0F 64 DF D3 60 A9 B6 7E 4D 9A 6F E0 08 19 EC AC E7 50 86 36
decrypted_text:41 45 53 20 65 6E 63 72 79 70 74 20 69 6E 20 6F 70 65 6E 73 73 6C 20 64 65 6D 6F 00 00 00 00 00
@ubuntu:~/aes_source$
参考link
<?xml version="1.0" encoding="UTF-8"?>
<bmp_para>
<para id="1">
<width>1920</width>
<height>1080</height>
<bit>3</bit>
<blue>0</blue>
<green>0</green>
<red>255</red>
</para>
</bmp_para>
#include
#include
#include
#include
#define DEFAULT_XML_FILE "test.xml"
//解析para字段,提取出width,height,bit,red,green,blue参数
static int parse_bmp(xmlDocPtr doc, xmlNodePtr cur)
{
assert(doc || cur);
xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
//获取width
if ((!xmlStrcmp(cur->name, (const xmlChar *)"width"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("width: %s\r\n", key);
xmlFree(key);
}
//获取height
if ((!xmlStrcmp(cur->name, (const xmlChar *)"height"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("height: %s\r\n", key);
xmlFree(key);
}
//获取bit
if ((!xmlStrcmp(cur->name, (const xmlChar *)"bit"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("bit: %s\r\n", key);
xmlFree(key);
}
//获取 blue
if ((!xmlStrcmp(cur->name, (const xmlChar *)"blue"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("blue: %s\r\n", key);
xmlFree(key);
}
//获取 green
if ((!xmlStrcmp(cur->name, (const xmlChar *)"green"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("green: %s\r\n", key);
xmlFree(key);
}
//获取 red
if ((!xmlStrcmp(cur->name, (const xmlChar *)"red"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("red: %s\r\n", key);
xmlFree(key);
}
cur = cur->next;
}
return 0;
}
static int parse_para(const char *file_name)
{
assert(file_name);
xmlDocPtr doc; //xml整个文档的树形结构
xmlNodePtr cur; //xml节点
xmlChar *id; //phone id
//获取树形结构
doc = xmlParseFile(file_name);
if (doc == NULL) {
fprintf(stderr, "Failed to parse xml file:%s\n", file_name);
goto FAILED;
}
//获取根节点
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr, "Root is empty.\n");
goto FAILED;
}
if ((xmlStrcmp(cur->name, (const xmlChar *)"bmp_para"))) {
fprintf(stderr, "The root is not bmp_para.\n");
goto FAILED;
}
//遍历处理根节点的每一个子节点
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"para"))) {
id = xmlGetProp(cur, "id");
printf("id:%s\r\n",id);
parse_bmp(doc, cur);
}
cur = cur->next;
}
xmlFreeDoc(doc);
return 0;
FAILED:
if (doc) {
xmlFreeDoc(doc);
}
return -1;
}
int main(int argc, char*argv[])
{
char cFileNameRead[64] = {0};
if(argc < 2)
{
printf("please input like this:\r\n");
printf("./testReadXml.bin test.xml \r\n");
printf("test.xml --------------- input xml file \r\n");
return -1;
}
sprintf(cFileNameRead,"%s",argv[1]);
printf("cFileNameRead=%s\r\n",cFileNameRead);
if (parse_para(cFileNameRead) != 0) {
fprintf(stderr, "Failed to parse bmp para.\n");
return -1;
}
return 0;
}
@ubuntu:~/xml$ gcc test.c -o test.bin -I /mnt/lib2xml/output/include/libxml2/ -L /mnt/lib2xml/output/lib/ -lxml2
@ubuntu:~/xml$ ./test.bin test.xml
cFileNameRead=test.xml
id:1
width: 1920
height: 1080
bit: 3
blue: 0
green: 0
red: 255