Skip to content
Advertisement

cannot decrypt String from text file [closed]

In my journey to mastering Java, I started doing some AES encryption and decryption, but ran into some problems. I can encrypt and decrypt just fine using my 2 methods below.

public static byte[] encryptString(String datatoEncrypt, String myKey, Cipher cipher)
{
    try
    {
        byte[] text = datatoEncrypt.getBytes(UNICODE_FORMAT);
        byte[] decodedKey = Base64.getDecoder().decode(myKey);
        SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); 
        cipher.init(Cipher.ENCRYPT_MODE, originalKey);
        byte[] textEncrpted = cipher.doFinal(text);
        
        return textEncrpted;
    }
    catch(Exception e)
    {
        return null;
    }
}

and the decrypt method

public static String decryptString(byte[] datatoDecrypt, String myKey, Cipher cipher)
{
    try
    {
        System.out.println("Key " + myKey);
        byte[] decodedKey = Base64.getDecoder().decode(myKey);
        SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); 
        cipher.init(Cipher.DECRYPT_MODE, originalKey);
        byte[] textDecrpted = cipher.doFinal(datatoDecrypt);
        String result = new String(textDecrpted, StandardCharsets.UTF_8);
        
        return result;
    }
    catch(Exception e)
    {
        return null;
    }
}

My problem is when I encrypt, I am saving the encrypted file to a text file, and now I am trying to decrypt that text file, but it won’t decrypt.

public static void task1()
{
    System.out.println("Key in file path name: ");        
    String filename = keyboard.nextLine();
    
    try
    {
        Scanner inputFile = new Scanner(new File(filename));
        
        String message = readFile(filename);
        String key = generateKey();
        Cipher chipher;
        chipher = Cipher.getInstance("AES");
        System.out.println("Key is " +key);
        byte[] encryptedData = encryptString(message, key, chipher);
        String encryptedString = new String(encryptedData);
        System.out.println("Encrypted data is: " + encryptedString);
        PrintWriter writer = new PrintWriter("ciphertext.txt");
        writer.println(encryptedString);
        writer.close();
        
        byte[] tryagain;
        System.out.println("enter File name");
        String filename1 = keyboard.nextLine();
        
        tryagain = Files.readAllBytes(Paths.get(filename1));
        
        System.out.println("Enter Key");
        String keyString = keyboard.nextLine();
        String decrypted = decryptString(tryagain,keyString,chipher);
        System.out.println("Decrypted data: "  + decrypted);
    }
    catch(Exception e)
    {
        System.out.println("Error " );
    }
}

It keeps giving me back null, I believe the problem is the text file as when not using text files it runs just fine. Below is some code the operated normally when not using text files.

String text = "Santosisverycool";
String key = generateKey();
Cipher chipher;
chipher = Cipher.getInstance("AES");
System.out.println("Key is " +key);
byte[] encryptedData = encryptString(text, key, chipher);
String encryptedString = new String(encryptedData);
System.out.println("Encrypted data is: " + encryptedString);

String decrypted = decryptString(encryptedData,key,chipher);
System.out.println("decrypted: " +decrypted);

Does anyone have any solutions? I have been stuck on this problem for at least a week and haven’t gotten any closer to solving it.

Advertisement

Answer

writer.println(encryptedString);

println prints the string, plus a newline after the string. You don’t want a new line, just the string. Replace it with:

writer.print(encryptedString);

As pointed out in the comments, the byte array will most likely contain unprintable characters, making the resulting data corrupt. Instead:

just skip the string conversion and write the byte array directly to a file with FileOutputStream:

try (FileOutputStream fos = new FileOutputStream("ciphertext.txt")) {
    fos.write(encryptedData);
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement