What to pass in cipher.doFinal in Android/Java?

后端 未结 2 805
遥遥无期
遥遥无期 2021-01-26 17:53

Android code

String apiResponse = \"EcUZvMif

Method:

protected void decryptDataWithAES(String apiResponse,         


        
相关标签:
2条回答
  • 2021-01-26 18:28

    You are trying to decrypt the "key", I think you need to decrypt the apiResponse

    Also you need the exact same IV the message was encrypted with, otherwise you won't be able to decrypt

    0 讨论(0)
  • 2021-01-26 18:36

    Here is a static method to decrypt using AES with secretKey

    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;
    }
    

    You can also use my library to encrypt/decrypt string using AES

    0 讨论(0)
提交回复
热议问题