求助:JAVA加密的數據PHP解密
問題描述
java 加密后的密文:iP6nVfCE9Eiw47utJDR6wV6Lhl4xyOgYykub0HUJSZQ=以下是 java 的 aes 加密類,希望能用 php 解密出來
package com.mk.api.util;import java.security.Key;import java.security.MessageDigest;import java.util.Arrays;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import javax.xml.bind.DatatypeConverter;/** * AES 加密 */public class AES { private static final String AESTYPE = "AES"; private static final String KEY = "Z8LSq0wWwB5v+6YJzurcP463H3F12iZh74fDj4S74oUH4EONkiKb2FmiWUbtFh97GG/c/lbDE47mvw6j94yXxKHOpoqu6zpLKMKPcOoSppcVWb2q34qENBJkudXUh4MWcreondLmLL2UyydtFKuU9Sa5VgY/CzGaVGJABK2ZR94="; /** * 加密 * @param content * @return */ public static String encrypt(String str) { try { Key key = generateKey(); Cipher c = Cipher.getInstance(AESTYPE); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(str.getBytes()); String encryptedValue = DatatypeConverter.printBase64Binary(encVal); return encryptedValue; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 解密 * @param content * @return */ public static String decrypt(String str) { try { Key key = generateKey(); Cipher c = Cipher.getInstance(AESTYPE); c.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = DatatypeConverter.parseBase64Binary(str); byte[] decValue = c.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; } catch (Exception e) { e.printStackTrace(); } return null; } private static Key generateKey() throws Exception { byte[] keyValue = KEY.getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-1"); keyValue = sha.digest(keyValue); keyValue = Arrays.copyOf(keyValue, 16); Key key = new SecretKeySpec(keyValue, AESTYPE); return key; } }
問題解答
回答1:看看這個:https://www.php.cn/php-weizijiaocheng-455991.html
回答2:package com.mk.api.util;import java.security.Key;import java.security.MessageDigest;import java.util.Arrays;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import javax.xml.bind.DatatypeConverter;/** * AES 加密 */public class AES { private static final String AESTYPE = "AES"; private static final String KEY = "Z8LSq0wWwB5v+6YJzurcP463H3F12iZh74fDj4S74oUH4EONkiKb2FmiWUbtFh97GG/c/lbDE47mvw6j94yXxKHOpoqu6zpLKMKPcOoSppcVWb2q34qENBJkudXUh4MWcreondLmLL2UyydtFKuU9Sa5VgY/CzGaVGJABK2ZR94="; /** * 加密 * @param content * @return */ public static String encrypt(String str) { try { Key key = generateKey(); Cipher c = Cipher.getInstance(AESTYPE); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(str.getBytes()); String encryptedValue = DatatypeConverter.printBase64Binary(encVal); return encryptedValue; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 解密 * @param content * @return */ public static String decrypt(String str) { try { Key key = generateKey(); Cipher c = Cipher.getInstance(AESTYPE); c.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = DatatypeConverter.parseBase64Binary(str); byte[] decValue = c.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; } catch (Exception e) { e.printStackTrace(); } return null; } private static Key generateKey() throws Exception { byte[] keyValue = KEY.getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-1"); keyValue = sha.digest(keyValue); keyValue = Arrays.copyOf(keyValue, 16); Key key = new SecretKeySpec(keyValue, AESTYPE); return key; } }
上面代碼格式亂了
相關文章:
1. macos - 無法source activate python272. 小白學python的問題 關于%d和%s的區別3. mysql - 在不允許改動數據表的情況下,如何優化以varchar格式存儲的時間的比較?4. 運行python程序時出現“應用程序發生異常”的內存錯誤?5. android - 如何實現QQ pad 點擊右側輸入框,只頂右側的布局,左側布局不動6. css3:flex排版問題7. css - 關于background-position百分比的問題?8. 問題Unknown column ’’ in ’where clause’9. html5 - chrome上的video控制條不同10. github - 求助大神啊,win10 git clone error,折騰了幾天都不行,以前原本好好的,突然就這樣了

網公網安備