J. Baron
J. Baron

Reputation: 25

Cipher Java not encrypting correctly

I am currently playing with Cipher to create a solution using a key that is always the same. I know this is not the most secure solution but it is what I have been asked to do. I am supposed to use AES256 and EBC, but I can not encrypt correctly. The problem is that I've got unknown characters.

private static String encrypt(String text) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException
{
    String keyString = AESEncryption.convertToUTF8("8DJE7K01U8B51807B3E17D21");
    text = AESEncryption.convertToUTF8(text);

    byte[]keyValue = Base64.getEncoder().encode(keyString.getBytes(StandardCharsets.UTF_8));
    Key key = new SecretKeySpec(keyValue, "AES");
    Cipher c1 = Cipher.getInstance("AES/ECB/PKCS5Padding");

    c1.init(Cipher.ENCRYPT_MODE, key);

    byte[] encodedText =Base64.getEncoder().encode(text.getBytes(StandardCharsets.UTF_8));
    System.out.println("Encoded text: "+new String(encodedText,StandardCharsets.UTF_8));

    byte[] encVal = c1.doFinal(encodedText);
    System.out.println("Encoded val: "+new String(encVal,StandardCharsets.UTF_8));

    return new String(encVal);
}

Edit: Sorry first time asking. I will give you the full scope. Afterwards I try to decrypt with the following code(I know that I have repeated code, I will clean it) But when I decrypt the output obtained by the encrypt method I recieve the following error. The message I am trying to encrypt and decrypt is "Hola"

    public static String desEncrypt(String text) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException 
{
    String keyString = AESEncryption.convertToUTF8("8DJE7K01U8B51807B3E17D21");

    byte[] keyValue = Base64.getEncoder().encode(keyString.getBytes(StandardCharsets.UTF_8));
    Key key = new SecretKeySpec(keyValue, "AES");
    Cipher c1 = Cipher.getInstance("AES/ECB/PKCS5Padding");

    c1.init(Cipher.DECRYPT_MODE, key);
    byte[]   encodedText = Base64.getDecoder().decode(text.getBytes(StandardCharsets.UTF_8));
    byte[] encVal = c1.doFinal(encodedText);

    System.out.println(new String(encodedText));
    return new String(encVal,StandardCharsets.UTF_8);

}

And the error:

Encoded text: aG9sYWNraXNqbWRlaXJncw==
Encoded val: ???D>??|??i9???Fd?\Zz?A?-
Exception in thread "main" java.lang.IllegalArgumentException: Illegal base64 character -3d
      at java.util.Base64$Decoder.decode0(Unknown Source)
      at java.util.Base64$Decoder.decode(Unknown Source)
      at AESEncryption.desEncrypt(AESEncryption.java:63)
      at AESEncryption.main(AESEncryption.java:79)

Thank you very much, and forgive for not providing all the info needed

Upvotes: 0

Views: 550

Answers (1)

JB Nizet
JB Nizet

Reputation: 691625

Your code makes no sense: converting a String to UTF8 and getting back a String makes no sense: a String contains characters. Not bytes.

Encoding a key to base64 doesn't make much sense either. Encoding the plain text to base64 is useless, too.

You need base64 encoding when you have random, binary bytes, and you want to transform them to printable english characters. Only then.

So the process should be:

  1. transform your key to bytes (using String.getBytes(UTF_8)). Unless the String key is in fact a base64-encoded byte array, in which case you need to base64 decode it;
  2. transform the plain text to bytes (using String.getBytes(UTF_8));
  3. encrypt the bytes you got from step 2, using the key obtained from step 1. You obtain completely "random" bytes. These bytes don't represent characters encoded in your platform default charset. So transforming them to a String using new String(bytes) doesn't make any sense, and is a lossy transformation.
  4. Just return the result as a byte array, or if you really want printable characters, base64-encode the bytes, and return the string you obtain from this base64 encoding.

To decrypt, use the reverse process:

  1. Use the same thing as in step 1 above to get the key
  2. take the bytes obtained from step4 above (if you chose to return a byte array), or base64-decode the string, to obtain the original random binary bytes
  3. decrypt the bytes obtained from step 2, using the key obtained from step 1. You get a byte array representing the UTF8-encoded characters of the original plain text.
  4. Use new String(decryptedBytes, UTF_8) to transform this byte array to a String.

Upvotes: 1

Related Questions