AES加密,128-192-256,方案二
来源:互联网 发布:java transient 作用 编辑:程序博客网 时间:2024/05/20 09:06
与方案一对比,方案二要简洁很多。
看代码:
package com.xiao.aes.util;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
/**
*
* 美国软件出口限制,JDK默认使用的AES算法最高只能支持128位。如需要更高的支持需要从oracle官网下载更换JAVA_HOME/jre/lib/
* security目录下的: local_policy.jar和US_export_policy.jar。<br/>
* 采用补码方式以及base64双重加密,依赖commons-codec-1.x.jar包中的base64加密
*
* @author xiao
*
*/
public class AESUtil2 {
/**
*
*/
private static Cipher cipher;
/**
* 初始化向量
*/
private static IvParameterSpec iv;
static {
try {
byte[] vi = Hex.decodeHex("12345678123456781234567812345678"
.toCharArray());
iv = new IvParameterSpec(vi);
// "算法/模式/补码方式"
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DecoderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 生成密钥
*
* @param type
* AES长度
* @return 密钥
*/
public static String createAESKey(AESType type) {
try {
KeyGenerator key = KeyGenerator.getInstance("AES");
key.init(type.value);
SecretKey ckey = key.generateKey();
byte[] keyByte = ckey.getEncoded();
return Base64.encodeBase64String(keyByte);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/**
* AES加密
*
* @param key
* 密钥
* @param plaintext
* 明文
* @return 秘文
*/
public static String encryptAES(String key, String plaintext) {
String ciphertext = "";
try {
byte[] keyByte = Base64.decodeBase64(key);
SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "AES");
// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] pbyte = plaintext.getBytes("utf-8");
byte[] result = cipher.doFinal(pbyte);
ciphertext = Base64.encodeBase64String(result);
} catch (Exception e) {
e.printStackTrace();
}
return ciphertext;
}
/**
* 解密
*
* @param key
* 密钥
* @param ciphertext
* 秘文
* @return 明文
*/
public static String decryptAES(String key, String ciphertext) {
String plaintext = "";
try {
byte[] keyByte = Base64.decodeBase64(key);
byte[] cbyte = Base64.decodeBase64(ciphertext);
SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "AES");
// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] pbyte = cipher.doFinal(cbyte);
plaintext = new String(pbyte, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return plaintext;
}
public static void main(String[] args) {
String key128 = createAESKey(AESType.AES_128);
String key192 = createAESKey(AESType.AES_192);
String key256 = createAESKey(AESType.AES_256);
System.out.println("Key 128:" + key128);
System.out.println("Key 192:" + key192);
System.out.println("Key 256:" + key256);
String source = "AES加密测试";
String ciphertext128 = encryptAES(key128, source);
String ciphertext192 = encryptAES(key192, source);
String ciphertext256 = encryptAES(key256, source);
System.out.println("Ciphertext 128:" + ciphertext128);
System.out.println("Ciphertext 192:" + ciphertext192);
System.out.println("Ciphertext 256:" + ciphertext256);
String plaintext128 = decryptAES(key128, ciphertext128);
String plaintext192 = decryptAES(key192, ciphertext192);
String plaintext256 = decryptAES(key256, ciphertext256);
System.out.println("Plaintext 128:" + plaintext128);
System.out.println("Plaintext 192:" + plaintext192);
System.out.println("Plaintext 256:" + plaintext256);
}
}
- AES加密,128-192-256,方案二
- AES加密,128-192-256,方案一
- AES+JNI 加密方案
- AES与RSA相结合数据加密方案
- android数据加密(二)AES加密
- Android数据传输加密(二):AES加密
- AES 192加密
- iOS AES 256加密
- AES 256 位 加密
- AES 128 加密补充
- AES 加密 128位
- AES加密学习笔记(二)
- AES 加密解密的 JAVA 实现 【二】
- AES+base64实现双重加密(二)
- java使用AES加密解密 AES-128-ECB加密
- AES 256位 加密解密
- android AES-256-CBC加密
- AES-128 ECB 加密有感
- 地震数据SEGY格式介绍及其查看分析(附示例地震数据)
- 关于opencv中的imread()函数声明
- ios 学习备忘
- android GridView默认选中分析
- 【SSH项目实战】国税协同平台-4.用户管理需求分析&CRUD方法1
- AES加密,128-192-256,方案二
- Kinect工作流程
- 【SSH项目实战】国税协同平台-4.用户管理需求分析&CRUD方法2
- 查看virsh管理qemu虚拟机日志信息和libvirtd日志信息
- uva111(dp)
- Java 程序性能优化《第一章》Java性能调优概述 1.3基本调优策略和手段
- Android从源码分析二:AsyncTask异步任务
- 【SSH项目实战】国税协同平台-5.头像上传功能
- 关于友盟自定义视图实现分享 (微信朋友 微信朋友圈 微信收藏)