簡體   English   中英

在 Android/Java 中傳遞 cipher.doFinal 什么?

[英]What to pass in cipher.doFinal in Android/Java?

安卓代碼

String apiResponse = "EcUZvMif

方法:

protected void decryptDataWithAES(String apiResponse, String key) {
        try {
            es(StandardCharsets.UTF_8);


            byte[] decodedResult = Base64.decode(apiResponse, Base64.NO_WRAP);

           terSpec = new IvParameterSpec(first16ByteArray);

            SecretKeySpec skey = new SecretKeySpec(byteArray, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(DECRYPT_MODE, skey, ivParameterSpec);

            String decryptString = new String(cipher.doFinal(byteArray), StandardCharsets.UTF_8);
            showLog("JSON: " + decryptString);

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

異常: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT

[wefopwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwefpwfpkpewfpkoewfkowf ewfwefwef] bhdfuiyh

您正在嘗試解密“密鑰”,我認為您需要解密 apiResponse

您還需要與消息加密時使用的完全相同的 IV,否則您將無法解密

這是使用帶有secretKey的AES解密的靜態方法

private final static String AES_PADDING = "AES/ECB/PKCS5PADDING"; //this need to be same as DECRYPTION 
private String secretKey = "Your secret key"; //your secret key

//DecryptString
@SuppressLint("GetInstance")
public static String AESDecryptionString(String encryptedStringData) {
    Cipher decipher = null;
    byte[] encryptedString = encryptedStringData.getBytes(StandardCharsets.ISO_8859_1);
    String returnData = encryptedStringData;
    try {
        decipher = Cipher.getInstance(AES_PADDING);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        e.printStackTrace();
    }
    byte[] decryption;
    try {
        assert decipher != null;
        decipher.init(Cipher.DECRYPT_MODE, secretKey);
        decryption = decipher.doFinal(encryptedString);
        returnData = new String(decryption);
    } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }
    return returnData;
}

您還可以使用我的使用 AES 加密/解密字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM