Results 1 to 6 of 6
Thread: Very Simple Encryption
- 03-22-2009, 01:11 AM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Very Simple Encryption
Hi, my friend asked me to make him a very simple program that could generate an encryption substitution code, enter your own code, and encrypt and decrypt a text file. So, i started to make the programs... and eventually I got them all to work. But I had a few problems, and I wanted to know if there were any easier or more efficient ways for me to do some things I did. This is a low priority request, as the programs do work just fine. I just wish to better my habits if anything I did is a bad practice. Anyway, any advice is appreciated. Thanks. :)
CryptGen - Generates a random substitution code and saves to a file.
CryptMaker - Allows you to enter a substitution code and saves it to a file.Java Code:import java.util.Random; import java.io.*; public class CryptGen { public static void main(String[] args)throws IOException { Random gen = new Random(); PrintWriter write = new PrintWriter(new File("CryptCode.txt")); char[] chars = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; char[] cryptList = new char[52]; int tracker = 0; while(tracker < 52) { int num = gen.nextInt(52); if(cryptList[num] == '\u0000') { cryptList[num] = chars[tracker]; tracker++; } } for(int i = 0; i < 52; i++) { write.println(chars[i] + " " + cryptList[i]); } write.close(); } }
Encrypt - Encrypts the plain text.Java Code:import java.util.Scanner; import java.io.*; public class CryptMaker { public static void main(String[] args)throws IOException { Scanner read = new Scanner(System.in); PrintWriter write = new PrintWriter(new File("CryptCode.txt")); char[] chars = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; char[] cryptList = new char[52]; for(int i = 0; i < 52; i++) { System.out.print(chars[i] + ": "); cryptList[i] = read.nextLine().charAt(0); } for(int i = 0; i < 52; i++) { write.println(chars[i] + " " + cryptList[i]); } write.close(); } }
Decrypt - Decrypts the cipher text.Java Code:import java.util.*; import java.io.*; public class Encrypt { public static void main(String[] args)throws IOException { Scanner read = new Scanner(new File("PlainText.txt")); PrintWriter write = new PrintWriter(new File("CipherText.txt")); String word; char chars[]; while(read.hasNext()) { word = read.next(); int length = word.length(); chars = new char[length]; for(int i = 0; i < length; i++) { chars[i] = word.charAt(i); } chars = Substitution(chars, length); chars = Transposition(chars, length); word = ""; for(int i = 0; i < length; i++) { word = word + chars[i]; } write.print(word + " "); //System.out.print(word + " "); } write.close(); } public static char[] Transposition(char[] chars, int length) { char transposed[] = new char[length]; for(int i = 0; i < length && i+2 <= length; i+=2) { transposed[i] = chars[i+1]; transposed[i+1] = chars[i]; } return transposed; } public static char[] Substitution(char[] chars, int length)throws IOException { Scanner getCode = new Scanner(new File("CryptCode.txt")); char plainChars[] = new char[52]; char cipherChars[] = new char[52]; for(int i = 0; i < 52; i++) { String line = getCode.nextLine(); plainChars[i] = line.charAt(0); cipherChars[i] = line.charAt(2); } for(int i = 0; i < length; i++) { for(int j = 0; j < 52; j++) { if(chars[i] == plainChars[j]) { chars[i] = cipherChars[j]; break; } } } return chars; } }
Java Code:import java.util.*; import java.io.*; public class Decrypt { public static void main(String[] args)throws IOException { Scanner read = new Scanner(new File("CipherText.txt")); PrintWriter write = new PrintWriter(new File("PlainText.txt")); String word; char chars[]; while(read.hasNext()) { word = read.next(); int length = word.length(); chars = new char[length]; for(int i = 0; i < length; i++) { chars[i] = word.charAt(i); } chars = Substitution(chars, length); chars = Transposition(chars, length); word = ""; for(int i = 0; i < length; i++) { word = word + chars[i]; } write.print(word + " "); //System.out.print(word + " "); } write.close(); } public static char[] Transposition(char[] chars, int length) { char transposed[] = new char[length]; for(int i = 0; i < length && i+2 <= length; i+=2) { transposed[i] = chars[i+1]; transposed[i+1] = chars[i]; } return transposed; } public static char[] Substitution(char[] chars, int length)throws IOException { Scanner getCode = new Scanner(new File("CryptCode.txt")); char plainChars[] = new char[52]; char cipherChars[] = new char[52]; for(int i = 0; i < 52; i++) { String line = getCode.nextLine(); plainChars[i] = line.charAt(0); cipherChars[i] = line.charAt(2); } for(int i = 0; i < length; i++) { for(int j = 0; j < 52; j++) { if(chars[i] == cipherChars[j]) { chars[i] = plainChars[j]; break; } } } return chars; } }Last edited by AndrewM16921; 03-22-2009 at 04:53 AM.
- 03-24-2009, 02:34 AM #2
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Also, I tried to get an Expansion method to work. Unfortunately, I never got it to work quite right. I think it has something to do with my need to change the array size of chars[], so I'll figure it out eventually. If you have some hints for that, I could use them. Also, if anyone has some additional ideas for things I could add to this, let me know. Thanks. :)
- 03-24-2009, 03:20 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
Just a suggestion, if you know C, it might just might just better to use that instead of java. Then again, I'm a n00b.
- 03-24-2009, 11:21 AM #4
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
I wasn't expecting that from a Java forum, lol. But why would you suggest C for this, rather than Java?
- 03-24-2009, 12:36 PM #5
Poincare... OK, I'll bite... why do you think this assignment would be better to do in C, than in Java?
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
Similar Threads
-
Encryption Project
By JennaTailier in forum New To JavaReplies: 6Last Post: 05-03-2009, 12:38 AM -
Encryption/Decryption
By Echilon in forum New To JavaReplies: 2Last Post: 03-24-2009, 11:58 AM -
encryption
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 02-06-2008, 10:27 AM -
Encryption/Decryption Through AOP
By SirRawlins in forum Advanced JavaReplies: 0Last Post: 12-19-2007, 03:22 PM -
Java encryption
By soul_krasty in forum Advanced JavaReplies: 3Last Post: 08-14-2007, 02:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks