两种不同的Java程序中的加密和解密

时间:2018-10-29 15:09:25

标签: java encryption

我正在尝试开发两个Java程序。一个用于加密纯文本,另一个用于解密该加密的文本。

下面是我的代码:

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;
    }

}
  

用于Encryption.java的输入:nonu和   来自Encryption.java的输出:8 ??? M?wFg(Ee

但是,当我在解密.java中输入encryption.java的输出时,它给了我一些错误,而不是给我回了纯文本。

这是Decryption.java代码

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;
    }

}

这给我这个错误

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)

如何解决此错误?

1 个答案:

答案 0 :(得分:4)

至少有一个问题,即此行:

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

通常,加密的二进制数据将不是有效的UTF-8编码。这意味着在将字节转换为字符串时,您失去了信息。

对二进制数据使用base64或其他某种编码将二进制字节转换为字符串。

相关问题