AES加密详解

下面讲解一下我使用AES加密遇到的问题:

在代码上一一列举和解释:

一直以来Base64的加密解密都是使用sun.misc包下的BASE64EncoderBASE64Decodersun.misc.BASE64Encoder/BASE64Decoder类。这人个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。但是在eclipseMyEclipse中直接使用,却找不到该类。!

import java.security.SecureRandom;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class aes {
	
	//全局变量
//	private static String password_model = "AES/CBC/PKCS5Padding";
	private static String password_model = "AES";
	private static int encode_bit = 128;
	private static String charsetName = "utf-8";
	
	//加密方法
	public static String AES_ENcode (String key, String password) throws Exception{
		
		//1、构造密钥生成器
		KeyGenerator keyGenerator = KeyGenerator.getInstance(password_model);
		//产生可信任的随机源
		SecureRandom secureRandom = new SecureRandom(key.getBytes());
		//2、初始化密钥生成器
		keyGenerator.init(encode_bit, secureRandom);
		//3、产生原始对称密钥
		SecretKey original_key = keyGenerator.generateKey();
		//4、获得原始对称密钥的字节数组
		byte[] byte_original_key = original_key.getEncoded();
		//5、根据字节数组生成AES密钥
		SecretKey secretKey = new SecretKeySpec(byte_original_key, password_model);
		//输出密钥的内容
		System.out.println("此密钥的内容:" + secretKey.getEncoded());
		//6、密码生成器
		Cipher cipher = Cipher.getInstance(password_model);
		//7、初始化密码器
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		//看看Cipher.DECRYPT_MODE代表的是什么数字
		System.out.println("Cipher.ENCRYPT_MODE:" + Cipher.ENCRYPT_MODE);
		//8、获取加密内容的字节数字。为防止有中文,设置格式为“utf-8”
		byte[] byte_password = password.getBytes(charsetName);
		//9、加密
		byte[] byte_encode = cipher.doFinal(byte_password);
		/**10、转化为字符串
		 * 	使用Base64eEncode中会找不到包
		 * 解决办法:在build path中先移除system library 然后再添加,重新编译就OK了
		 
右键项目-》属性-》java bulid path-》jre System Library-》access rules-》resolution选择accessible,下面填上** 点击确定即可!!!
*/

String string_encode = new String(new BASE64Encoder().encode(byte_encode));

return string_encode;
}

//解密
public static String AES_DEcode(String key, String password) throws Exception{

//构造密钥生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance(password_model);
//初始化
keyGenerator.init(encode_bit, new SecureRandom(key.getBytes()));
//原始对称密钥
SecretKey original_key = keyGenerator.generateKey();
//对称密钥字节
byte[] byte_original_key = original_key.getEncoded();
//生成aes密钥
SecretKey secretKey = new SecretKeySpec(byte_original_key, password_model);
//密码生成器
Cipher cipher = Cipher.getInstance(password_model);
//初始化
cipher.init(Cipher.DECRYPT_MODE, secretKey);
System.out.println("DECRYPT_MODE:" + Cipher.DECRYPT_MODE);
//将内容边写成字节
byte[] byte_password = new BASE64Decoder().decodeBuffer(password);
//解密
byte[] byte_decode = cipher.doFinal(byte_password);
String string_decode =  new String(byte_decode, charsetName);
return string_decode;
}

public static void main(String[] args) {

String key = "123456";
String password = "test";
System.out.println(password.length());

try {
//加密
String string_encode = AES_ENcode(key, password);
System.out.println("加密:" + string_encode);
//解密
String string_decode = AES_DEcode(key, string_encode);
System.out.println("解密:" + string_decode);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}

多有不足的地方,望各位提出意见,我及时修改,谢谢批评,大神勿喷

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值