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=");