Sunmeet Singh
Sunmeet Singh

Reputation: 47

Encryption And Decryption in Two different Java Programs

I am trying to develop two java programs. One for encrypting plain text and another to decrypt that encrypted text.

Below is my code:

Encryption.java

public class Encryption {

    private static Cipher cipher = null;

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

        String keyText = "9ofAGtArndXw9Ffu3lRTGWy9svXuUBl8";
        byte[] keyBytes = keyText.getBytes("UTF-8");

        SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
        cipher = Cipher.getInstance("AES");

        System.out.println("Enter the plain text to be encrypted: ");
        String plainText = scan.nextLine();

        byte[] plainTextByte = plainText.getBytes("UTF-8");
        byte[] encryptedBytes = encrypt(plainTextByte, secretKey);

        String encryptedText = new String(encryptedBytes, "UTF-8");
        System.out.println("Encrypted Text After Encryption: " + encryptedText);
    }

    static byte[] encrypt(byte[] plainTextByte, SecretKey secretKey) throws Exception {
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(plainTextByte);
        return encryptedBytes;
    }

}

INPUT FOR Encryption.java : nonu AND Ouput FROM Encryption.java : ??8???M?wFg(Ee

But when I enter the output from encryption.java in the decryption.java, it is giving me some error rather than giving me back the plain text.

Here is the Decryption.java code

public class Decryption {

    private static Cipher cipher = null;

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

        String keyText = "9ofAGtArndXw9Ffu3lRTGWy9svXuUBl8";
        byte[] keyBytes = keyText.getBytes("UTF-8");

        SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
        cipher = Cipher.getInstance("AES");

        System.out.println("Enter the encrypted text to be decrypted: ");
        String encryptedText = scan.nextLine();
        byte[] encryptedBytes = encryptedText.getBytes("UTF-8");
        byte[] decryptedBytes = decrypt(encryptedBytes, secretKey);
        String decryptedText = new String(decryptedBytes, "UTF-8");

        System.out.println("Plain Text is: " + decryptedText);
    }

    static byte[] decrypt(byte[] encryptedBytes, SecretKey secretKey)
        throws Exception {
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return decryptedBytes;
    }

}

It is giving me this error

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
    at javax.crypto.Cipher.doFinal(Cipher.java:2164)
    at encryption.Decryption.decrypt(Decryption.java:36)
    at encryption.Decryption.main(Decryption.java:27)

How can I solve this error?

Upvotes: 4

Views: 789

Answers (1)

Henry
Henry

Reputation: 43798

There is at least one problem, namely with this line:

String encryptedText = new String(encryptedBytes, "UTF-8");

The encrypted binary data will in general not be a valid UTF-8 encoding. This means you loose information when converting the bytes to a string.

Use base64 or some other encoding for binary data to convert the binary bytes to a string.

Upvotes: 4

Related Questions