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!

caesar cipher - how to encrypt and decrypt files

807598Apr 19 2006 — edited Apr 19 2006
hey,
i am new to java and just looking at the caesar cipher encryption technique. i have written the encryption and decryption methods but i was wondering if this could be applied to selected files on my laptop somehow?

here is the encryption method:

BufferedReader in =new BufferedReader(new InputStreamReader(System.in));  //reader for keyboard
						int key=10; //I have chosen 22 to be my key value but you can change this to any value between 0 and 25
						System.out.println("Enter the text you wish to encrypt please: ");
						String s=in.readLine();   // string s now contains the plaintext
						for(int i=0;i<s.length();i++)  //we look at the plaintext one character at a time
						{
							int current=s.charAt(i);   //current holds the character as an int value - the ASCII value
							if(current>64 && current<91)  //if the character is an upper case letter
							{
								current=current+key;  //add the key value onto the ASCII value
								if(current>90) current=current-26;  //if we've come off the end of the alphabet then we loop
							}										//back around by subtracting 26.
							else if(current>96 && current<123) //if the character is a lower case letter
							{
								current=current+key;
								if(current>122)current=current-26;
							}
							System.out.print((char)(current));  //now we print out the encrypted character or if the character
		}System.out.println();
}
Here is the decryption

			BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
					int key=16;
					System.out.println("Enter the text you wish to encrypt please: ");
					String s=in.readLine();
					for(int i=0;i<s.length();i++)
					{
						int current=s.charAt(i);
						if(current>64 && current<91)
						{
							current=current+key;  //add the key value onto the ASCII value
							if(current>90) current=current-26;  //if we've come off the end of the alphabet then we loop
						}										//back around by subtracting 26.
						else if(current>96 && current<123) //if the character is a lower case letter
						{
							current=current+key;
							if(current>122)current=current-26;
						}
						System.out.print((char)(current));
		}System.out.println();
}
Any help would be greatfully received, thank you
Marcello
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 17 2006
Added on Apr 19 2006
3 comments
409 views