Exercise
File encrypter
Objetive
Create a program to encrypt a text file into another text file.
Example Code
import java.util.*;
public class Main
{
public static void main(String[] args)
{
java.io.InputStreamReader inputFile;
java.io.OutputStreamWriter outputFile;
String line;
String name;
if (args.length < 1)
{
System.out.println("Not enough parameters!");
System.out.println("Enter file name: ");
name = new Scanner(System.in).nextLine();
}
else
{
name = args[0];
}
inputFile = File.OpenText(name);
outputFile = File.CreateText(name + ".encrypted");
do
{
line = inputFile.ReadLine();
if (line != null)
{
String newText = Encrypter.Encrypt(line);
outputFile.write(newText + System.lineSeparator());
}
} while (line != null);
inputFile.close();
outputFile.close();
}
}
public class Encrypter
{
public static String Encrypt(String originalString)
{
String result = "";
for (char letter : originalString)
{
char newLetter = letter;
newLetter++;
result += newLetter;
}
return result;
}
public static String Decrypt(String originalString)
{
String result = "";
for (char letter : originalString)
{
char newLetter = letter;
newLetter--;
result += newLetter;
}
return result;
}
}