之前文章Java里面生成AES加密,c++里面AES解密介绍了Java代码加密,c++代码解密。本文介绍:Java带KeyGenerator(密钥生成器)生成AES加密,c++里面AES解密
一、Java代码
加密:
public static void OutPut(byte[] content) {
for (int i=0; i<content.length; i++) {
System.out.print(content[i] + ", ");
}
System.out.print("\n");
}
public static String encrypt(String content) {
try {
if (content.isEmpty())
return "";
String password = "t7CLWhtUTAgAFbw0";
// 使用密钥生成器
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
OutPut(enCodeFormat);
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
String str = Base64.encodeBase64String(result);
return str; // 加密
} catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
} catch (NoSuchPaddingException e) {
// e.printStackTrace();
} catch (InvalidKeyException e) {
// e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// e.printStackTrace();
} catch (BadPaddingException e) {
// e.printStackTrace();
}
return null;
}
解密:
public static String decrypt(String str) {
try {
String password = "t7CLWhtUTAgAFbw0";
byte[] content = Base64.decodeBase64(str);
// 使用密钥生成器
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return new String(result);
} catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
} catch (NoSuchPaddingException e) {
// e.printStackTrace();
} catch (InvalidKeyException e) {
// e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// e.printStackTrace();
} catch (BadPaddingException e) {
// e.printStackTrace();
} catch (Exception e) {
// e.printStackTrace();
}
return "";
}
二、Java测试代码
String enStr = encrypt("hello world");
System.out.println("enStr: " + enStr);
System.out.println("deStr: " + decrypt(enStr));
测试结果:
重要说明:c++能解密的关键是获取上面输出框里面打印除了的值,把它作为c++里面解密的key
c++代码片段:
UINT8 gAesKey[32] = { -84, 115, -48, 61, 3, 127, 34, 37, -73, -21, 85, 100, -4, 117, 63, 19 }; // t7CLWhtUTAgAFbw0
bool gInitFlag = false;
CAes256::CAes256()
{
}
void CAes256::initAes(const string& key)
{
if (key != "") //t7CLWhtUTAgAFbw0
{
memset(gAesKey, 0x00, 32);
memcpy_s(gAesKey, 16, key.c_str(), 16);
}
aesInit(gAesKey);
}
三、c++测试代码
使用CAesLib.lib,代码如下:
// TestAesLib.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include "encrypt_decrypt.h"
using namespace std;
#pragma comment(lib, "CAesLib.lib")
int _tmain(int argc, _TCHAR* argv[])
{
// "hello world"
string enstr = "czYfaKaALBfQwIlojCC0Dg=="; // 原文:hello world,加密字符串来自java代码加密
string str = CAes256::decryptData(enstr ); // c++解密
cout << "str = " << str << endl;
cout << "enstr = " << CAes256::encryptData(str) << endl; // c++加密,对比结果与java代码加密的是否一样
system("pause");
return 0;
}
运行结果: