gpt4 book ai didi

java - Android加密 "pad block corrupted"异常

转载 作者:太空宇宙 更新时间:2023-11-04 13:33:36 24 4
gpt4 key购买 nike

在此代码中,此行导致异常:

clearText = c.doFinal(Base64.decode(encryptedText, Base64.DEFAULT));

javax.crypto.BadPaddingException:填充 block 损坏

我从以下位置获得了代码: http://www.techrepublic.com/blog/software-engineer/attention-android-developers-keep-user-data-safe/

有什么想法吗?

    private String decrypt (String encryptedText) {
byte[] clearText = null;
try {
SecretKeySpec ks = new SecretKeySpec(getKey(), "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, ks);
clearText = c.doFinal(Base64.decode(encryptedText, Base64.DEFAULT));
return new String(clearText, "UTF-8");
} catch (Exception e) {
return null;
}
}

详细信息:我也在 Android 上对其进行加密

最佳答案

owlstead 的建议很有帮助,但对于本例,使用中的代码时

Android 开发者注意:确保用户数据安全 http://www.techrepublic.com/blog/software-engineer/attention-android-developers-keep-user-data-safe/

我对代码进行了一些更改,这可能会对将来的其他人有所帮助。我完全删除了getkey方法。

private static String seed;

/**
* Encrypts the text.
* @param clearText The text you want to encrypt
* @return Encrypted data if successful, or null if unsucessful
*/
protected String encrypt(String clearText) {
byte[] encryptedText = null;
try {
byte[] keyData = seed.getBytes();
SecretKey ks = new SecretKeySpec(keyData, "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, ks);
encryptedText = c.doFinal(clearText.getBytes("UTF-8"));
return Base64.encodeToString(encryptedText, Base64.DEFAULT);
} catch (Exception e) {
return null;
}
}

/**
* Decrypts the text
* @param encryptedText The text you want to encrypt
* @return Decrypted data if successful, or null if unsucessful
*/
protected String decrypt (String encryptedText) {
byte[] clearText = null;
try {
byte[] keyData = seed.getBytes();
SecretKey ks = new SecretKeySpec(keyData, "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, ks);
clearText = c.doFinal(Base64.decode(encryptedText, Base64.DEFAULT));
return new String(clearText, "UTF-8");
} catch (Exception e) {
return null;
}
}

关于java - Android加密 "pad block corrupted"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com