|
|
@@ -1,148 +1,73 @@
|
|
|
package com.goafanti.common.utils;
|
|
|
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
-import java.security.InvalidKeyException;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
-import java.security.SecureRandom;
|
|
|
-
|
|
|
-import javax.crypto.BadPaddingException;
|
|
|
import javax.crypto.Cipher;
|
|
|
-import javax.crypto.IllegalBlockSizeException;
|
|
|
-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.binary.Base64;
|
|
|
|
|
|
public class AesUtils {
|
|
|
|
|
|
- /**
|
|
|
- * 加密
|
|
|
- *
|
|
|
- * @param pwd
|
|
|
- * @param aesSecretKey
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String AesEncrypt(String pwd, String aesSecretKey) {
|
|
|
- return parseByte2HexStr(encrypt(pwd, aesSecretKey));
|
|
|
- }
|
|
|
+ public static String encrypt(String content, String key) throws Exception {
|
|
|
+ try {
|
|
|
+ String data = content;
|
|
|
+ String iv = key;
|
|
|
|
|
|
- /**
|
|
|
- * 解密
|
|
|
- *
|
|
|
- * @param pwd
|
|
|
- * @param aesSecretKey
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String AesDecrypt(String pwd, String aesSecretKey) {
|
|
|
- return new String(decrypt(parseHexStr2Byte(pwd), aesSecretKey));
|
|
|
- }
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
|
|
+ int blockSize = cipher.getBlockSize();
|
|
|
|
|
|
- public static byte[] encrypt(String content, String password) {
|
|
|
- try {
|
|
|
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
|
|
|
- SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
|
|
|
- random.setSeed(password.getBytes());
|
|
|
-
|
|
|
- //kgen.init(128, random);
|
|
|
- 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[] ret = encode(content.getBytes("UTF-8"),password);
|
|
|
- //encrypt = new String(Base64.encode(ret));
|
|
|
- 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) {
|
|
|
+ byte[] dataBytes = data.getBytes();
|
|
|
+ int plaintextLength = dataBytes.length;
|
|
|
+ if (plaintextLength % blockSize != 0) {
|
|
|
+ plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] plaintext = new byte[plaintextLength];
|
|
|
+ System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
|
|
|
+
|
|
|
+ SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
|
|
|
+ IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
|
|
|
+
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
|
|
|
+ byte[] encrypted = cipher.doFinal(plaintext);
|
|
|
+
|
|
|
+ return Base64.encodeBase64String(encrypted);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ return null;
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
- public static byte[] decrypt(byte[] content, String password) {
|
|
|
+ public static String decrypt(String content, String key) throws Exception {
|
|
|
try {
|
|
|
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
|
|
|
- SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
|
|
|
- random.setSeed(password.getBytes());
|
|
|
- //kgen.init(128, random);
|
|
|
- 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;
|
|
|
- }
|
|
|
+ String data = content;
|
|
|
+ String iv = key;
|
|
|
|
|
|
- /**
|
|
|
- * 将二进制转换成16进制
|
|
|
- *
|
|
|
- * @param buf
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String parseByte2HexStr(byte buf[]) {
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
- for (int i = 0; i < buf.length; i++) {
|
|
|
- String hex = Integer.toHexString(buf[i] & 0xFF);
|
|
|
- if (hex.length() == 1) {
|
|
|
- hex = '0' + hex;
|
|
|
- }
|
|
|
- sb.append(hex.toUpperCase());
|
|
|
- }
|
|
|
- return sb.toString();
|
|
|
- }
|
|
|
+ byte[] decoded = Base64.decodeBase64(data);
|
|
|
+
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
|
|
+ SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
|
|
|
+ IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
|
|
|
+
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
|
|
|
|
|
|
- public static byte[] parseHexStr2Byte(String hexStr) {
|
|
|
- if (hexStr.length() < 1)
|
|
|
+ byte[] original = cipher.doFinal(decoded);
|
|
|
+ String originalString = new String(original);
|
|
|
+ return originalString;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
return null;
|
|
|
- byte[] result = new byte[hexStr.length() / 2];
|
|
|
- for (int i = 0; i < hexStr.length() / 2; i++) {
|
|
|
- int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
|
|
|
- int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
|
|
|
- result[i] = (byte) (high * 16 + low);
|
|
|
}
|
|
|
- return result;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- /*@Test
|
|
|
- public void test(){
|
|
|
- String s = "1";
|
|
|
- String key = "aft666";
|
|
|
- String ss = AesEncrypt(s,key);
|
|
|
- System.out.println(ss);
|
|
|
-
|
|
|
- String sss = AesDecrypt(ss, key);
|
|
|
- System.out.println(sss);
|
|
|
-
|
|
|
- System.out.println("716B715661513134686D485A34557531564C476E46513D3D".length());
|
|
|
-
|
|
|
- }*/
|
|
|
-
|
|
|
-}
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String pwd = "Ai/0sYzmwiWiE6vq";
|
|
|
+ String msg = "hahahahahahaha";
|
|
|
+ String encrypted = AesUtils.encrypt(msg, pwd);
|
|
|
+ System.out.println(encrypted);
|
|
|
+ System.out.println(encrypted.length());
|
|
|
+ System.out.println(AesUtils.decrypt(encrypted, pwd));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|