I am trying to use IV with AES so that the ciphertext is unpredictable. However, the encrypted hexadecimal string is always the same.
I actually tried several ways to try to add some randomness by passing some additional parameters for calling init init:
1) IV generation manual
byte[] iv = generateIv(); IvParameterSpec ivspec = new IvParameterSpec(iv);
2) Encryption request for generation IV
AlgorithmParameters params = cipher.getParameters(); params.getParameterSpec(IvParameterSpec.class);
3) Using PBEParameterSpec
byte[] encryptionSalt = generateSalt(); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);
All of them do not seem to affect the ciphertext .... help !!!
My code is:
package com.citc.testencryption; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class Main extends Activity { public static final int SALT_LENGTH = 20; public static final int PBE_ITERATION_COUNT = 1000; private static final String RANDOM_ALGORITHM = "SHA1PRNG"; private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC"; private static final String CIPHER_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC"; private static final String TAG = Main.class.getSimpleName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { String password = "password"; String plainText = "plaintext message to be encrypted";
source share