openssl aes

openssl aes

介绍

先生成key,然后用这个key去加解密
AES_set_encrypt_key
AES_set_decrypt_key
这两个的第二个参数必须是128 192 256,因为是按bit算的,所以16个字节的key乘8就行了,刚好128

Demo

#include <iostream>
#include <cstring>
#include <openssl/aes.h>

int main()
{
    char* key = "1234567890123456";
    AES_KEY enkey, dekey;
    if(AES_set_encrypt_key((const unsigned char*)key, strlen((char*)key) * 8, &enkey) != 0)
    {
        std::cout << "AES_set_encrypt_key failed!" << std::endl;
        return -1;
    }
    
    if(AES_set_decrypt_key((const unsigned char*)key, strlen((char*)key) * 8, &dekey) != 0)
    {
        std::cout << "AES_set_decrypt_key failed!" << std::endl;
        return -1;
    }

    char* m = "abcdefghijklmn";
    char enbuf[1024] = { 0 };
    char debuf[1024] = { 0 };
    AES_encrypt((const unsigned char*)m, (unsigned char*)enbuf, &enkey);
    AES_decrypt((const unsigned char*)enbuf, (unsigned char*)debuf, &dekey);

    std::cout << enbuf << std::endl;
    std::cout << debuf << std::endl;


    return 0;
}
上一篇:mfs+pacemaker+pcs+corosync+iscsi+fence实现mfs的集群管理,高可用等


下一篇:c++练习 加密函数 请编写一加密函数,函数原型是void encrypt(char * info) 该函数针对输入字符串的加密规律是:对字符串的每个字母以该字母后面第4个字母加以替换。