Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt and decrypt a string with AES-128

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();
    }
  }
}
like image 402
Yodism Avatar asked Dec 11 '22 01:12

Yodism


2 Answers

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");
like image 142
Artjom B. Avatar answered Dec 14 '22 05:12

Artjom B.


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);
}
like image 41
Jerry C. Avatar answered Dec 14 '22 03:12

Jerry C.