实际上我在应用程序中工作并且在解密AES时遇到问题
我在这行UserDefaults.standard.bool(forKey: "adsPurchased")
错误: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();
}
答案 0 :(得分:3)
我重新开始了解你的解密例程。看看下面的例子。我对您在代码中看到的key
和data
字段做了一些假设。了解这些字段的来源及其格式非常重要。此代码已经过测试,可用作演示。
评论应该会帮助您完成。
至于您看到的错误,它表示您的加密字符串不是正确的长度。由于它似乎是填充的,它应该是16字节的倍数(它不是),这是AES块大小。您提供的字符串缺少终止引号,因此无论如何都不会编译。很难说加密字符串到底发生了什么。
初始化向量未正确设置。 iv与密钥无关,在加密期间单独导出,在解密期间是“给定的”。看看如何在以下例程中设置初始化向量。
我希望这会有所帮助。
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";
}
答案 1 :(得分:1)
我认为问题在于这一行:
byte[] results = cipher.doFinal(Base64.encode(text.getBytes("UTF-8"), Base64.DEFAULT));
看起来你是base64编码的东西已经是base64编码的。我认为你必须为下一个代码改变它:
String decoded = new String(cipher.doFinal(Base64Coder.decode(text)), "UTF-8");