Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

simple encryption / decryption

843789Feb 2 2010 — edited Feb 2 2010
hello,

can anyone help? i am at my wits end with this encryption problem and have had to scrap some code i thought was encrypting and decrypting a file but turns out it was doing nothing. the code that seems to work that i have, reads a file and prints to the console window some absolute rubbish. basicallt i need some simple code that will take the text file print the contents to screen, encrypt it using the keys then print the encrypted text to screen and saving the encrypted text in a new file. it then needs to read the encrypted file contents and decrypt it back again and print this to screen, prving that both encrypt and decrypt work. so if it was encrypteing / decrypting the word java using the keys in the code it should output like this:

java -- encrypt to:
a717 -- save text in new file

a717 - - decrypt back to:
java

can anyone help me to get this to work, i dont know what else to do; heres what i have that i thought worked...
public class encryption 
{
	private String key1 ="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private String key2 ="78tuv9ABCabcpqrswxyz01KLMNOPQ23456DEFGHIJRSTUVdefghijklmnoWXYZ";
	
	public void encrypt (String text)
	{
		String firstLetter = "" +text.charAt(0);
		String returnedValue = encryptLetter(firstLetter);
		
		System.out.println(returnedValue);
	}
	
	public void decrypt (String text)
	{
		String firstLetter = ""+text.charAt(0);//takes first letter in array
		String returnedValue = decryptLetter(firstLetter);
		
		System.out.println(returnedValue);
		
	}
	
	private String encryptLetter (String letter)
	{
		String returnString = letter; //defaults to original letter if nothing found
		int location1 = key1.indexOf(letter);
		
		if(location1 >= 0)
		{
			returnString = "" +key2.charAt(location1);//"" returns a string from a character
			
		}
		return returnString;
		
	}
 

	private String decryptLetter (String letter)//need to make a decrypt
	{
		String returnString = letter; //defaults to original letter if nothing found
		int location2 = key2.indexOf(letter);
		
		if(location2 <=0)
		{
			returnString = "" +key1.charAt(location2);
			
		}
		return returnString;
	}
 
}
class fileIO
{
   public String readFile(String path)
   {
	   String data=""; 
	   
	   try {
		FileInputStream fis = new FileInputStream(new File(path));
		BufferedInputStream bio = new BufferedInputStream(new DataInputStream(fis));
		try {
			while(bio.read()!=-1)
			{
				data=data+(char)bio.read();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 return data;  
	 
   }
   public void writeFile(String s,String path)
   {
	   FileOutputStream out; // declare a file output object
       PrintStream p; // declare a print stream object
       
      
       try
       {
               // Create a new file output stream
               // connected to "myfile.txt"
               out = new FileOutputStream(path);
 
               // Connect print stream to the output stream
               p = new PrintStream( out );
 
               p.println (s);
 
               p.close();
       }
       catch (Exception e)
       {
               System.err.println ("Error writing to file");
       }
       
       
       System.out.println("The following new encryption has been saved here: "+ path);
       
   }
}
public class fileIOStart {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileContents = "";


fileIO testFile = new fileIO();//start new instance of read write file
 

fileContents = testFile.readFile("c:\\Users\\d\\Desktop\\sample.txt");//read this file extension



testFile.writeFile(fileContents, "c:\\Users\\d\\Desktop\\sample2.txt");//write the contents of the file into another
 
System.out.println(fileContents);//print the contents onto the screen
 



fileContents = testFile.readFile("c:\\Users\\Daniel\\Desktop\\sample2.txt");//read this file extension



testFile.writeFile(fileContents, "c:\\Users\\Daniel\\Desktop\\sample3.txt");//write the contents of the file into another
 
System.out.println(fileContents);//print the contents onto the screen

 
 
 
}
 
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 2 2010
Added on Feb 2 2010
16 comments
643 views