Skip to main content
fixed list formatting
Source Link
Freiheit
  • 8.8k
  • 6
  • 63
  • 109

Every time the encryption values changed by using AES, let anyone investigate the below code and let me know the issue

code:

private static final String secretKeys = "58BA833E57A51CBF9BF8BAB696BF9" public static String encrypt() throws Exception { byte[] salt = new byte[16]; SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); PBEKeySpec pbeKeySpec = new PBEKeySpec(secretKeys.getChars(),salt,1000, 256); Key secretKey = factory.generateSecret(pbeKeySpec); byte[] key = new byte[32]; byte[] iv = new byte[16]; SecretKeySpec secret = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); byte[] result = cipher.doFinal("welcome".getBytes("UTF-8")); String s = Base64.getEncoder().encodeToString(result); return s } 

Output first time I got the below-encrypted string

CZRIP35M4CnJtuDQ6YpmaQ== 

The second time I got the below-encrypted string

/fylTjohAZDsnCaHhiZo3A== 

Let me know

1.why the encrypted string not a constant?

2.how can I set the Blocksize? ( AES.BlockSize = 128;)have three questions:

  1. How can I set the padding mode? (AES.Padding = PaddingMode.PKCS7;)

I'm not familiar with AES algorithm, please help

  1. why the encrypted string not a constant?

  2. how can I set the Blocksize? ( AES.BlockSize = 128;)

  3. How can I set the padding mode? (AES.Padding = PaddingMode.PKCS7;)

Every time the encryption values changed by using AES, let anyone investigate the below code and let me know the issue

code:

private static final String secretKeys = "58BA833E57A51CBF9BF8BAB696BF9" public static String encrypt() throws Exception { byte[] salt = new byte[16]; SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); PBEKeySpec pbeKeySpec = new PBEKeySpec(secretKeys.getChars(),salt,1000, 256); Key secretKey = factory.generateSecret(pbeKeySpec); byte[] key = new byte[32]; byte[] iv = new byte[16]; SecretKeySpec secret = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); byte[] result = cipher.doFinal("welcome".getBytes("UTF-8")); String s = Base64.getEncoder().encodeToString(result); return s } 

Output first time I got the below-encrypted string

CZRIP35M4CnJtuDQ6YpmaQ== 

The second time I got the below-encrypted string

/fylTjohAZDsnCaHhiZo3A== 

Let me know

1.why the encrypted string not a constant?

2.how can I set the Blocksize? ( AES.BlockSize = 128;)

  1. How can I set the padding mode? (AES.Padding = PaddingMode.PKCS7;)

I'm not familiar with AES algorithm, please help

Every time the encryption values changed by using AES, let anyone investigate the below code and let me know the issue

code:

private static final String secretKeys = "58BA833E57A51CBF9BF8BAB696BF9" public static String encrypt() throws Exception { byte[] salt = new byte[16]; SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); PBEKeySpec pbeKeySpec = new PBEKeySpec(secretKeys.getChars(),salt,1000, 256); Key secretKey = factory.generateSecret(pbeKeySpec); byte[] key = new byte[32]; byte[] iv = new byte[16]; SecretKeySpec secret = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); byte[] result = cipher.doFinal("welcome".getBytes("UTF-8")); String s = Base64.getEncoder().encodeToString(result); return s } 

Output first time I got the below-encrypted string

CZRIP35M4CnJtuDQ6YpmaQ== 

The second time I got the below-encrypted string

/fylTjohAZDsnCaHhiZo3A== 

I have three questions:

  1. why the encrypted string not a constant?

  2. how can I set the Blocksize? ( AES.BlockSize = 128;)

  3. How can I set the padding mode? (AES.Padding = PaddingMode.PKCS7;)

Source Link
Prabu
  • 3.8k
  • 9
  • 54
  • 92

Java AES encryption issue

Every time the encryption values changed by using AES, let anyone investigate the below code and let me know the issue

code:

private static final String secretKeys = "58BA833E57A51CBF9BF8BAB696BF9" public static String encrypt() throws Exception { byte[] salt = new byte[16]; SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); PBEKeySpec pbeKeySpec = new PBEKeySpec(secretKeys.getChars(),salt,1000, 256); Key secretKey = factory.generateSecret(pbeKeySpec); byte[] key = new byte[32]; byte[] iv = new byte[16]; SecretKeySpec secret = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); byte[] result = cipher.doFinal("welcome".getBytes("UTF-8")); String s = Base64.getEncoder().encodeToString(result); return s } 

Output first time I got the below-encrypted string

CZRIP35M4CnJtuDQ6YpmaQ== 

The second time I got the below-encrypted string

/fylTjohAZDsnCaHhiZo3A== 

Let me know

1.why the encrypted string not a constant?

2.how can I set the Blocksize? ( AES.BlockSize = 128;)

  1. How can I set the padding mode? (AES.Padding = PaddingMode.PKCS7;)

I'm not familiar with AES algorithm, please help