gpt4 book ai didi

java - 解密时得到 "EVP_DecryptFinal_ex:wrong final block length"

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:34 31 4
gpt4 key购买 nike

我按照本教程在 android/java 中加密和解密简单字符串:

https://stackoverflow.com/questions/4319496/how-to-encrypt-and-decrypt-data-in-java我做了一个密码学课:

public class Cryptography {

public static SecretKey generateKey() throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA");
digest.update("BhLKTyLoP YroUsRQT".getBytes());
return new SecretKeySpec(digest.digest(), 0, 16, "AES");
}

public static byte[] encrypt(String message, SecretKey key) throws NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException {
Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes.init(Cipher.ENCRYPT_MODE, key);
return aes.doFinal(message.getBytes());
}

public static String decrypt(byte[] cipherText, SecretKey key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes.init(Cipher.DECRYPT_MODE, key);
return new String(aes.doFinal(cipherText));
}

}

我能够加密方法并给了我这个:

Encrypted username: [B@52aff408 
Encrypted password: [B@52aff6d8

但是,当我使用解密时:

SecretKey secret = Cryptography.generateKey();
Log.d("encryption", "Decrypted username: " + Cryptography.decrypt(encryptedUsername.getBytes(),secret)
+ " Decrypted password: " + Cryptography.decrypt(encyptedPassword.getBytes(),secret));

它给我错误:

03-25 15:22:23.461    2073-2073/com.sblive.aufschoolbliz W/System.err﹕ java.lang.RuntimeException: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
03-25 15:22:23.461 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at org.apache.harmony.xnet.provider.jsse.NativeCrypto.EVP_CipherFinal_ex(Native Method)
03-25 15:22:23.461 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at org.apache.harmony.xnet.provider.jsse.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:398)
03-25 15:22:23.461 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at org.apache.harmony.xnet.provider.jsse.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:434)
03-25 15:22:23.461 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at javax.crypto.Cipher.doFinal(Cipher.java:1111)
03-25 15:22:23.461 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at com.sblive.utils.Cryptography.decrypt(Cryptography.java:28)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at com.sblive.aufschoolbliz.GradeBookFragment$2.onClick(GradeBookFragment.java:99)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at android.view.View.performClick(View.java:4240)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at android.view.View$PerformClick.run(View.java:17721)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
03-25 15:22:23.465 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-25 15:22:23.469 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 15:22:23.469 2073-2073/com.sblive.aufschoolbliz W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

最佳答案

Encrypted username: [B@52aff408

Encrypted password: [B@52aff6d8

这些太小了。假设纯文本消息少于 16 个字节,那么由于 PKCS 填充,这些应该正好是 16 个字节。

您在某个地方遇到了编码问题。可能是一个嵌入的空值,当被解释为字符串时,它会切掉密文的末尾......

事实上,它们看起来像是打印出来的指针....

关于java - 解密时得到 "EVP_DecryptFinal_ex:wrong final block length",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22628085/

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