在解密过程中获取“EVP_DecryptFinal_ex:错误的最终块长度”。

I followed this tutorial for encrypting and decrypting simple strings in android/java:

我跟随本教程,对android/java中的简单字符串进行了加密和解密:

https://stackoverflow.com/questions/4319496/how-to-encrypt-and-decrypt-data-in-java I made a Cryptography class:

我做了一个加密类:

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

}

I was able to encrypt method and gave me this:

我可以加密方法给我这个:

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

However, when I use decrypt:

然而,当我使用解密:

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

It gives me the error:

它给了我错误:

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)

1 个解决方案

#1


3  

Encrypted username: [B@52aff408

加密用户名:[B@52aff408

Encrypted password: [B@52aff6d8

加密的密码:[B@52aff6d8

These are too small. Assuming the plain text message was fewer than 16 bytes, then these should be exactly 16 bytes because of PKCS padding.

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

You have an encoding problem somewhere. Probably an embedded null that slices off the end of the cipher text when interpreted as a string...

你在某个地方有一个编码问题。可能是一个嵌入的null,当被解释为字符串时,将密码文本的末端切掉。

As a matter of fact, they look like pointers being printed....

事实上,它们看起来像指针被打印出来了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
EVP_aes_256_cbc是OpenSSL库提供的一种加密算法,可以使用C++进行实现。下面是一个简单的示例代码: ```c++ #include <openssl/evp.h> int aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; /* 创建并初始化加密上下文 */ if(!(ctx = EVP_CIPHER_CTX_new())) return -1; /* 初始化加密操作,设置加密算法、密钥和IV */ if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) return -1; /* 执行加密操作 */ if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) return -1; ciphertext_len = len; /* 结束加密操作 */ if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) return -1; ciphertext_len += len; /* 释放加密上下文 */ EVP_CIPHER_CTX_free(ctx); return ciphertext_len; } int aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) { EVP_CIPHER_CTX *ctx; int len; int plaintext_len; /* 创建并初始化解密上下文 */ if(!(ctx = EVP_CIPHER_CTX_new())) return -1; /* 初始化解密操作,设置解密算法、密钥和IV */ if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) return -1; /* 执行解密操作 */ if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) return -1; plaintext_len = len; /* 结束解密操作 */ if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) return -1; plaintext_len += len; /* 释放解密上下文 */ EVP_CIPHER_CTX_free(ctx); return plaintext_len; } ``` 使用示例: ```c++ unsigned char plaintext[] = "Hello, world!"; unsigned char key[] = "0123456789abcdef0123456789abcdef"; unsigned char iv[] = "0123456789abcdef"; unsigned char ciphertext[128]; unsigned char decryptedtext[128]; int decryptedtext_len, ciphertext_len; /* 加密操作 */ ciphertext_len = aes_encrypt(plaintext, strlen((char *)plaintext), key, iv, ciphertext); printf("Ciphertext is:\n"); BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len); /* 解密操作 */ decryptedtext_len = aes_decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext); decryptedtext[decryptedtext_len] = '\0'; printf("Decrypted text is:\n%s\n", decryptedtext); ``` 注意,这里的key和iv长度都为32字节,可以根据需要修改。同时,由于EVP_aes_256_cbc使用的是对称加密算法,因此在实际使用需要保证key和iv的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是你的春哥!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值