4

I got it in encrypting 128-bit key. What can I do to reverse the process. I'm almost sitting here almost an hour to figure this but i cant. I'm new to this btw.

The sample input is:J§???????ÿK♥?{↕?

The output should be: hello

In this program:

The sample input is:hello

The output is: J§???????ÿK♥?{↕?...

public class en {
    public static void main(String[] args){
      ...
    try{
      System.out.print("Enter text: ");
        String text = dataIn.readLine();
        String key = "dAtAbAsE98765432"; // 128 bit key

     // Create key and cipher
     Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
     Cipher cipher = Cipher.getInstance("AES");

     // encrypt the text
     cipher.init(Cipher.ENCRYPT_MODE, aesKey);
     byte[] encrypted = cipher.doFinal(text.getBytes());
     System.err.println("Encrypted: " + new String(encrypted));

     // Decrypt the text
     cipher.init(Cipher.DECRYPT_MODE, aesKey);
     String decrypted = new String(cipher.doFinal(encrypted));
     System.err.println("Decrypted: " + decrypted);
    }catch(Exception e){
      e.printStackTrace();
    }
  }
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Yodism
  • 227
  • 1
  • 5
  • 17
  • 1
    As far as I can understand the code, the input and the output should be the same – Pablo Lozano Jan 19 '15 at 13:34
  • In my programs output I tried to get encrypted and Decrypted text. My input is a string. What I want to become possible is to Decrypt the symbols instead of getting the string as an output. Accepting symbols as an input to get its equivalent text. – Yodism Jan 19 '15 at 13:37
  • 2
    If the encrypted String contains characters which are not included in the font you are using to show them you will see a question mark `?` symbol. – SubOptimal Jan 19 '15 at 13:42
  • I don't understand. Your code works. The only thing is that you should fully specify your scheme: `Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");` depending on your default provider. – Artjom B. Jan 19 '15 at 13:50
  • It works but i want to try to reverse the input. Instead of encrypting it will decrypt the symbols. – Yodism Jan 19 '15 at 13:51
  • 1
    If you really want to output your encrypted data on screen to type it in, you need some additional encoding like base64 which allows you to recover the exact bytes. Simply outputting your byte array as string won't work because you loose some information. – Drunix Jan 19 '15 at 13:58

2 Answers2

8

Ciphertext is composed of bytes and is supposed to look random. You will get unprintable characters which you also cannot type in. You have to use an encoding like Base64 to print your ciphertext after encryption and type in before decryption.

During the encryption (Java 8):

cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.err.println("Encrypted: " + new String(Base64.getEncoder().encodeToString(encrypted)));

During the decryption (Java 8):

System.out.print("Enter ciphertext: ");
byte[] encrypted = Base64.getDecoder().decode(dataIn.readLine());
...
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.err.println("Decrypted: " + decrypted);

Encoding reference


Apart from that you really should fully specify your scheme, because it might behave differently on another Java implementation.

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
7

For some that is still having trouble to decrypt what you have encrypted, you have to decode it using Base 64. You can use apache commons codec dependency. Here is a working code copied from http://javapointers.com/tutorial/how-to-encrypt-and-decrypt-using-aes-in-java/

public class EncryptDecrypt {

private static final String SECRET_KEY_1 = "ssdkF$HUy2A#D%kd";
private static final String SECRET_KEY_2 = "weJiSEvR5yAC5ftB";

private IvParameterSpec ivParameterSpec;
private SecretKeySpec secretKeySpec;
private Cipher cipher;

public EncryptDecrypt() throws UnsupportedEncodingException, NoSuchPaddingException, NoSuchAlgorithmException {
    ivParameterSpec = new IvParameterSpec(SECRET_KEY_1.getBytes("UTF-8"));
    secretKeySpec = new SecretKeySpec(SECRET_KEY_2.getBytes("UTF-8"), "AES");
    cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
}


public String encrypt(String toBeEncrypt) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(toBeEncrypt.getBytes());
    return Base64.encodeBase64String(encrypted);
}

public String decrypt(String encrypted) throws InvalidAlgorithmParameterException, InvalidKeyException,
        BadPaddingException, IllegalBlockSizeException {
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
    byte[] decryptedBytes = cipher.doFinal(Base64.decodeBase64(encrypted));
    return new String(decryptedBytes);
}
Jerry C.
  • 121
  • 1
  • 5