| 79 | * 3.将加密内容解密 |
| 80 | */ |
| 81 | public static String AESDncode(final String encodeRules, String content) { |
| 82 | try { |
| 83 | // 1.构造密钥生成器,指定为AES算法,不区分大小写 |
| 84 | KeyGenerator keygen = KeyGenerator.getInstance("AES"); |
| 85 | // 2.根据ecnodeRules规则初始化密钥生成器 |
| 86 | // 生成一个128位的随机源,根据传入的字节数组 |
| 87 | SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); |
| 88 | random.setSeed(encodeRules.getBytes()); |
| 89 | keygen.init(128, random); |
| 90 | // 3.产生原始对称密钥 |
| 91 | SecretKey original_key = keygen.generateKey(); |
| 92 | // 4.获得原始对称密钥的字节数组 |
| 93 | byte[] raw = original_key.getEncoded(); |
| 94 | // 5.根据字节数组生成AES密钥 |
| 95 | SecretKey key = new SecretKeySpec(raw, "AES"); |
| 96 | // 6.根据指定算法AES自成密码器 |
| 97 | Cipher cipher = Cipher.getInstance("AES"); |
| 98 | // 7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY |
| 99 | cipher.init(Cipher.DECRYPT_MODE, key); |
| 100 | // 8.将加密并编码后的内容解码成字节数组 |
| 101 | byte[] byte_content = Base64Ext.decode(content.getBytes(), Base64Ext.NO_WRAP); |
| 102 | /* |
| 103 | * 解密 |
| 104 | */ |
| 105 | byte[] byte_decode = cipher.doFinal(byte_content); |
| 106 | return new String(byte_decode, Opslab.UTF_8); |
| 107 | } catch (NoSuchAlgorithmException e) { |
| 108 | e.printStackTrace(); |
| 109 | } catch (NoSuchPaddingException e) { |
| 110 | e.printStackTrace(); |
| 111 | } catch (InvalidKeyException e) { |
| 112 | e.printStackTrace(); |
| 113 | } catch (IOException e) { |
| 114 | e.printStackTrace(); |
| 115 | } catch (IllegalBlockSizeException e) { |
| 116 | e.printStackTrace(); |
| 117 | } catch (BadPaddingException e) { |
| 118 | e.printStackTrace(); |
| 119 | } |
| 120 | // 如果有错就返加nulll |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * 加密字符串 |