Сначала я сгенерировал IV, используя этот метод:
public static String generateRandomIV() {
SecureRandom ranGen = new SecureRandom();
byte[] aesKey = new byte[16];
ranGen.nextBytes(aesKey);
StringBuffer result = new StringBuffer();
for (byte b : aesKey) {
result.append(String.format("%02x", b));
}
if (16 > result.toString().length()) {
return result.toString();
} else {
return result.toString().substring(0, 16);
}
}
А затем попытался расшифровать строку, которая была зашифрована с использованием IV, сгенерированного вышеуказанным методом:
private String decrypt(String _inputText, String _encryptionKey,
String _initVector) throws Exception {
String _out = "";
int len = _encryptionKey.getBytes("UTF-8").length;
if (_encryptionKey.getBytes("UTF-8").length >= _key.length) {
len = _key.length;
int ivlen = _initVector.getBytes("UTF-8").length;
if (_initVector.getBytes("UTF-8").length > _iv.length)
ivlen = _iv.length;
System.arraycopy(_encryptionKey.getBytes("UTF-8"), 0, _key, 0, len);
System.arraycopy(_initVector.getBytes("UTF-8"), 0, _iv, 0, ivlen);
SecretKeySpec keySpec = new SecretKeySpec(_key, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(_iv);
//Decryption starts
_cx.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decodeValue = Base64.decode(_inputText.getBytes(), Base64.DEFAULT);
Приходит следующее исключениев последней строке приведенного выше фрагмента кода:
java.lang.RuntimeException: java.security.InvalidAlgorithmParameterException: IV must be specified in CBC mode
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at com.android.org.conscrypt.OpenSSLCipher.engineInit(OpenSSLCipher.java:265)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2977)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at javax.crypto.Cipher.tryCombinations(Cipher.java:2884)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2789)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at javax.crypto.Cipher.chooseProvider(Cipher.java:956)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at javax.crypto.Cipher.init(Cipher.java:1199)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at javax.crypto.Cipher.init(Cipher.java:1143)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at oversight.controller.network.CryptLib.encryptDecrypt(CryptLib.java:135)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at oversight.controller.network.CryptLib.decrypt(CryptLib.java:174)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at oversight.controller.network.RestAPIs.decrypt(RestAPIs.java:89)
2018-05-12 00:36:20.984 2637-2637/com.emerson.oversight W/System.err: at oversight.controller.Oversight.onCreate(Oversight.java:54)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1032)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5876)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at android.app.ActivityThread.-wrap3(ActivityThread.java)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at android.os.Looper.loop(Looper.java:154)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6682)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: Caused by: java.security.InvalidAlgorithmParameterException: IV must be specified in CBC mode
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.engineInitInternal(OpenSSLCipher.java:498)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: at com.android.org.conscrypt.OpenSSLCipher.engineInit(OpenSSLCipher.java:262)
2018-05-12 00:36:20.985 2637-2637/com.emerson.oversight W/System.err: ... 20 more