I am a beginner at Java programming, and I'm always curious about how to make code more efficient or learn what I am doing wrong. I just started learning about cryptography, and this is a simple cryptography example I made.
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? What do I do to make the Demo file exist? Also, where would be good places to put comments? I don't want to have a bunch of unnecessary comments, but I don't know which ones are necessary.
encrypt
method just writes a file with the byte0xC8
, and thedecrypt
method throws an exception. – Roland Illig May 9 '16 at 6:50