印度支付


JAVA AES加解密示例

<p>import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; public static void main(String[] args) { String key = &quot;A3E8A4C522C8609F1166CCEB8728B9685EEDD70976F5FC8F48F570E777095963&quot;; // hex to bytes byte[] key_bytes = parseHexStr2Byte(key); String text = &quot;hello world&quot;; byte[] encryptStr = encrypt1(text.getBytes(), key_bytes); byte[] encode = java.util.Base64.getEncoder().encode(encryptStr); System.out.println(new String(encode)); // decryption String str = &quot;7qB4cawg7K6WwecZyR/4wA==&quot;; byte[] decode = java.util.Base64.getDecoder().decode(str); byte[] bytes1 = decrypt1(decode, key_bytes); System.out.println(new String(bytes1)); } public static byte[] decrypt1(byte[] data, byte[] key) { try { SecretKeySpec secretKey = new SecretKeySpec(key, &quot;AES&quot;); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, &quot;AES&quot;); // create cipher Cipher cipher = Cipher.getInstance(&quot;AES/ECB/PKCS5Padding&quot;);<br /> cipher.init(Cipher.DECRYPT_MODE, seckey);<br /> byte[] result = cipher.doFinal(data); return result;<br /> } catch (Exception e) { throw new RuntimeException(&quot;decrypt fail!&quot;, e); } } public static byte[] encrypt1(byte[] data, byte[] key) { try { SecretKeySpec secretKey = new SecretKeySpec(key, &quot;AES&quot;); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, &quot;AES&quot;); Cipher cipher = Cipher.getInstance(&quot;AES/ECB/PKCS5Padding&quot;);<br /> cipher.init(Cipher.ENCRYPT_MODE, seckey);<br /> byte[] result = cipher.doFinal(data); return result;<br /> } catch (Exception e) { throw new RuntimeException(&quot;encrypt fail!&quot;, e); } } // 二进制转十六进制 public static String parseByte2HexStr(byte buf[]) { StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; buf.length; i++) { String hex = Integer.toHexString(buf[i] &amp; 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } // 十六进制转二进制 public static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() &lt; 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i &lt; hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i <em> 2, i </em> 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i <em> 2 + 1, i </em> 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; }</p>

页面列表

ITEM_HTML