Why doesn't this simple AES encryption work?

The output of the cipher is a sequence of random-looking bytes. You have no guarantee that these bytes will be a valid encoding for a character string in whatever is your system's default encoding. So this line.

The output of the cipher is a sequence of random-looking bytes. You have no guarantee that these bytes will be a valid encoding for a character string in whatever is your system's default encoding. So this line: String cipherText = new String(cipher.

DoFinal(.....)); is likely to lose information that you'll need for decryption. Therefore you will not get the right bytes reconstructed in your decrypt operation. For example, if your default encoding is UTF-8, it is overwhelmingly unlikely that the correct ciphertext is something that String.getBytes() is even able to produce.

The code posted is a particular test-case that is failing. – pst Aug 18 at 22:04 2 But the OP is using new String(...) on the encrypted bytes and attempting to reconstruct them with String.getBytes() in the decrypt operation. That has no right to work.

– Henning Makholm Aug 18 at 22:08 Agreed, however the wording in this post ... misses why it won't work. – pst Aug 18 at 22:09 What do you think is unclear? – Henning Makholm Aug 18 at 22:10 The part where it talks about UTF-8/8-bit character sets.

It just confuses, IMOHO. I think if it talked about new String(byteArr) (assume a particular encoding, even though it's system-specific) only being valid where byteArr was generated from a string for said particular encoding not an array of random bytes, would sum it up. – pst Aug 18 at 22:12.

Two things: No padding can only work if you use input that is an exact mulitple of your key size, which is 128 bit or 16 bytes. So in your particular case "".getBytes() is actually a multiple of 16, but this is of course not true for arbitrary Strings. Use "AES/CBC/PKCS5Padding" instead to solve this issue.Do not turn your encrypted data into a String - this will and change the encrypted output.

There's no guarantee that new String(byte).getBytes() returns the exact same byte array! So you should leave the encrypted data as what it is - a stream of bytes. Thus encrypt should return byte instead and decrypt should take byte as input - this is a working example: public class NewClass { public static void main(String args) { try { String plainText = ""; String encryptionKey = "E072EDF9534053A0B6C581C58FBF25CC"; System.out.

Println("Before encryption - " + plainText); byte cipherText = encrypt(plainText, encryptionKey); System.out. Println("After encryption - " + cipherText); String decrypted = decrypt(cipherText, encryptionKey); // -> System.out. Println("After decryption - " + decrypted); } catch (Exception e) { e.printStackTrace(); } } public static byte encrypt(String plainText, String passkey) throws Exception { Cipher cipher = Cipher.

GetInstance("AES/CBC/PKCS5Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(hexStringToByteArray(passkey), "AES"); cipher. Init(Cipher. ENCRYPT_MODE, key, new IvParameterSpec(new bytecipher.getBlockSize())); return cipher.

DoFinal(plainText.getBytes()); } public static String decrypt(byte cipherText, String passkey) throws Exception{ Cipher cipher = Cipher. GetInstance("AES/CBC/PKCS5Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(hexStringToByteArray(passkey), "AES"); cipher. Init(Cipher.

DECRYPT_MODE, key, new IvParameterSpec(new bytecipher.getBlockSize())); return new String(cipher. DoFinal(cipherText)); }.

You need to create the SecretKeySpec object once and use it for both encrypt and decrypt. Currently the code is creating two different keys for each operation and this will definitely lead to incorrect results.

– pst Aug 18 at 22:03 1 That shouldn't matter, because the same bytes are used to construct both SecretKeySpecs. A symmetric cipher would not be worth much if the recipient of encrypted data could not construct a correct key object from hist knowledge of the key bytes. – Henning Makholm Aug 18 at 22:04 You are correct.

I was thinking that the SecretKeySpecs object wrapped the raw key bytes with a key wrapping algo. I got my work mixed up with this code sample. – uperez Aug 19 at 0:31.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions