AES加解密,JAVA密码器的使用
分组密码简介
分组密码是将明文消息编码表示后的数字(简称明文数字)序列,划分成长度为n的组(可看成长度为n的矢量),每组分别在密钥的控制下变换成等长的输出数字(简称密文数字)序列。
主要分组密码有:DES,AES,这些密码在JAVA中的使用,皆可以通过java自带的密码器来实现。
AES简介
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。
加密器语句
1.创建密码器
Cipher cipher = Cipher.getInstance();
// 创建密码器
其中,括号中可以填写加密的”算法/模式/补码方式”
如
Cipher cipher = Cipher.getInstance(“AES/ECB/PKCS5Padding”);//”算法/模式/补码方式”
就是AES加密,工作模式是ECB
当然也可以简写为:
Cipher cipher = Cipher.getInstance(“AES”);
这只是创建一个AES加密器,其他方式皆为默认值。
2.初始化
cipher.init(Cipher.ENCRYPT_MODE, key);
其中,key为要使用的密钥。
这个密钥可以用密钥生成语句来规范SecretKey,这个我们会单独写一个章节。
3.加密
byte[] encrypted = cipher.doFinal(sSrc.getBytes(“utf-8”));
sSrc为加密内容,后面的语句表示把其转成utf-8来进行加密,加解密的转换用的规则必须一致。
这样我们新建的字节数组 encrypted就是我们获得的加密后的密文字节。
具体程序案列
import java.security.*;
import javax.crypto.*;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
public class AES2 {
public AES2() {
}
//测试
public static void main(String args[]) {
String content = "testtest123456";
String password = "12345678";
//加密
System.out.println("加密前:" + content);
byte[] encryptResult = encrypt(content, password);
//解密
byte[] decryptResult = decrypt(encryptResult,password);
System.out.println("解密后:" + new String(decryptResult));
}
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @return
*/
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
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);
return result; // 加密
} 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;
}
/**解密
* @param content 待解密内容
* @param password 解密密钥
* @return
*/
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
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 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();
}
return null;
}
/**
* 异或运算
*/
public static byte[] xor(byte[] content, byte[] key){
byte[] result=new byte[64];
for(int i=0;i<64;i++){
result[i]=(byte)(content[i]^key[i]);
}
return result;
}
}
结果图