Thursday, June 16, 2011
Encryption Program
For the posts that contain things that I don't want others to see, I have encrypted them with my special encryption program so that only I will be able to decipher the code.
However, if for whatever reason I lose the code of my encryption program, I will not be able to decipher my posts. So I decided to post my code onto my Blog as well. So yeah, this post really is just for me...
(Note: a keyword is required on top of the program, and this is why I am comfortable with posting my code)
import java.awt.*;
import hsa.Console;
import java.io.*;
public class Encrypt
{
static Console c; // The output console
static String keyword;
static String line;
static boolean decipher =false;
static int multiplier;
static String inFile;
static String outFile;
public static int change (int original, int key, int min, int max, int hi)
{
int result = 0;
if (hi == -1)
{
result = original - key;
while (result < min || result > max)
{
if (result < min)
result = max - min + 1 + result;
else
result = min - max - 1 + result;
}
}
else
{
for (int x = min ; x <= max ; x = x + 1)
{
if (change (x, key, min, max, -1) == original)
{
return x;
}
}
}
return result;
}
public static String convert (String text, String key)
{
int index = -1;
String newString = "";
int num;
for (int x = 0 ; x < text.length () ; x = x + 1)
{
if ((text.charAt (x) >= 'a' && text.charAt (x) <= 'z') || (text.charAt (x) >= 'A' && text.charAt (x) <= 'Z') || (text.charAt (x) >= '0' && text.charAt (x) <= '9'))
{
index = index + 1;
if (index >= key.length ())
{
index = 0;
}
// c.println (((text.charAt (x))) + " " + ((key.charAt (index))) + " " + (int) ('A') + " " + (int) ('Z'));
if (text.charAt (x) >= 'a' && text.charAt (x) <= 'z')
{
num = change ((int) (text.charAt (x)), (int) (key.charAt (index)), (int) ('a'), (int) ('z'), multiplier);
}
else if (text.charAt (x) >= 'A' && text.charAt (x) <= 'Z')
{
num = change ((int) (text.charAt (x)), (int) (key.charAt (index)), (int) ('A'), (int) ('Z'), multiplier);
}
else
{
num = change ((int) (text.charAt (x)), (int) (key.charAt (index)), (int) ('0'), (int) ('9'), multiplier);
}
newString = newString + (char) (num);
}
else
{
newString = newString + text.charAt (x);
index = 0;
}
}
return newString;
}
public static void save ()
{
keyword = c.readString ();
BufferedReader input = null;
PrintWriter output = null;
try
{
input = new BufferedReader (new FileReader (inFile));
output = new PrintWriter (new FileWriter (outFile));
while (true)
{
line = input.readLine ();
if (line.equalsIgnoreCase ("end"))
break;
line = convert (line, keyword);
output.println (line);
}
input.close ();
output.close ();
}
catch (Exception e)
{
try
{
input.close ();
output.close ();
}
catch (Exception f)
{
}
c.print (e.toString ());
}
}
public static void main (String[] args)
{
c = new Console ();
if (decipher)
{
multiplier = 1;
inFile = "Code.txt";
outFile = "Actual.txt";
}
else
{
multiplier = -1;
outFile = "Code.txt";
inFile = "Actual.txt";
}
save ();
c.print ("Finished");
}
} // Password: *******y***
Labels:
technology
Encryption Program
2011-06-16T19:35:00-04:00
K
technology|
Subscribe to:
Post Comments (Atom)