sruly
sruly

Reputation: 115

Decrypting using AES returning blank string

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.*;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.util.Arrays;

import java.util.*;

public class AES {

    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter your 16 character key here:");
        String EncryptionKey = input.next();
        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
        KeyGen.init(128);

        Cipher AesCipher = Cipher.getInstance("AES/CFB/NoPadding");
        System.out.println("Enter text to encrypt or decrypt:");
        String Text = input.next();

        System.out.println("Do you want to encrypt or decrypt (e/d)");
        String answer = input.next();
        if (answer.equalsIgnoreCase("e")) {

            byte[] byteKey = (EncryptionKey.getBytes());
            byte[] byteText = (Text).getBytes();
            SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "AES");
            AesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec);
            AesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec); // ERROR LINE
            byte[] byteCipherText = AesCipher.doFinal(byteText);

            System.out.println(byteCipherText);

        } else if (answer.equalsIgnoreCase("d")) {

            byte[] byteKey = (EncryptionKey.getBytes());
            byte[] byteText = (Text).getBytes();
            String decryptKeyString = input.nextLine();
            Charset charset = StandardCharsets.UTF_16;
            byte[] cipherText = decryptKeyString.getBytes(charset);

            SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "AES");
            AesCipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivspec); // ERROR LINE
            //byte[] bytePlainText = AesCipher.doFinal(cipherText);
            String plaintext = new String(AesCipher.doFinal(cipherText), "UTF-8");
            //Files.write(Paths.get(FileName2), bytePlainText);
            System.out.println(plaintext);
        }
    }

}

TERMINAL OUTPUT:

Enter your 16 character key here:
electricboogaloo
Enter text to encrypt or decrypt:
helloworld
Do you want to encrypt or decrypt (e/d)
e
[B@504bae78
//SECOND RUN OF PROGRAM
Enter your 16 character key here:
electricboogaloo
Enter text to encrypt or decrypt:
[B@504bae78
Do you want to encrypt or decrypt (e/d)
d

This code is largely repurposed from other code on StackExchange, I just wanted to adapt the code to not use files but to use the console instead. For some reason decryption always returns empty for me though.

If you could help me out with what specific line I have wrong that would be greatly appreciated. Thanks so much.

Upvotes: 0

Views: 322

Answers (2)

Alex R
Alex R

Reputation: 3311

[B@504bae78 is not the encrypted text but the string representation of a byte array. If you want to print the encoded text, you should convert the byte array to base64 so that it's printed using printable characters.

Then, when your programs asks you to enter the ecrypted text, enter the base64 encoded string, convert it back to a byte array and pass it to the function.

Upvotes: 1

TeWu
TeWu

Reputation: 6526

When you want to print an array in Java, you should use Arrays.toString to convert it to string first:

System.out.println(Arrays.toString(byteCipherText));

instead of:

System.out.println(byteCipherText);

See this question for more ways to print an array in Java.

Upvotes: 1

Related Questions