dimanche 8 mai 2016

Simple Java Encryption/Decryption

This code adds the number 100 after each character.

import java.io.*;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileEncryptionAndDecryption {
    public static void main(String[] args)
    throws IOException {
        String inputFileName = "DemoFile";
        String encryptedFile = "Encrypted File";
        String decryptedFile = "Decrypted File";
        encryptFile(inputFileName, encryptedFile);
        FileDecryption.decryptFile(encryptedFile, decryptedFile);
    }
    public static void encryptFile(String inputFileName,
        String encryptedFile)
    throws IOException, FileNotFoundException {
        File file1 = new File(inputFileName);

        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(encryptedFile);
        int[] encrypt = {
            100
        };

        DataOutputStream outputFile =
            new DataOutputStream(new FileOutputStream("Encrypt.dat"));

        for (int i = 0; i < encrypt.length; i++)
            fos.write(encrypt[i] + 100);

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

        outputFile.close();
    }
}

class FileDecryption {
    public static void decryptFile(String inputFileName,
        String encryptedFile)
    throws IOException, FileNotFoundException {
        File file1 = new File(inputFileName);

        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(encryptedFile);
        int[] decrypt = {
            0
        };

        DataInputStream inputFile =
            new DataInputStream(new FileInputStream("Encrypt.dat"));

        for (int i = 0; i < decrypt.length; i--)
            fos.write(decrypt[i] - 100);

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

        inputFile.close();


 }
}

What would be a good, simple test code? And where should I put comments? I don't want a bunch of unnecessary comments, though I don't know which comments would be necessary.

Aucun commentaire:

Enregistrer un commentaire