gpt4 book ai didi

java - 密码获取错误::EVP_DecryptFinal_ex:解密错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:13:32 26 4
gpt4 key购买 nike

我有以下代码用于用 Java 解密我的数据,但它给了我“Bad Decrypt”错误。谁能帮我解决这个问题?

     String input = "5H5h8acnv2gzv2PeTVb+pw==";
String key = "thisismykey___2011_1234567898765";
byte[] output = null;
try{

SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
output = cipher.doFinal(Base64.decode(input, Base64.DEFAULT));
Log.i("word is: ", new String(output));
}catch(Exception e){

Log.d("myapp", "Error decrypting data", e); // This is the line
}

这是我用来加密数据的 PHP 代码:

function getEncrypt($sStr, $sKey) {
return base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$sKey,
$sStr,
MCRYPT_MODE_ECB
)
);
}

function getDecrypt($sStr, $sKey) {
return mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$sKey,
base64_decode($sStr),
MCRYPT_MODE_ECB
);
}
$key = "thisismykey___2011_1234567898765";
getEncrypt("Ekram",$key); // After encryption i get this "5H5h8acnv2gzv2PeTVb+pw=="

获取以下异常:

01-03 10:26:36.930: I/The Text is(911): error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

复制代码

********* 堆栈跟踪 ************

01-03 12:06:53.740: D/myapp(902): Error decrypting data
01-03 12:06:53.740: D/myapp(902): javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
01-03 12:06:53.740: D/myapp(902): at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
01-03 12:06:53.740: D/myapp(902): at com.android.org.conscrypt.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:420)
01-03 12:06:53.740: D/myapp(902): at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:456)
01-03 12:06:53.740: D/myapp(902): at javax.crypto.Cipher.doFinal(Cipher.java:1204)
01-03 12:06:53.740: D/myapp(902): at com.example.android.fragments.MainActivity.EncryptDecryptTest(MainActivity.java:529)
01-03 12:06:53.740: D/myapp(902): at com.example.android.fragments.MainActivity$5.onTouch(MainActivity.java:285)
01-03 12:06:53.740: D/myapp(902): at android.view.View.dispatchTouchEvent(View.java:7701)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
01-03 12:06:53.740: D/myapp(902): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
01-03 12:06:53.740: D/myapp(902): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
01-03 12:06:53.740: D/myapp(902): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
01-03 12:06:53.740: D/myapp(902): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
01-03 12:06:53.740: D/myapp(902): at android.view.View.dispatchPointerEvent(View.java:7886)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
01-03 12:06:53.740: D/myapp(902): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
01-03 12:06:53.740: D/myapp(902): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
01-03 12:06:53.740: D/myapp(902): at android.os.MessageQueue.nativePollOnce(Native Method)
01-03 12:06:53.740: D/myapp(902): at android.os.MessageQueue.next(MessageQueue.java:138)
01-03 12:06:53.740: D/myapp(902): at android.os.Looper.loop(Looper.java:123)
01-03 12:06:53.740: D/myapp(902): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-03 12:06:53.740: D/myapp(902): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 12:06:53.740: D/myapp(902): at java.lang.reflect.Method.invoke(Method.java:515)
01-03 12:06:53.740: D/myapp(902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-03 12:06:53.740: D/myapp(902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-03 12:06:53.740: D/myapp(902): at dalvik.system.NativeStart.main(Native Method)
01-03 12:06:53.860: D/dalvikvm(902): GC_FOR_ALLOC freed 762K, 9% free 8545K/9372K, paused 32ms, total 36ms

最佳答案

尝试检查您传递给 cipher.doFinal()

的数据格式

我有同样的问题,但结果是:

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

应该改为

Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

异常消失了。我确实比较了字符串在 PHP 中的加密方式,并尝试在 Java 中对其进行解密。

关于java - 密码获取错误::EVP_DecryptFinal_ex:解密错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20902854/

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