|
@@ -1,7 +1,5 @@
|
|
|
package com.its.utils;
|
|
|
|
|
|
-import org.apache.commons.codec.binary.Base64;
|
|
|
-
|
|
|
import javax.crypto.BadPaddingException;
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
@@ -9,10 +7,7 @@ import javax.crypto.NoSuchPaddingException;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
-import java.security.InvalidAlgorithmParameterException;
|
|
|
-import java.security.InvalidKeyException;
|
|
|
-import java.security.Key;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.*;
|
|
|
|
|
|
public class AES256Util {
|
|
|
private String iv;
|
|
@@ -26,36 +21,59 @@ public class AES256Util {
|
|
|
byte[] keyBytes = new byte[16];
|
|
|
byte[] b = key.getBytes("UTF-8");
|
|
|
int len = b.length;
|
|
|
- if(len > keyBytes.length)
|
|
|
+ if (len > keyBytes.length) {
|
|
|
len = keyBytes.length;
|
|
|
+ }
|
|
|
System.arraycopy(b, 0, keyBytes, 0, len);
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
|
|
|
-
|
|
|
this.keySpec = keySpec;
|
|
|
}
|
|
|
-
|
|
|
- public String aesEncode(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
|
|
|
- NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
|
|
|
- IllegalBlockSizeException, BadPaddingException{
|
|
|
+
|
|
|
+// public String aesEncode2(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
|
|
|
+// NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
|
|
|
+// IllegalBlockSizeException, BadPaddingException{
|
|
|
+// Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
+// c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));
|
|
|
+// byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
|
|
|
+// return new String(Base64.encodeBase64(encrypted));
|
|
|
+// }
|
|
|
+ public byte[] aesEncode(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
|
|
|
+ NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
|
|
|
+ IllegalBlockSizeException, BadPaddingException{
|
|
|
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));
|
|
|
-
|
|
|
byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
|
|
|
- String enStr = new String(Base64.encodeBase64(encrypted));
|
|
|
-
|
|
|
- return enStr;
|
|
|
+ return encrypted;
|
|
|
}
|
|
|
-
|
|
|
- //��ȣȭ
|
|
|
- public String aesDecode(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
|
|
|
- NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
|
|
|
- IllegalBlockSizeException, BadPaddingException {
|
|
|
- Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
- c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
|
|
|
-
|
|
|
- byte[] byteStr = Base64.decodeBase64(str.getBytes());
|
|
|
-
|
|
|
- return new String(c.doFinal(byteStr),"UTF-8");
|
|
|
+
|
|
|
+// public String aesDecode(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
|
|
|
+// NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
|
|
|
+// IllegalBlockSizeException, BadPaddingException {
|
|
|
+// Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
+// c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
|
|
|
+// byte[] byteStr = Base64.decodeBase64(str.getBytes());
|
|
|
+// return new String(c.doFinal(byteStr),"UTF-8");
|
|
|
+// }
|
|
|
+
|
|
|
+ public static String encryptSHA256(String str) {
|
|
|
+ String SHA = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ MessageDigest msgDigest = MessageDigest.getInstance("SHA-256");
|
|
|
+ msgDigest.update(str.getBytes());
|
|
|
+ byte byteData[] = msgDigest.digest();
|
|
|
+ StringBuffer builder = new StringBuffer();
|
|
|
+
|
|
|
+ for (byte b : byteData) {
|
|
|
+ builder.append(String.format("%02X", b));
|
|
|
+ }
|
|
|
+// for(int i=0; i < byteData.length; i++) {
|
|
|
+// builder.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
|
|
|
+// }
|
|
|
+ SHA = builder.toString();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ SHA = "";
|
|
|
+ }
|
|
|
+ return SHA;
|
|
|
}
|
|
|
-
|
|
|
}
|