1

I encrypted a text file in AES algorithm. I am not able to decrypt it. I used the same key and the whole process is running in the same method body. At first, the input.txt file is being encrypted into encrypted.txt file. Then the decoder, decrypt the encrypted.txt into decrypted.txt Here is the code. Thank you for your help.

public static void main(String[] args) throws IOException,
        NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException {

    Scanner sc = new Scanner(System.in);
    String filename = sc.nextLine();
    sc.close();

    System.out.println("The file requested is " + filename);

    File file = new File(filename);

    if (file.exists())
        System.out.println("File found");

    File to_b_encf = new File("encrypted.txt");

    if (!to_b_encf.exists())
        to_b_encf.createNewFile();

    System.out.println("encrypting");

    Cipher encipher = Cipher.getInstance("AES");
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    SecretKey key = keygen.generateKey();

    encipher.init(Cipher.ENCRYPT_MODE, key);

    FileOutputStream output = new FileOutputStream(to_b_encf);
    FileInputStream input = new FileInputStream(filename);
    CipherInputStream cis = new CipherInputStream(input, encipher);

    int read;

    while ((read = cis.read()) != -1) {
        output.write(read);
        output.flush();
    }

    input.close();
    output.close();

    System.out.println("done");
    System.out.println("decrypting");

    Cipher decipher = Cipher.getInstance("AES");//initiate a cipher for decryption
    decipher.init(Cipher.DECRYPT_MODE, key);//decrypt the file 

    File sourcefile = new File("encrypted.txt");
    File destfile = new File("decrypted.txt");

    if (!destfile.exists())
        destfile.createNewFile();

    FileInputStream decf = new FileInputStream(sourcefile);
    CipherInputStream c_decf = new CipherInputStream(decf,decipher);
    FileOutputStream destf = new FileOutputStream(destfile);

    cout = new CipherOutputStream(destf,decipher);

    while ((read = c_decf.read()) != -1) {
        cout.write(read);
        cout.flush();
    }

    c_decf.close();
    destf.close();
    cout.close();
    decf.close();
    System.out.println("done");
}

2 Answers 2

2

You messed with InputStream, OutputStream and whatnot. I made a simplfied version of your code (no files, all in-memory I/O) that illustrates the main concepts:

public class EncDec {

    public static void main(String[] args) throws IOException
            , InvalidKeyException, NoSuchAlgorithmException
            , NoSuchPaddingException {

        final String MESSAGE = "I'm a secret message";
        final Charset CHARSET = Charset.defaultCharset();

        Cipher cipher = Cipher.getInstance("AES");
        SecretKey key = KeyGenerator.getInstance("AES").generateKey();
        cipher.init(Cipher.ENCRYPT_MODE, key);

        // Encrypt the message
        InputStream plainIn = new ByteArrayInputStream(
                MESSAGE.getBytes(CHARSET));
        ByteArrayOutputStream encryptedOut = new ByteArrayOutputStream();
        copy(plainIn, new CipherOutputStream(encryptedOut, cipher));

        // Decrypt the message
        cipher.init(Cipher.DECRYPT_MODE, key);
        InputStream encryptedIn = new CipherInputStream(
                new ByteArrayInputStream(encryptedOut.toByteArray()), cipher);
        ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
        copy(encryptedIn, plainOut);

        System.out.println(new String(plainOut.toByteArray(), CHARSET));
    }

    private static void copy(InputStream in, OutputStream out)
            throws IOException {
        byte[] buffer = new byte[4096];
        while ( in.read(buffer) > -1) {
            out.write(buffer);
        }
        out.flush();
    }
}

The Java I/O API is inspired by the decorator pattern. Encryption/decription libraries provide a decorator CipherInputStream for reading encrypted content and a decorator CipherOutputStream to encrypt a plain source and write it to the decorated output destination.

Sign up to request clarification or add additional context in comments.

Comments

0
CipherInputStream c_decf = new CipherInputStream(decf,decipher);
FileOutputStream destf = new FileOutputStream(destfile);

cout = new CipherOutputStream(destf,decipher);

while ((read = c_decf.read()) != -1) {
    cout.write(read);
    cout.flush();
}

It looks like you're deciphering it twice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.