二つの異なるJavaプログラムでは、暗号化と復号化

Sunmeetシン:

私は2つの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 FOR INPUT:Encryption.java FROM NONUと出力リレー:?? ??? 8 M WFG(EE?

私はdecryption.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)

どのように私はこのエラーを解決することができますか?

ヘンリー:

つまり、このラインを有する少なくとも一つの問題は、あります:

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

暗号化されたバイナリデータは、一般的に有効なUTF-8エンコーディングではありません。これは、あなた緩い情報の文字列にバイトを変換します。

使用BASE64または文字列にバイナリバイトに変換するバイナリデータのためのいくつかの他の符号化。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=188423&siteId=1
おすすめ