/study-cryption/src/main/java/com/bage/study/cryption/github/lugeek/AES.java
https://github.com/bage2014/study · Java · 76 lines · 48 code · 19 blank · 9 comment · 3 complexity · eccaf8ca729bc08b51bb634569e714b3 MD5 · raw file
- package com.bage.study.cryption.github.lugeek;
- import java.security.Key;
- import java.security.SecureRandom;
- import java.util.Base64;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * 来自:https://github.com/lugeek/Encryption/
- * @author bage
- *
- */
- public class AES {
- public static final String ALGORITHM = "AES";
-
- public static byte[] decryptBASE64(String key) throws Exception {
- return Base64.getDecoder().decode(key);
- }
- public static String encryptBASE64(byte[] key) throws Exception {
- return Base64.getEncoder().encodeToString(key);
- }
- private static Key toKey(byte[] key) throws Exception {
- //DESKeySpec dks = new DESKeySpec(key);
- //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
- //SecretKey secretKey = keyFactory.generateSecret(dks);
- // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码
- SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
- return secretKey;
- }
- public static byte[] decrypt(byte[] data, String key) throws Exception {
- Key k = toKey(decryptBASE64(key));
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- cipher.init(Cipher.DECRYPT_MODE, k);
- return cipher.doFinal(data);
- }
- public static byte[] encrypt(byte[] data, String key) throws Exception {
- Key k = toKey(decryptBASE64(key));
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- cipher.init(Cipher.ENCRYPT_MODE, k);
- return cipher.doFinal(data);
- }
- public static String initKey() throws Exception {
- return initKey(null);
- }
- public static String initKey(String seed) throws Exception {
- SecureRandom secureRandom = null;
- if (seed != null) {
- secureRandom = new SecureRandom(decryptBASE64(seed));
- } else {
- secureRandom = new SecureRandom();
- }
- KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
- kg.init(secureRandom);
- SecretKey secretKey = kg.generateKey();
- return encryptBASE64(secretKey.getEncoded());
- }
- }