CryptoJS C/C++ openssl Java AES_256_ecb 加密解密互通
CryptoJS
<script type="text/javascript" src="crypto-js.js"></script>
<script type="text/javascript">
// 加密
var ciphertext = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse('123456'), CryptoJS.enc.Utf8.parse('12345678901234567890123456789012'), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }).toString();
console.log(ciphertext);
// 解密
var bytes = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse('12345678901234567890123456789012'), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
var originalText = bytes.toString(CryptoJS.enc.Utf8);
console.log(originalText);
</script>
效果
解析base64查看原始密文
Java
public static byte[] AES_256_ecb_encrypt(byte[] input, byte[] byteKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKey key = new SecretKeySpec(byteKey, "AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(input);
return encrypted;
}
public static byte[] AES_256_ecb_decrypt(byte[] input, byte[] byteKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKey key = new SecretKeySpec(byteKey, "AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(input);
return decrypted;
}
@Test
public void testAES_256_ecb_encrypt() throws Exception {
String input = "123456";
String key = "12345678901234567890123456789012";
byte[] encrypted = CipherUtil.AES_256_ecb_encrypt(input.getBytes(), key.getBytes());
log.debug(CipherUtil.hexDumpEncoder.encode(encrypted));
byte[] decrypted = CipherUtil.AES_256_ecb_decrypt(encrypted, key.getBytes());
String decyptedStr = new String(decrypted);
log.debug(decyptedStr);
}
效果
C(调用openssl crypto库)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
void printhexDump(const char *buffer, size_t len)
{
if (buffer == NULL || len <= 0)
{
return;
}
printf("0x%x: (len=%d)[", buffer, len);
for (size_t i = 0; i < len; i++)
{
printf("%.2X ", (unsigned char)buffer[i]);
}
printf("]\n");
}
int main()
{
unsigned char key[32] = "12345678901234567890123456789012";
unsigned char *rawData = "123456";
int rawDataLen = strlen(rawData);
int encLen = 0;
int outLen = 0;
unsigned char encData[1024];
printf("%s\n", rawData);
EVP_CIPHER_CTX *ctx;
ctx = EVP_CIPHER_CTX_new();
EVP_CipherInit_ex(ctx, EVP_aes_256_ecb(), NULL, key, NULL, AES_ENCRYPT);
EVP_CipherUpdate(ctx, encData, &outLen, rawData, rawDataLen);
encLen = outLen;
printf("%d\n", encLen);
EVP_CipherFinal(ctx, encData + outLen, &outLen);
encLen += outLen;
printf("%d\n", encLen);
EVP_CIPHER_CTX_free(ctx);
printhexDump(encData, encLen);
//
int decLen = 0;
int outlen = 0;
unsigned char decData[1024];
EVP_CIPHER_CTX *ctx2;
ctx2 = EVP_CIPHER_CTX_new();
EVP_CipherInit_ex(ctx2, EVP_aes_256_ecb(), NULL, key, NULL, AES_DECRYPT);
EVP_CipherUpdate(ctx2, decData, &outlen, encData, encLen);
decLen = outlen;
EVP_CipherFinal(ctx2, decData + outlen, &outlen);
decLen += outlen;
EVP_CIPHER_CTX_free(ctx2);
decData[decLen] = '\0';
printf("decrypt: %s\n", decData);
return 0;
}
效果
总结
明文:123456
密钥(32字节256位):12345678901234567890123456789012
经AES加密算法
CryptoJS: { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }
Java: Cipher.getInstance("AES/ECB/PKCS5Padding"))
C openssl:EVP_aes_256_ecb()
得到相同密文十六进制:D7 86 C4 BC 0E C2 7B 22 28 A2 96 24 62 AB 4D 5B
解密得到相同明文:123456
Qt C++可直接采用C openssl库方案。