|
@@ -1,7 +1,6 @@
|
|
package cn.com.qmth.examcloud.commons.util;
|
|
package cn.com.qmth.examcloud.commons.util;
|
|
|
|
|
|
import java.security.GeneralSecurityException;
|
|
import java.security.GeneralSecurityException;
|
|
-import java.util.Locale;
|
|
|
|
|
|
|
|
import javax.crypto.BadPaddingException;
|
|
import javax.crypto.BadPaddingException;
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.Cipher;
|
|
@@ -10,6 +9,8 @@ import javax.crypto.SecretKey;
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
|
|
|
+import org.apache.commons.codec.DecoderException;
|
|
|
|
+import org.apache.commons.codec.binary.Hex;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
@@ -77,7 +78,7 @@ public class AES {
|
|
public String encrypt(byte[] bytes) {
|
|
public String encrypt(byte[] bytes) {
|
|
try {
|
|
try {
|
|
byte[] enc = encryptCipher.doFinal(bytes);
|
|
byte[] enc = encryptCipher.doFinal(bytes);
|
|
- return bytes2HexString(enc);
|
|
|
|
|
|
+ return Hex.encodeHexString(enc);
|
|
} catch (IllegalBlockSizeException e) {
|
|
} catch (IllegalBlockSizeException e) {
|
|
throw new ExamCloudRuntimeException(e);
|
|
throw new ExamCloudRuntimeException(e);
|
|
} catch (BadPaddingException e) {
|
|
} catch (BadPaddingException e) {
|
|
@@ -103,12 +104,14 @@ public class AES {
|
|
*/
|
|
*/
|
|
public String decrypt(String str) {
|
|
public String decrypt(String str) {
|
|
try {
|
|
try {
|
|
- byte[] dec = decryptCipher.doFinal(hexString2Bytes(str));
|
|
|
|
|
|
+ byte[] dec = decryptCipher.doFinal(Hex.decodeHex(str));
|
|
return new String(dec);
|
|
return new String(dec);
|
|
} catch (IllegalBlockSizeException e) {
|
|
} catch (IllegalBlockSizeException e) {
|
|
throw new ExamCloudRuntimeException(e);
|
|
throw new ExamCloudRuntimeException(e);
|
|
} catch (BadPaddingException e) {
|
|
} catch (BadPaddingException e) {
|
|
throw new ExamCloudRuntimeException(e);
|
|
throw new ExamCloudRuntimeException(e);
|
|
|
|
+ } catch (DecoderException e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -125,42 +128,4 @@ public class AES {
|
|
return new SecretKeySpec(target, KEY_ALGORITHM);
|
|
return new SecretKeySpec(target, KEY_ALGORITHM);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param hexString
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- private byte[] hexString2Bytes(String hexString) {
|
|
|
|
- hexString = hexString.toUpperCase(Locale.US);
|
|
|
|
- int length = hexString.length() / 2;
|
|
|
|
- char[] hexChars = hexString.toCharArray();
|
|
|
|
- byte[] bytes = new byte[length];
|
|
|
|
-
|
|
|
|
- String str = "0123456789ABCDEF";
|
|
|
|
-
|
|
|
|
- for (int i = 0; i < length; i++) {
|
|
|
|
- int pos = i * 2;
|
|
|
|
- bytes[i] = (byte) (str.indexOf(hexChars[pos]) << 4 | str.indexOf(hexChars[pos + 1]));
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- return bytes;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @param bytes
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- private String bytes2HexString(byte[] bytes) {
|
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
- for (int i = 0; i < bytes.length; i++) {
|
|
|
|
- String hex = Integer.toHexString(bytes[i] & 0xFF);
|
|
|
|
- if (hex.length() == 1) {
|
|
|
|
- hex = '0' + hex;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- sb.append(hex);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return sb.toString();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|