Cifrado y descifrado en dos programas diferentes de Java

Sunmeet Singh:

Estoy tratando de desarrollar dos programas java. Uno para el cifrado de texto y otra para desencriptar el texto cifrado.

A continuación se muestra el código:

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

}

ENTRADA PARA Encryption.java: nonu Y matriz resultado Encryption.java:? ?? ??? 8 M WFG (Ee

Pero cuando entro en la salida de encryption.java en el decryption.java, es que me da un error en lugar de devolverme el texto sin formato.

Aquí está el código 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;
    }

}

Se me está dando este 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)

¿Cómo puedo solucionar este error?

Enrique :

Hay por lo menos un problema, es decir, con esta línea:

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

Los datos binarios cifrada en general no ser válida para codificación UTF-8. Esto significa que pierden la información al convertir los bytes en una cadena.

Uso base 64 o alguna otra codificación de los datos binarios para convertir los bytes binarios en una cadena.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=188426&siteId=1
Recomendado
Clasificación