0

Estoy trabajando utilizando el algoritmo de DES, el problema que tengo es que al imprimir y mostrar el mensaje encriptado en la interfaz, no me muestra los caracteres especiales. (como por ejemplo: Ûcµ˜ž'M¬ãa€î )solo me muestra los espacios como cuadros. introducir la descripción de la imagen aquí

El código que tengo para imprimir el texto es:

private void btnEncriptarActionPerformed(java.awt.event.ActionEvent evt) {                                             

    sv.edu.ufg.LogicaDes.textoPlano texto = new sv.edu.ufg.LogicaDes.textoPlano();
    sv.edu.ufg.LogicaDes.Encriptado cifrado = new sv.edu.ufg.LogicaDes.Encriptado();

    String nombreTxt, msj, clave;

    nombreTxt = txtNombreArchivo.getText();
    msj = txtMensaje.getText();
    clave = txtClave.getText();

    texto.escribir(nombreTxt, msj);//SE CREA EL NOMBRE DEL ARCHIVO Y EL MENSAJE
    cifrado.imprimir(nombreTxt, clave);//SE MANDA EL NOMBRE DEL ARCHIVO PARA ENCONTRARLO Y PASAR ENCRIPTAR
    //SE PASA LA CLAVE PARA CONCATENAR EL CIFRADO Y HACERLO MAS SEGURO

    File archivo = new File("C:\\Users\\Ana\\Desktop\\algoritmoDes\\archivos textos\\Encriptados\\" + nombreTxt + "_encript" + ".txt");

    try {
        BufferedReader leer = new BufferedReader(new FileReader(archivo) );
        String mensaje = leer.readLine();
        while (mensaje != null) {
            jtaEncriptar.append(mensaje);
            mensaje = leer.readLine();
        }

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Chingados ahora el error es en encriptar" + ex);
    }

el código de encriptacion es:

public void imprimir(String nombre, String clave)
{
    File plaintext = new File("C:\\Users\\Ana\\Desktop\\algoritmoDes\\archivos textos\\ARCHIVOS\\"+nombre+".txt");
    File encrypted = new File("C:\\Users\\Ana\\Desktop\\algoritmoDes\\archivos textos\\Encriptados\\"+nombre+"_encript"+".txt");
    try {
        encryptDecrypt(clave, Cipher.ENCRYPT_MODE, plaintext,encrypted);
    } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | IOException e) {
        e.printStackTrace();
    }
}
public void encryptDecrypt(String key, int cipherMode, File in, File out)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IOException {
    FileInputStream fis = new FileInputStream(in);
    FileOutputStream fos = new FileOutputStream(out);

    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = skf.generateSecret(desKeySpec);

    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

    if (cipherMode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        write(cis, fos);
    } else if (cipherMode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        CipherOutputStream cos = new CipherOutputStream(fos, cipher);
        write(fis, cos);
    }
}

private static void write(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[64];
    int numOfBytesRead;
    while ((numOfBytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, numOfBytesRead);
    }
    out.close();
    in.close();
}
2
  • hola acabo de pasar por acá y creo que puede ser problema de la fuente que estás usando para tu text area el 14 may. 2023 a las 6:35
  • normalmente ese error es como te mencionan arriba, problema de fuente. si usas una fuente personalizada revisa que soporte caracteres especiales el 12 sep. 2023 a las 0:14

1 respuesta 1

0

Tendrías que cambiar el typescript de netbeans. haz click derecho sobre el que deseas modificar en el listado de proyectos y, en la pestaña de fuentes (o sources) aparece el selector de codificaciones. prueba primero con utf-8.

1

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.