使用JDK8引入的 java.util.Base64 负责base64转码
Base64Tools 工具类,AES加密要用到它,负责 byte 数组的base64编码,base64 编码的目的是消除乱码
import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class Base64Tools {
private final static Base64.Decoder decoder = Base64.getDecoder();
private final static Base64.Encoder encoder = Base64.getEncoder();
public static byte[] encode(byte[] content) throws UnsupportedEncodingException {
return encoder.encode(content);
}
public static byte[] decode(byte[] content) throws UnsupportedEncodingException {
return decoder.decode(content);
}
}
AESTools 工具类,负责对 byte 数组加密和解密
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
public class AESTools {
private static final Base64.Decoder decoder = Base64.getDecoder();
private static final Base64.Encoder encoder = Base64.getEncoder();
public static byte[] AESEncode(String password, byte[] content) {
try {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
keygen.init(128, random);
SecretKey originalKey = keygen.generateKey();
SecretKey key = new SecretKeySpec(originalKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return Base64Tools.encode(cipher.doFinal(content)); // 加密后用base64编码
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
public static byte[] AESDecode(String password, byte[] content) {
try {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
keygen.init(128, random);
SecretKey originalKey = keygen.generateKey();
SecretKey key = new SecretKeySpec(originalKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(Base64Tools.decode(content));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
}
测试用例我从网上找了一段文字,将它加密后再解密,和原文进行对比
public class Main {
public static void main(String[] args) throws Exception {
String password = "passwordwhateveryouwant";
String content = "We hold these truths to be self-evident, that all men are created equal\n我们认为下述真理是不言而喻的:人人生而平等";
System.out.println(content);
String encryptedString = new String(AESTools.AESEncode(password, content.getBytes()));
System.out.println(encryptedString);
String decryptedString = new String(AESTools.AESDecode(password, encryptedString.getBytes()));
System.out.println(decryptedString);
if (content.equals(decryptedString)) System.out.printf("原文和加密后再解密的结果相等");
else System.out.printf("原文和加密后再解密的结果不相等");
}
}
运行结果