![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
encryption/decryption program
I have a program that takes a file name as input and either encrypts it or decrypts it and makes a new file. it works fine except if i decrypt the file i need to have a .dec extension on the new file and if i encrypt the file i need a .enc extension on the new file. Does anyone know how i would do this? Here is the program:
import java.io.*;
public class EncryptProg
{
static String line = "";
public static void main(String args[])
{
System.out.println("1. Encrypt File.");
System.out.println("2. Decrypt File.");
line = EncryptProg.getInput();
System.out.print("Please enter File name: ");
String s = EncryptProg.getInput();
EncryptProg.file(s);
}
//gets a single input from the user, and returns it as a String
public static void file(String s)
{
try
{
FileReader f = new FileReader(s);
BufferedReader input = new BufferedReader(f);
FileOutputStream out = new FileOutputStream(s + ".enc");
PrintWriter output = new PrintWriter(out);
String line = input.readLine();
while(line != null)
{
output.println(EncryptProg.encryption(line));
line = input.readLine();
}
output.close();
}
catch(IOException e) {}
}
static String getInput()
{
java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
java.io.BufferedReader console = new java.io.BufferedReader(input);
try
{
return console.readLine();
}
catch(java.io.IOException e)
{
System.err.println("Bad NEWS!!!");
return "";
}
}
static String encryption(String s)
{
String encrypt = "";
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == 'a')
encrypt = encrypt + "n";
else if(s.charAt(i) == 'b')
encrypt = encrypt + "o";
else if(s.charAt(i) == 'c')
encrypt = encrypt + "p";
else if(s.charAt(i) == 'd')
encrypt = encrypt + "q";
else if(s.charAt(i) == 'e')
encrypt = encrypt + "r";
else if(s.charAt(i) == 'f')
encrypt = encrypt + "s";
else if(s.charAt(i) == 'g')
encrypt = encrypt + "t";
else if(s.charAt(i) == 'h')
encrypt = encrypt + "u";
else if(s.charAt(i) == 'i')
encrypt = encrypt + "v";
else if(s.charAt(i) == 'j')
encrypt = encrypt + "w";
else if(s.charAt(i) == 'k')
encrypt = encrypt + "x";
else if(s.charAt(i) == 'l')
encrypt = encrypt + "y";
else if(s.charAt(i) == 'm')
encrypt = encrypt + "z";
else if(s.charAt(i) == 'n')
encrypt = encrypt + "a";
else if(s.charAt(i) == 'o')
encrypt = encrypt + "b";
else if(s.charAt(i) == 'p')
encrypt = encrypt + "c";
else if(s.charAt(i) == 'q')
encrypt = encrypt + "d";
else if(s.charAt(i) == 'r')
encrypt = encrypt + "e";
else if(s.charAt(i) == 's')
encrypt = encrypt + "f";
else if(s.charAt(i) == 't')
encrypt = encrypt + "g";
else if(s.charAt(i) == 'u')
encrypt = encrypt + "h";
else if(s.charAt(i) == 'v')
encrypt = encrypt + "i";
else if(s.charAt(i) == 'w')
encrypt = encrypt + "j";
else if(s.charAt(i) == 'x')
encrypt = encrypt + "k";
else if(s.charAt(i) == 'y')
encrypt = encrypt + "l";
else if(s.charAt(i) == 'z')
encrypt = encrypt + "m";
if(s.charAt(i) == 'A')
encrypt = encrypt + "N";
else if(s.charAt(i) == 'B')
encrypt = encrypt + "O";
else if(s.charAt(i) == 'C')
encrypt = encrypt + "P";
else if(s.charAt(i) == 'D')
encrypt = encrypt + "Q";
else if(s.charAt(i) == 'E')
encrypt = encrypt + "R";
else if(s.charAt(i) == 'F')
encrypt = encrypt + "S";
else if(s.charAt(i) == 'G')
encrypt = encrypt + "T";
else if(s.charAt(i) == 'H')
encrypt = encrypt + "U";
else if(s.charAt(i) == 'I')
encrypt = encrypt + "V";
else if(s.charAt(i) == 'J')
encrypt = encrypt + "W";
else if(s.charAt(i) == 'K')
encrypt = encrypt + "X";
else if(s.charAt(i) == 'L')
encrypt = encrypt + "Y";
else if(s.charAt(i) == 'M')
encrypt = encrypt + "Z";
else if(s.charAt(i) == 'N')
encrypt = encrypt + "A";
else if(s.charAt(i) == 'O')
encrypt = encrypt + "B";
else if(s.charAt(i) == 'P')
encrypt = encrypt + "C";
else if(s.charAt(i) == 'Q')
encrypt = encrypt + "D";
else if(s.charAt(i) == 'R')
encrypt = encrypt + "E";
else if(s.charAt(i) == 'S')
encrypt = encrypt + "F";
else if(s.charAt(i) == 'T')
encrypt = encrypt + "G";
else if(s.charAt(i) == 'U')
encrypt = encrypt + "H";
else if(s.charAt(i) == 'V')
encrypt = encrypt + "I";
else if(s.charAt(i) == 'W')
encrypt = encrypt + "J";
else if(s.charAt(i) == 'X')
encrypt = encrypt + "K";
else if(s.charAt(i) == 'Y')
encrypt = encrypt + "L";
else if(s.charAt(i) == 'Z')
encrypt = encrypt + "M";
else if (s.charAt(i) == ' ')
encrypt = encrypt + " ";
}
return encrypt;
}
} |
|
|
|
|
|
#2 |
|
Programmer
|
2 words.....switch statement
![]() JD |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
ive never heard of a switch statement? Maybe an example or something would help?
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
Ok, first, in the same class you don't have to call methods with the class name
like u did : EncryptProg.file(s); ... u just say file(s). The same for java.io.InputStreamReader .. you imported it at the top of the program so there's no probleme, just use : InputStreamReader Now, what you want is to split the fileName from the extension, so u could put any extension to it. \
public class EncryptProg
{
static String line = "";
public static void main(String args[])
{
System.out.println("1. Encrypt File.");
System.out.println("2. Decrypt File.");
line = getInput();
System.out.print("Please enter File name: ");
String s = getInput();
file(s,line);
public static void file(String s, String l)
{
line = l; // u need to know what the user wants
FileReader f = null;
BufferedReader input = null;
FileWriter out = null;
int i=0;
String[] tokens = new String[2];
StringTokenizer tk = new StringTokenizer(s,".");
while(tk.hasMoreTokens()){
tokens[i] = tk.nextToken();
i++;
}
String fileName = tokens[0];
String extention = tokens[1];
try
{
f = new FileReader(s);
input = new BufferedReader(f);
String line = input.readLine();
if(line.equals("1")) out = new FileWriter(fileName+".dec");
else if(line.equals("2")) out = new FileWriter(fileName+".enc");
out = new FileWriter(fileName+".dec");
PrintWriter output = new PrintWriter(out);
while(line != null)
{
output.println(encryption(line));
line = input.readLine();
}
f.close();
out.close();
}
catch(IOException e) {}
}You should also import java.util.*; for the StringTokenizer. Also, switch won't help. Try using 2 arrays in wich u store the letters (one for a,b,c,d .... and another one for n,o,p,q....) and then using indexes. Hope I haven't made (m)any errors :o
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
Programmer
|
And also for the encryption method instead of writing all the possible characters you should use this code:
char c = null;
for(int i = 0; i < s.length(); i++)
{ if(s.charAt(i) == ' ')
{ encrypt += " ";}
else
{ c = (char)(s.charAt(i) + 13);
if((int)(c) > 123)
{ c = (char)(c - 26); }
}
encrypt += (String)(c);
}The reason why youi should subtract 26 is that if the character goes above 'z' then it should start from 'a' again. Hint for the decryption method: c = (char)(s.charAt(i) - 13);
if((int)(c) < 97)
{ c = (char)(c + 26); }
__________________
countdown++; Last edited by HeX; May 7th, 2005 at 6:27 AM. |
|
|
|
|
|
#6 |
|
Programmer
|
oh... i forgot that there could be also uppercases. here is version 1.1:
char c = null;
for(int i = 0; i < s.length(); i++)
{ if(s.charAt(i) == ' ')
{ encrypt += " ";}
else
{ if(s.charAt(i) < 'z' || s.charAt(i) > 'a')
{ c = (char)(s.charAt(i) + 13);
if((int)(c) > 123)
{ c = (char)(c - 26); }
}
else
{ c = (char)(s.charAt(i) + 13);
if((int)(c) > 91)
{ c = (char)(c - 26); }
}
}
encrypt += (String)(c);
}
__________________
countdown++; |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
ok so now it works fine when i go from an encrypted file to a decyrpted file however it doesnt work when i go from a decrypted file to an ecyrpted file it doesnt even make a new file it just writes over the original? Here is what i changed:
import java.io.*;
import java.util.*;
public class EncryptProg1
{
static String line = "";
public static void main(String args[])
{
System.out.println("1. Encrypt File.");
System.out.println("2. Decrypt File.");
line = getInput();
System.out.print("Please enter File name: ");
String s = getInput();
file(s,line);
}
static void file(String s, String l)
{
line = l; // u need to know what the user wants
FileReader f = null;
BufferedReader input = null;
FileWriter out = null;
int i=0;
String[] tokens = new String[2];
StringTokenizer tk = new StringTokenizer(s,".");
while(tk.hasMoreTokens()){
tokens[i] = tk.nextToken();
i++;
}
String fileName = tokens[0];
String extention = tokens[1];
try
{
f = new FileReader(s);
input = new BufferedReader(f);
String line = input.readLine();
if(line.equals("1")) out = new FileWriter(fileName+".dec");
else if(line.equals("2")) out = new FileWriter(fileName+".enc");
out = new FileWriter(fileName+".dec");
PrintWriter output = new PrintWriter(out);
while(line != null)
{
output.println(encryption(line));
line = input.readLine();
}
f.close();
out.close();
}
catch(IOException e) {}
}
static String getInput()
{
java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
java.io.BufferedReader console = new java.io.BufferedReader(input);
try
{
return console.readLine();
}
catch(java.io.IOException e)
{
System.err.println("Bad NEWS!!!");
return "";
}
}
static String encryption(String s)
{
String encrypt = "";
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == 'a')
encrypt = encrypt + "n";
else if(s.charAt(i) == 'b')
encrypt = encrypt + "o";
else if(s.charAt(i) == 'c')
encrypt = encrypt + "p";
else if(s.charAt(i) == 'd')
encrypt = encrypt + "q";
else if(s.charAt(i) == 'e')
encrypt = encrypt + "r";
else if(s.charAt(i) == 'f')
encrypt = encrypt + "s";
else if(s.charAt(i) == 'g')
encrypt = encrypt + "t";
else if(s.charAt(i) == 'h')
encrypt = encrypt + "u";
else if(s.charAt(i) == 'i')
encrypt = encrypt + "v";
else if(s.charAt(i) == 'j')
encrypt = encrypt + "w";
else if(s.charAt(i) == 'k')
encrypt = encrypt + "x";
else if(s.charAt(i) == 'l')
encrypt = encrypt + "y";
else if(s.charAt(i) == 'm')
encrypt = encrypt + "z";
else if(s.charAt(i) == 'n')
encrypt = encrypt + "a";
else if(s.charAt(i) == 'o')
encrypt = encrypt + "b";
else if(s.charAt(i) == 'p')
encrypt = encrypt + "c";
else if(s.charAt(i) == 'q')
encrypt = encrypt + "d";
else if(s.charAt(i) == 'r')
encrypt = encrypt + "e";
else if(s.charAt(i) == 's')
encrypt = encrypt + "f";
else if(s.charAt(i) == 't')
encrypt = encrypt + "g";
else if(s.charAt(i) == 'u')
encrypt = encrypt + "h";
else if(s.charAt(i) == 'v')
encrypt = encrypt + "i";
else if(s.charAt(i) == 'w')
encrypt = encrypt + "j";
else if(s.charAt(i) == 'x')
encrypt = encrypt + "k";
else if(s.charAt(i) == 'y')
encrypt = encrypt + "l";
else if(s.charAt(i) == 'z')
encrypt = encrypt + "m";
if(s.charAt(i) == 'A')
encrypt = encrypt + "N";
else if(s.charAt(i) == 'B')
encrypt = encrypt + "O";
else if(s.charAt(i) == 'C')
encrypt = encrypt + "P";
else if(s.charAt(i) == 'D')
encrypt = encrypt + "Q";
else if(s.charAt(i) == 'E')
encrypt = encrypt + "R";
else if(s.charAt(i) == 'F')
encrypt = encrypt + "S";
else if(s.charAt(i) == 'G')
encrypt = encrypt + "T";
else if(s.charAt(i) == 'H')
encrypt = encrypt + "U";
else if(s.charAt(i) == 'I')
encrypt = encrypt + "V";
else if(s.charAt(i) == 'J')
encrypt = encrypt + "W";
else if(s.charAt(i) == 'K')
encrypt = encrypt + "X";
else if(s.charAt(i) == 'L')
encrypt = encrypt + "Y";
else if(s.charAt(i) == 'M')
encrypt = encrypt + "Z";
else if(s.charAt(i) == 'N')
encrypt = encrypt + "A";
else if(s.charAt(i) == 'O')
encrypt = encrypt + "B";
else if(s.charAt(i) == 'P')
encrypt = encrypt + "C";
else if(s.charAt(i) == 'Q')
encrypt = encrypt + "D";
else if(s.charAt(i) == 'R')
encrypt = encrypt + "E";
else if(s.charAt(i) == 'S')
encrypt = encrypt + "F";
else if(s.charAt(i) == 'T')
encrypt = encrypt + "G";
else if(s.charAt(i) == 'U')
encrypt = encrypt + "H";
else if(s.charAt(i) == 'V')
encrypt = encrypt + "I";
else if(s.charAt(i) == 'W')
encrypt = encrypt + "J";
else if(s.charAt(i) == 'X')
encrypt = encrypt + "K";
else if(s.charAt(i) == 'Y')
encrypt = encrypt + "L";
else if(s.charAt(i) == 'Z')
encrypt = encrypt + "M";
else if (s.charAt(i) == ' ')
encrypt = encrypt + " ";
}
return encrypt;
}
} |
|
|
|
|
|
#8 |
|
Programmer
|
Again, instead of using all these lines of code
static String encryption(String s)
{
String encrypt = "";
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == 'a')
encrypt = encrypt + "n";
else if(s.charAt(i) == 'b')
encrypt = encrypt + "o";
else if(s.charAt(i) == 'c')
encrypt = encrypt + "p";
else if(s.charAt(i) == 'd')
encrypt = encrypt + "q";
else if(s.charAt(i) == 'e')
encrypt = encrypt + "r";
else if(s.charAt(i) == 'f')
encrypt = encrypt + "s";
else if(s.charAt(i) == 'g')
encrypt = encrypt + "t";
else if(s.charAt(i) == 'h')
encrypt = encrypt + "u";
else if(s.charAt(i) == 'i')
encrypt = encrypt + "v";
else if(s.charAt(i) == 'j')
encrypt = encrypt + "w";
else if(s.charAt(i) == 'k')
encrypt = encrypt + "x";
else if(s.charAt(i) == 'l')
encrypt = encrypt + "y";
else if(s.charAt(i) == 'm')
encrypt = encrypt + "z";
else if(s.charAt(i) == 'n')
encrypt = encrypt + "a";
else if(s.charAt(i) == 'o')
encrypt = encrypt + "b";
else if(s.charAt(i) == 'p')
encrypt = encrypt + "c";
else if(s.charAt(i) == 'q')
encrypt = encrypt + "d";
else if(s.charAt(i) == 'r')
encrypt = encrypt + "e";
else if(s.charAt(i) == 's')
encrypt = encrypt + "f";
else if(s.charAt(i) == 't')
encrypt = encrypt + "g";
else if(s.charAt(i) == 'u')
encrypt = encrypt + "h";
else if(s.charAt(i) == 'v')
encrypt = encrypt + "i";
else if(s.charAt(i) == 'w')
encrypt = encrypt + "j";
else if(s.charAt(i) == 'x')
encrypt = encrypt + "k";
else if(s.charAt(i) == 'y')
encrypt = encrypt + "l";
else if(s.charAt(i) == 'z')
encrypt = encrypt + "m";
if(s.charAt(i) == 'A')
encrypt = encrypt + "N";
else if(s.charAt(i) == 'B')
encrypt = encrypt + "O";
else if(s.charAt(i) == 'C')
encrypt = encrypt + "P";
else if(s.charAt(i) == 'D')
encrypt = encrypt + "Q";
else if(s.charAt(i) == 'E')
encrypt = encrypt + "R";
else if(s.charAt(i) == 'F')
encrypt = encrypt + "S";
else if(s.charAt(i) == 'G')
encrypt = encrypt + "T";
else if(s.charAt(i) == 'H')
encrypt = encrypt + "U";
else if(s.charAt(i) == 'I')
encrypt = encrypt + "V";
else if(s.charAt(i) == 'J')
encrypt = encrypt + "W";
else if(s.charAt(i) == 'K')
encrypt = encrypt + "X";
else if(s.charAt(i) == 'L')
encrypt = encrypt + "Y";
else if(s.charAt(i) == 'M')
encrypt = encrypt + "Z";
else if(s.charAt(i) == 'N')
encrypt = encrypt + "A";
else if(s.charAt(i) == 'O')
encrypt = encrypt + "B";
else if(s.charAt(i) == 'P')
encrypt = encrypt + "C";
else if(s.charAt(i) == 'Q')
encrypt = encrypt + "D";
else if(s.charAt(i) == 'R')
encrypt = encrypt + "E";
else if(s.charAt(i) == 'S')
encrypt = encrypt + "F";
else if(s.charAt(i) == 'T')
encrypt = encrypt + "G";
else if(s.charAt(i) == 'U')
encrypt = encrypt + "H";
else if(s.charAt(i) == 'V')
encrypt = encrypt + "I";
else if(s.charAt(i) == 'W')
encrypt = encrypt + "J";
else if(s.charAt(i) == 'X')
encrypt = encrypt + "K";
else if(s.charAt(i) == 'Y')
encrypt = encrypt + "L";
else if(s.charAt(i) == 'Z')
encrypt = encrypt + "M";
else if (s.charAt(i) == ' ')
encrypt = encrypt + " ";
}
return encrypt;
}you could use this method for encryption: static String encryption(String s)
{
String encrypt = "";
char c = null;
for(int i = 0; i < s.length(); i++)
{ if(s.charAt(i) == ' ')
{ encrypt += " ";}
else
{ if(s.charAt(i) < 'z' || s.charAt(i) > 'a')
{ c = (char)(s.charAt(i) + 13);
if((int)(c) > 123)
{ c = (char)(c - 26); }
}
else
{ c = (char)(s.charAt(i) + 13);
if((int)(c) > 91)
{ c = (char)(c - 26); }
}
}
encrypt += (String)(c);
}
return encrypt;
}and this one for decryption: static String decryption(String s)
{
String decrypt = "";
char c = null;
for(int i = 0; i < s.length(); i++)
{ if(s.charAt(i) == ' ')
{ decrypt += " ";}
else
{ if(s.charAt(i) < 'z' || s.charAt(i) > 'a')
{ c = (char)(s.charAt(i) - 13);
if((int)(c) < 97)
{ c = (char)(c + 26); }
}
else
{ c = (char)(s.charAt(i) - 13);
if((int)(c) < 65)
{ c = (char)(c + 26); }
}
}
decrypt += (String)(c);
}
return decrypt;
}hope u like it ![]()
__________________
countdown++; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|