AES加解密代码

C++代码

#ifndef TAesCipher_H_
#define TAesCipher_H_

extern "C"
{
#include<openssl/evp.h>
#include<openssl/bio.h>
#include<openssl/x509.h>
}
#include<cstdio>
#include<cstring>
#include<string>
#include "../../comm/libs/utils/ll_log.h"

class TAesCipher
{
public:
    static bool encrypt(const std::string & key, const std::string & iv, const std::string & in, std::string & out)
    {
        BIO *baes = BIO_new(BIO_f_cipher());    
        BIO *b64 = BIO_new(BIO_f_base64());
        BIO *bmem = BIO_new(BIO_s_mem());
        if(!baes || !b64 || !bmem)
        {
            LLOGE("BIO_new error
");
            if(baes)BIO_free(baes);
            if(b64)BIO_free(b64);
            if(bmem)BIO_free(bmem);
            return false;
        }
        
        BIO_set_cipher(baes, EVP_aes_128_cbc(),(unsigned char*)key.c_str(), (unsigned char*)iv.c_str(), 1);    
        baes = BIO_push(baes, b64);
        baes = BIO_push(baes, bmem);
        
        if(BIO_write(baes, in.c_str(), in.length()) < 0)
        {
            LLOGE("BIO_write error
");
            BIO_free_all(baes);
            return false;
        }
        BIO_flush(baes);
        
        BUF_MEM *bptr = NULL;
        BIO_get_mem_ptr(b64,&bptr);    
        if(!bptr)
        {
            LLOGE("BIO_get_mem_ptr error
");
            BIO_free_all(baes);
            return false;
        }
        out.assign(bptr->data, bptr->length);

        BIO_free_all(baes);
        return true;
    }
    static bool decrypt(const std::string & key, const std::string & iv, const std::string & in, std::string & out)
    {
        BIO *baes = BIO_new(BIO_f_cipher());
        BIO *b64 = BIO_new(BIO_f_base64());
        BIO *bmem = BIO_new_mem_buf((void*)in.c_str(), in.length());
        if(!baes || !b64 || !bmem)
        {
            LLOGE("BIO_new error
");
            if(baes)BIO_free(baes);
            if(b64)BIO_free(b64);
            if(bmem)BIO_free(bmem);
            return false;
        }
        
        BIO_set_cipher(baes, EVP_aes_128_cbc(),(unsigned char*)key.c_str(), (unsigned char*)iv.c_str(), 0);
        baes = BIO_push(baes, b64);
        baes = BIO_push(baes,bmem);
            
        char decode[256] = {0};
        int ret = BIO_read(baes,decode,sizeof(decode));
        std::string decryptStr;
        while(ret > 0)
        {
            decryptStr.append(decode, ret);
            memset(decode, 0, sizeof(decode));
            ret = BIO_read(baes,decode,sizeof(decode));    
        }
            
        if(!BIO_get_cipher_status(baes))
        {
            LLOGE("decrypt error
");
            BIO_free_all(baes);
            return false;
        }
        
        out.swap(decryptStr);
        
        BIO_free_all(baes);
        return true;
    }
    
};

#endif

AES解密时传入的base64编码数据后面至少有一个“ ”换行符符,否则解密失败。注意在AES加密时如果使用BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL),那么加密后base64编码不会添加换行符" "

JAVA代码

import java.util.Base64; 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class TAesCipher
{
    public static String encrypt(String key, String iv, String in) throws Exception
    {
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec sIvSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, sIvSpec);
        byte[] result = cipher.doFinal(in.getBytes());    
        return Base64.getEncoder().encodeToString(result);    
    }
    
    public static String decrypt(String key, String iv, String in) throws Exception
    {
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec sIvSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, sIvSpec);
        byte[] result = cipher.doFinal(Base64.getDecoder().decode(in.getBytes()));
        return new String(result);
    }    
}
原文地址:https://www.cnblogs.com/mingzhang/p/13906466.html