| | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * mybatis数据库的AES_ENCRYPT(加密)和AES_DECRYPT(解密) |
| | |
| | | * @param data 需要解密的数据 |
| | | * @param aesKey 秘钥 |
| | | * @param encoding 编码,不填默认UTF-8 |
| | | * @return |
| | | * @throws Exception |
| | | * @return 解密数据 |
| | | */ |
| | | public static String decrpt(String data,String aesKey,String encoding) throws Exception { |
| | | if(StringUtils.isEmpty(encoding)){ |
| | | encoding = "UTF-8"; |
| | | public static String decryptData(String data,String aesKey,String encoding) { |
| | | try{ |
| | | if(data == null){ |
| | | return null; |
| | | } |
| | | if(StringUtils.isEmpty(encoding)){ |
| | | encoding = "UTF-8"; |
| | | } |
| | | // Decrypt |
| | | final Cipher decryptCipher = Cipher.getInstance("AES"); |
| | | decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); |
| | | data = new String(decryptCipher.doFinal(Hex.decodeHex(data.toCharArray()))); |
| | | }catch (Exception e){ |
| | | throw new RuntimeException(e); |
| | | } |
| | | // Decrypt |
| | | final Cipher decryptCipher = Cipher.getInstance("AES"); |
| | | decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); |
| | | return new String(decryptCipher.doFinal(Hex.decodeHex(data.toCharArray()))); |
| | | return data; |
| | | } |
| | | |
| | | /**AES加密 |
| | |
| | | * @param aesKey 秘钥 |
| | | * @param encoding 编码,不填默认UTF-8 |
| | | * @return 返回大写加密的数据 |
| | | * @throws Exception |
| | | */ |
| | | public static String encrpt(String data,String aesKey,String encoding) throws Exception { |
| | | if(StringUtils.isEmpty(encoding)){ |
| | | encoding = "UTF-8"; |
| | | public static String encryptData(String data,String aesKey,String encoding) { |
| | | try { |
| | | if(data == null){ |
| | | return null; |
| | | } |
| | | if (StringUtils.isEmpty(encoding)) { |
| | | encoding = "UTF-8"; |
| | | } |
| | | // Encrypt |
| | | final Cipher encryptCipher = Cipher.getInstance("AES"); |
| | | encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); |
| | | char[] code = Hex.encodeHex(encryptCipher.doFinal(data.getBytes(encoding))); |
| | | StringBuilder builder = new StringBuilder(); |
| | | for (char d : code) { |
| | | builder.append(d); |
| | | } |
| | | data = builder.toString().toUpperCase(); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | // Encrypt |
| | | final Cipher encryptCipher = Cipher.getInstance("AES"); |
| | | encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); |
| | | char[] code = Hex.encodeHex(encryptCipher.doFinal(data.getBytes(encoding))); |
| | | StringBuilder builder = new StringBuilder(); |
| | | for (char d : code) { |
| | | builder.append(d); |
| | | } |
| | | String strning = builder.toString(); |
| | | return strning.toUpperCase(); |
| | | |
| | | return data; |
| | | } |
| | | |
| | | /** |
| | | * 判断是不是十六进制的字符串(字母是大写的) |
| | | * @param str 需要判断的字符串 |
| | | * @return true or false |
| | | */ |
| | | public static boolean isHexStrValid(String str) { |
| | | if(str == null){ |
| | | return false; |
| | | } |
| | | String pattern = "^[0-9A-F]+$"; |
| | | return Pattern.compile(pattern).matcher(str).matches(); |
| | | } |
| | | |
| | | |
| | | |
| | | } |