zoukankan      html  css  js  c++  java
  • 使用openssl的aes各种加密算法

    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <openssl/aes.h>
    
    //g++ -g -o -Wall -m64 AesTest AesTest.cpp -lssl -lcrypto
    //g++ -g -o -Wall AesTest AesTest.cpp -lssl -lcrypto
    
    int main(int argc, char **argv)
    {//由于与直接对接用的char,那么加解密要强制转换
        char Source[1024];
        char *InputData=NULL;
        char *EncryptData=NULL;
        char *DecryptData=NULL;
         
        unsigned char Key[AES_BLOCK_SIZE+1];    //建议用unsigned char
        unsigned char ivec[AES_BLOCK_SIZE];     //建议用unsigned char
        AES_KEY AesKey;
         
        int DataLen=0,SetDataLen=0, i;
    
        memset(Source, 0x00, sizeof(Source));
        strcpy(Source, "1234567890abcde");  //要加密的数据
        DataLen = strlen(Source);
    
        memset(Key, 0x00, sizeof(Key));
        memcpy(Key, "0123456789abcdef", AES_BLOCK_SIZE);
    
     // set the encryption length
        SetDataLen = 0;
        if ((DataLen%AES_BLOCK_SIZE) == 0)
        {
            SetDataLen = DataLen;
        }
        else
        {
            SetDataLen = ((DataLen/AES_BLOCK_SIZE)+1) * AES_BLOCK_SIZE;
        }
        printf("SetDataLen:%d...
    ", SetDataLen);   //取16的倍数
         
        InputData = (char *)calloc(SetDataLen+1, sizeof(char));
        if(InputData == NULL)   //注意要SetDataLen+1
        {
            fprintf(stderr, "Unable to allocate memory for InputData
    ");
            exit(-1);
        }
        memcpy(InputData, Source, DataLen);
         
        EncryptData = (char *)calloc(SetDataLen+1, sizeof(char));
        if(EncryptData == NULL) //注意要SetDataLen+1
        {
            fprintf(stderr, "Unable to allocate memory for EncryptData
    ");
            exit(-1);
        }
         
        DecryptData = (char *)calloc(SetDataLen+1, sizeof(char));
        if(DecryptData == NULL) //注意要SetDataLen+1
        {
            fprintf(stderr, "Unable to allocate memory for DecryptData
    ");
            exit(-1);
        }
    
        memset(&AesKey, 0x00, sizeof(AES_KEY));
        if(AES_set_encrypt_key(Key, 128, &AesKey) < 0)
        {//设置加密密钥
            fprintf(stderr, "Unable to set encryption key in AES...
    ");
            exit(-1);
        }
    
        for(i=0; i<AES_BLOCK_SIZE; i++)
        {//必须要有
            ivec[i] = 0;
        }
        //加密
        AES_cbc_encrypt((unsigned char *)InputData, (unsigned char *)EncryptData, 
            SetDataLen, &AesKey, ivec, AES_ENCRYPT);   
    
        memset(&AesKey, 0x00, sizeof(AES_KEY));
        if(AES_set_decrypt_key(Key, 128, &AesKey) < 0)
        {//设置解密密钥
            fprintf(stderr, "Unable to set encryption key in AES...
    ");
            exit(-1);
        }
    
        for(i=0; i<AES_BLOCK_SIZE; i++)
        {//必须要有
            ivec[i] = 0;
        }
        //解密
        AES_cbc_encrypt((unsigned char *)EncryptData, (unsigned char *)DecryptData, 
            SetDataLen, &AesKey, ivec, AES_DECRYPT); 
    
        printf("DecryptData:%s...
    ", (char *)DecryptData);
    
        if(InputData != NULL)
        {
            free(InputData);
            InputData = NULL;
        }
         
        if(EncryptData != NULL)
        {
            free(EncryptData);
            EncryptData = NULL;
        }
         
        if(DecryptData != NULL)
        {
            free(DecryptData);
            DecryptData = NULL;
        }
    
        exit(0);
    }
    
    
     
    
     
    
    OpenSSL支持多种不同的加密算法
    加密:
    AES, Blowfish, Camellia, SEED, CAST-128, DES, IDEA, RC2, RC4, RC5, Triple DES, GOST 28147-89[4]
    散列函数:
    MD5, MD2, SHA-1, SHA-2, RIPEMD-160, MDC-2, GOST R 34.11-94[4]
    公开密钥加密:
    RSA, DSA, Diffie–Hellman key exchange, Elliptic curve, GOST R 34.10-2001[4]

  • 相关阅读:
    软件项目开发典型风险一览
    删除数据库所有表数据
    今天愚人节,教大家一个真正的最强整人方法
    潘正磊谈微软研发团队管理之道(下)
    追MM与23种设计模式
    22个所见即所得在线 Web 编辑器
    神奇的js代码,图片全都飞起来了
    字体 小 中 大
    使用ODP.NET连接Oracle数据库一个OracleCommand运行多条SQL语句的方法
    删除SQL数据库中所有的表
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9410056.html
Copyright © 2011-2022 走看看