Java里面生成AES加密,c++里面AES解密

之前文章c++实现AES加密解密算法介绍了c++实现加解密,本文介绍:用Java代码生成AES加密字符串,在c++代码里面解密字符串

一、Java代码

加密:

public static String encrypt(String content) {
		try {
			if (content.isEmpty())
				return "";

			String password = "t7CLWhtUTAgAFbw0";
			SecretKeySpec key = new SecretKeySpec(password.getBytes(), "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);
			SecretKeySpec key = new SecretKeySpec(password.getBytes(), "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++测试代码

使用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[])
{
	CAes256::initAes("t7CLWhtUTAgAFbw0");
	// "hello world"
	string enstr = "IlphHBX6JENV1iW09ZW2Tg=="; // 原文:hello world,加密字符串来自java代码加密
	string str = CAes256::decryptData("IlphHBX6JENV1iW09ZW2Tg=="); // c++解密
	cout << "str = " << str << endl;
	cout << "enstr = " << CAes256::encryptData(str) << endl; // c++加密,对比结果与java代码加密的是否一样
	
	system("pause");

	return 0;
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁爸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值