AES/CBC/PKCS5Padding+Base64解码

C语言实现:

#include <openssl/evp.h>
#include <openssl/aes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <android/log.h>

#define LOG_TAG "llx"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

void decryptAES(const char* encryptedText, const char* key, const char* iv) {
    // 将密钥和IV转换为字节数组
    unsigned char* keyBytes = (unsigned char*)key;
    unsigned char* ivBytes = (unsigned char*)iv;	

    // 解码Base64
    size_t encryptedLength = strlen(encryptedText);
    unsigned char* encryptedBytes = (unsigned char*)malloc(encryptedLength);
    memset(encryptedBytes, 0, encryptedLength);
    int decodedLength = EVP_DecodeBlock(encryptedBytes, (const unsigned char*)encryptedText, encryptedLength);
    
    // 校正解码后的数据长度
    if(encryptedText[encryptedLength-1]=='=') {
    	decodedLength --;
    	if(encryptedText[encryptedLength-2]=='=') {
    	   decodedLength --;
    	}
    }
    
    LOGD("decodedLength: %d, encryptedLength: %zu", decodedLength, encryptedLength);

    // 创建AES解密上下文
    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();

    // 设置解密算法和参数
    EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, keyBytes, ivBytes);

    // 计算解密后的数据长度
    int plaintextLength = decodedLength + AES_BLOCK_SIZE;
    unsigned char* plaintext = (unsigned char*)malloc(plaintextLength);
    memset(plaintext,0,plaintextLength);

    // 解密数据
    int decryptedLength = 0;
    EVP_DecryptUpdate(ctx, plaintext, &decryptedLength, encryptedBytes, decodedLength);

    // 解密结束
    int finalLength = 0;
    EVP_DecryptFinal_ex(ctx, plaintext + decryptedLength, &finalLength);

    // 计算最终解密后的数据长度
    int totalLength = decryptedLength + finalLength;

    // 输出解密结果
    printf("%.*s\n", totalLength, plaintext);

    // 清理资源
    free(plaintext);
    free(encryptedBytes);
    EVP_CIPHER_CTX_cleanup(ctx);
    EVP_CIPHER_CTX_free(ctx);
}

int main() {
    const char* encryptedText = "UKpe1OIJ0at/TDwQjEv0kxkCORnyVzu8W+mWtaWihGxr4H+rAf1HIbGafOynuM/6ITv09fT9lB8IHAm6jkFflddbf7Ok5g0afPgXsnkVwKE=";
    const char* key = "u23vFWFslqqkLEZDm6fU3wRwzvx2QLQU";
    const char* iv = "0123456789abcdef";

    decryptAES(encryptedText, key, iv);

    return 0;
}
LOCAL_LDLIBS := -lcrypto -lssl

Java实现:

import java.nio.charset.StandardCharsets;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public static String AESDecryptor(String encryptedText) {
    return AESDecryptor(encryptedText, "u23vFWFslqqkLEZDm6fU3wRwzvx2QLQU");
}

public static String AESDecryptor(String encryptedText, String key) {
    return AESDecryptor(encryptedText, key, "0123456789abcdef");
}

public static String AESDecryptor(String encryptedText, String key, String iv) {
    try {
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);// decode Base64

        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);

        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
        Log.d("llx", decryptedText);
        return decryptedText;
    } catch (Exception e) {
        Log.e("llx",e.toString());
    }
    return null;
}
AESDecryptor("UKpe1OIJ0at/TDwQjEv0kxkCORnyVzu8W+mWtaWihGxr4H+rAf1HIbGafOynuM/6ITv09fT9lB8IHAm6jkFflddbf7Ok5g0afPgXsnkVwKE=");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值