Results 1 to 6 of 6
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default 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.
    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();
    	}
    }
    CryptMaker - Allows you to enter a substitution code and saves it to a file.
    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();
    	}
    }
    Encrypt - Encrypts the plain 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;
    	}
    }
    Decrypt - Decrypts the cipher text.
    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.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default

    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. :)

  3. #3
    Poincare is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    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.

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default

    I wasn't expecting that from a Java forum, lol. But why would you suggest C for this, rather than Java?

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Poincare... OK, I'll bite... why do you think this assignment would be better to do in C, than in Java?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AndrewM16921 View Post
    I wasn't expecting that from a Java forum, lol. But why would you suggest C for this, rather than Java?
    Simple if the only tool you have is a hammer, pretty soon everything starts looking like a nail. Mr. Andrew, I suggest you ignore poncaire's "advice".

Similar Threads

  1. Encryption Project
    By JennaTailier in forum New To Java
    Replies: 6
    Last Post: 05-03-2009, 12:38 AM
  2. Encryption/Decryption
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 11:58 AM
  3. encryption
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 02-06-2008, 10:27 AM
  4. Encryption/Decryption Through AOP
    By SirRawlins in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 03:22 PM
  5. Java encryption
    By soul_krasty in forum Advanced Java
    Replies: 3
    Last Post: 08-14-2007, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •