基本信息
源码名称:java AES加密/解密算法 示例源码
源码大小:0.95KB
文件格式:.rar
开发语言:Java
更新时间:2017-11-02
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
直接解压,放入工程,运行测试。
直接解压,放入工程,运行测试。
package com;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES方式加解密算法
* @author venus4A
*/
public class AES {
/**
* 由服务端提供给调用者的一个用于数据加密的共享密钥
*/
private String _publicKey = null;
/**
*
* @param publicKey AES密钥
*/
public AES() {
String publicKey = "jKV4GEAwBtcyehND";
_publicKey = publicKey;
}
public static final String KEY_ALGORITHM = "AES";
public static final String CIPHER_ALGORITHM = "AES/CTR/PKCS5Padding";
public static final String ivParameter = "1234567890abcdef";
/**
* 解密
* @param data
* @return
*/
public String decrypt(String data) {
try {
SecretKeySpec skeySpec = new SecretKeySpec(_publicKey.getBytes("ASCII"), KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/**
* 加密
* @param data
* @return
*/
public String encrypt(String data) {
try {
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
SecretKeySpec sKeySpec = new SecretKeySpec(_publicKey.getBytes(), KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
byte[] encrypted = cipher.doFinal(data.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws Exception {
//举例
String str = "王海平";
AES aes = new AES();
str = aes.encrypt(str);
System.out.println("加密=" str);
str = aes.decrypt(str);
System.out.println("解密=" str);
}
}