Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3633130
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:36:50+00:00 2026-05-19T00:36:50+00:00

I am trying to use an IV with AES so that the encrypted text

  • 0

I am trying to use an IV with AES so that the encrypted text is unpredictable. However, the encrypted hex string is always the same.

I have actually tried a few methods of attempting to add some randomness by passing some additional parameters to the cipher init call:

1) Manual IV generation

byte[] iv = generateIv();
IvParameterSpec ivspec = new IvParameterSpec(iv);

2) Asking cipher to generate IV

AlgorithmParameters params = cipher.getParameters();
params.getParameterSpec(IvParameterSpec.class);

3) Using a PBEParameterSpec

byte[] encryptionSalt = generateSalt();
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);

All of these seem to have no influence on the encrypted text…. help!!!

My code:

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";

   // byte[] salt = generateSalt();
   byte[] salt = "dfghjklpoiuytgftgyhj".getBytes();
   Log.i(TAG, "Salt: " + salt.length + " " + HexEncoder.toHex(salt));
   PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT);
   SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBE_ALGORITHM);
   SecretKey secretKey = keyFac.generateSecret(pbeKeySpec);
   byte[] key = secretKey.getEncoded();
   Log.i(TAG, "Key: " + HexEncoder.toHex(key));

   // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);

   Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);

   // byte[] encryptionSalt = generateSalt();
   // Log.i(TAG, "Encrypted Salt: " + encryptionSalt.length + " " + HexEncoder.toHex(encryptionSalt));
   // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);
   // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
   // Log.i(TAG, encryptionCipher.getParameters() + " ");
   byte[] iv = generateIv();
   IvParameterSpec ivspec = new IvParameterSpec(iv);

   encryptionCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
   byte[] encryptedText = encryptionCipher.doFinal(plainText.getBytes());
   Log.i(TAG, "Encrypted: " + HexEncoder.toHex(encryptedText)); // <== Why is this always the same :(

   Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
   decryptionCipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
   byte[] decryptedText = decryptionCipher.doFinal(encryptedText);
   Log.i(TAG, "Decrypted: " + new String(decryptedText));

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 private byte[] generateSalt() throws NoSuchAlgorithmException {
  SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
  byte[] salt = new byte[SALT_LENGTH];
  random.nextBytes(salt);
  return salt;
 }

 private byte[] generateIv() throws NoSuchAlgorithmException {
  SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
  byte[] iv = new byte[16];
  random.nextBytes(iv);
  return iv;
 }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:36:51+00:00Added an answer on May 19, 2026 at 12:36 am

    I changed the way I was generating the secret key… fixed now.

    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 javax.crypto.spec.SecretKeySpec;
    
    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 = "AES/CBC/PKCS5Padding";
    
        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";
    
                // byte[] salt = generateSalt();
                byte[] salt = "dfghjklpoiuytgftgyhj".getBytes();
                Log.i(TAG, "Salt: " + salt.length + " " + HexEncoder.toHex(salt));
                PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256);
                SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM);
                SecretKey tmp = factory.generateSecret(pbeKeySpec);
                SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
                byte[] key = secret.getEncoded();
                Log.i(TAG, "Key: " + HexEncoder.toHex(key));
    
                // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
    
                Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
    
                // byte[] encryptionSalt = generateSalt();
                // Log.i(TAG, "Encrypted Salt: " + encryptionSalt.length + " " + HexEncoder.toHex(encryptionSalt));
                // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(encryptionSalt, 1000);
                // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
                Log.i(TAG, encryptionCipher.getParameters() + " ");
                byte[] iv = generateIv();
                IvParameterSpec ivspec = new IvParameterSpec(iv);
    
                encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
                byte[] encryptedText = encryptionCipher.doFinal(plainText.getBytes());
                Log.i(TAG, "Encrypted: " + HexEncoder.toHex(encryptedText));
    
                Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);
                decryptionCipher.init(Cipher.DECRYPT_MODE, secret, ivspec);
                byte[] decryptedText = decryptionCipher.doFinal(encryptedText);
                Log.i(TAG, "Decrypted: " + new String(decryptedText));
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        private byte[] generateSalt() throws NoSuchAlgorithmException {
            SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
            byte[] salt = new byte[SALT_LENGTH];
            random.nextBytes(salt);
            return salt;
        }
    
        private byte[] generateIv() throws NoSuchAlgorithmException {
            SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
            byte[] iv = new byte[16];
            random.nextBytes(iv);
            return iv;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a BlackBerry app that uses AES encryption. I am trying to
I have been trying use the edit_post_link() function to contain an image. All of
I am trying to create a plot that is annotated with text that contains
I am trying to use larger block size for the AES Encryption: private static
I am trying to use the following snippet to decrypt file, which was encrypted
I am trying use the jQuery table sorter plugin for a table that is
I am trying to use openssl to encode a simple string, no need of
I am trying use neo4j with Ruby on rails on Mac OSX. I have
I am trying use the mysql connector in c++ in ubuntu. It appears that
I'm trying to use a JavaScript library to encrypt data and send that to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.