gpt4 book ai didi

android - 例程 :EVP_DecryptFinal_ex:wrong final block length in android

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:41:29 25 4
gpt4 key购买 nike

实际上我在一个应用程序中工作并且在解密 AES 时遇到问题

我在这一行 byte[] results = cipher.doFinal(Base64.encode(text.getBytes("UTF-8"), Base64.DEFAULT)) 中有错误

错误 : error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

String key = "grnR++15jgNFVmVg61UvQA==";
String data = "vrvwe+9wVhyNJmb/KcLD2K2j9gqkwVFXq0jt0qL7zyHHG2JWpkvr+VpG/QHm6INW01WsBZcpRYYk25bMtjWgLY6dzFNs3WLXZRMnVtIDhjuIfv0sp2RKvhjNLEu3Gscz4sdxYYS6XcLOWAY705Ll48+mA55Ke5WSq71jVR+TCJVT0w2nYZvzviE+N0QwRX5EZjCMjbaH4gpFLr+LsNevkAQ7e7Id4oSvrwqJ3PyUS4mo8OvPcUjKDhQCJc39k2aqBBaZ9O1AuqQqCOWpgy2XW9kacAP8zrcDRO7oNtSbIM0sqIyKS4PGzXiL+hw43hs33d/VO8tvLGHWOE0UvWkQY0QPJqzRDmmWpRTgr0Xt56UxIXjjAmteVlb+TWo3kzRSUQ2Un+ScCfEBHjaNJNShE27zSeXOjkMWpB0jobSMJy6KdR/fqopHmYcWd41DNOz25nkUtQBmBK+x5sqM0dLswL8TKfb7MdIdhqdZaHt7h1CTNpozEwg+A7ZEkcGZ3hcGAwPWkPrg1yoXE5uaeCzdslf4knbXBxIx8ekrBiyUY5BhMPak/7LJm9D64RPQEpdOUTeg2fh9nKShXnr1OdKGrf68H7c/rSincB2uqgEuo9oHaBIIwm0RuUkM/jCjeLmVEE2zIjJE2osk2XQKf4iOlHP12XQOCtITYBZm8jc0OKmjpelFaWNLFheAE/txRk/NSS+qmUcWor1BSXDZAxje0ftwTl0jYz69U3tW9pDm3yooWWU51ORoTpQHqEGLumQyJom3OfsSkQ/T/pEMxY6B3a7TmJ+8u+QJla+ZCHFav5aKL1Ojy7xijYzIlsoVP9m7nDp30oVA8rpI8vYKpUHXUuQPxdVV4/yjvCeRkWT/veQtHpA9OWYDSTQLdRYfOOeQoxg/kGua4HU0RlC8IVgm/iJJnpWJgvdKD0KKmVgwFKmZ1TFg5yMRN4oOPDk4yhtnjPV9VhJU4lHztHw7TG53UWblwieeorD+v94LHySXFAj1tyd4tebgrvFqyuPovT4iP7Xm37KA/LmtrCPiCaBn6g==;

try {
Decrypt(data,key);
} catch (Exception e) {
e.printStackTrace();
}

方法

String Decrypt(String text, String key) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] keyBytes = Base64.decode(key.getBytes(), Base64.DEFAULT);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
byte[] b = new byte[keySpec.getEncoded().length];
System.arraycopy(keySpec.getEncoded(), 0, b, 0, b.length);
IvParameterSpec ivSpec = new IvParameterSpec(b);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] results = cipher.doFinal(Base64.encode(text.getBytes("UTF-8"), Base64.DEFAULT));
String decoded = new String(cipher.doFinal(results), "UTF-8");
return decoded;
}

最佳答案

我重新开始了解您的解密例程如何工作。看看下面的例子。我对您将在代码中看到的 keydata 字段做了一些假设。了解这些字段的来源及其格式很重要。此代码已经过测试,可用作演示。

评论应该有助于引导您完成。

至于您看到的错误,这表明您的加密字符串长度不正确。因为它似乎被填充了,所以它应该是 AES block 大小的 16 字节的倍数(实际上不是)。您提供的字符串缺少终止引号,因此无论如何都无法编译。很难说加密字符串到底发生了什么。

未正确设置初始化向量。 iv 独立于 key ,在加密过程中单独导出,在解密过程中是“给定的”。看看下面例程中初始化向量是如何设置的。

希望对您有所帮助。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
final String SECRET_KEY = "PloasdlOoasdllasiewjsaroo9o55ooo"; // 256 bits
// encryptedString is encrypted form of the string "This is just some test data." It has
// the initialization vector added as a prefix.
final String encryptedString = "yDdtrKCl30b+fndIMkhasDhBNk+OGYqiM6uVVF89pu2WSnjMsz1lnw6x4H23QWxP";
String decryptedString;

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
decryptedString = doDecryption(encryptedString, SECRET_KEY);
} catch (Exception e) {
decryptedString = "Error doing decryption!";
e.printStackTrace();
}
Log.d(TAG, ">>Decrypted string=" + decryptedString);
}

/**
* Decrypt from Base 64 representation of a string with initialization vector prefix.
*
* @param sourceBase64 Initialization vector prefixed to string to decrypt in base 64.
* @param secretKey The secret key used to do the encryption and the decryption.
* @return The decrypted string.
* @throws Exception Exception
*/

String doDecryption(String sourceBase64, String secretKey) throws Exception {
Cipher cipher; // The cipher used to encrypt and decrypt
int cipherBlockSize; // Size of the cipher block
byte[] sourceBytes; // Decoded byte array from sourceBase64
byte[] iv; // Initialization vector
byte[] bytesToDecrypt; // Bytes on which to perform decryption

// String passed in is in base 64. Translate so we can work with it.
sourceBytes = Base64.decode(sourceBase64.getBytes("UTF-8"), Base64.DEFAULT);

// Get the secretKey spec for our secret key to work with our cipher.
SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "AES");

// Set up the cipher.
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherBlockSize = cipher.getBlockSize();

// The initialization vector prefixes the string that was passed in (sourceBase64).
// The length of this initialization vector is the same as our cipher's blocksize.
iv = new byte[cipherBlockSize];

// Split the inititializatoin vector from the bytes we need to decrypt.
bytesToDecrypt = new byte[sourceBytes.length - cipherBlockSize];
System.arraycopy(sourceBytes, 0, iv, 0, iv.length);
System.arraycopy(sourceBytes, iv.length, bytesToDecrypt, 0, bytesToDecrypt.length);

// Now do the actual decryption.
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
return new String(cipher.doFinal(bytesToDecrypt));
}

private static final String TAG = "MainActivity";
}

关于android - 例程 :EVP_DecryptFinal_ex:wrong final block length in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43267874/

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