Important alert: (current site time 7/15/2013 3:41:41 PM EDT)
 

VB icon

Caesers Shift encryption

Email
Submitted on: 11/13/2002 5:43:48 PM
By: Steve Klein 
Level: Advanced
User Rating: By 3 Users
Compatibility: Java (JDK 1.2)
Views: 14099
 
     Encrypt by shifting x number of characters

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Caesers Shift encryption
// Description:Encrypt by shifting x number of characters
// By: Steve Klein
//
// Inputs:Text and Shift
//
// Assumes:Really simple encrypt. Any number (n) encrypts and (-n_ decrypts. This is the most basic of encryptions, but I like the GUI. Im almost done a code for the RSA and maybe I'll post it.....maybe
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3292&lngWId=2//for details.//**************************************

import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
public class TextAreaDemo extends JFrame {
 private JTextArea textArea1, textArea2;
 private JButton copyButton;
 private JTextField textField1;
 // set up GUI
 public TextAreaDemo()
 {
 // super( "TextArea Demo" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
String string = "attack at dawn";
// set up textArea1
textArea1 = new JTextArea( string, 10, 15 );
container.add( new JScrollPane( textArea1 ) );
textField1 = new JTextField ("Enter shift");
// set up copyButton
copyButton = new JButton( "Encrypt!" );
	 copyButton.addActionListener (
 // anonymous inner class to handle copyButton event
 new ActionListener() {
 // set text in textArea2 to selected
 // text from textArea1
 public void actionPerformed( ActionEvent event )
 {
			 String cText = "";
			 String plainText = textArea1.getText();
			 		//
			 		// prepare plaintext for encryption
			 		//
			 		for ( int i = 0; i < plainText.length(); i++ )
			 		{
			 			if ( plainText.charAt ( i ) != ' ' )
			 			{
			 				cText = cText + plainText.charAt ( i );
			 			}
			 		}
			 		plainText = cText;
			 		cText = "";
			 		for ( int i = 0; i < plainText.length(); i++ ) // do whole string
			 		{
			 			cText = cText + ( char ) ( ( plainText.charAt ( i ) + Integer.parseInt(textField1.getText())) % 256 );
			 		}
textArea2.setText( cText );
 }
 } // end anonymous inner class
); // end call to addActionListener
container.add( copyButton );
container.add(textField1);
// set up textArea2
textArea2 = new JTextArea( 10, 15 );
textArea2.setEditable( false );
container.add( new JScrollPane( textArea2 ) );
setSize( 215, 415);
setVisible( true );
 }
 // execute application
 public static void main( String args[] )
 {
TextAreaDemo application = new TextAreaDemo();
application.setDefaultCloseOperation(
 JFrame.EXIT_ON_CLOSE );
 }
} // end class TextAreaDemo


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
2/26/2003 11:07:10 AMjohn hunsley

good code,
might be useful to have a decrypt button aswell!
(If this comment was disrespectful, please report it.)

 
5/17/2003 2:17:12 PMSimon A Scott

It is beyond my powers of reasoning as why /how this gets to be:
'Code of the Day' ?
(If this comment was disrespectful, please report it.)

 
5/30/2003 12:52:48 PM

Is good, but I need other kind of security like ssl security. Can you help me please?
(If this comment was disrespectful, please report it.)

 
5/30/2003 12:54:15 PM

Is ok your code, but I need other kind of security, please help me to know how can I programming in Java with SSL ?
(If this comment was disrespectful, please report it.)

 
3/19/2004 9:09:06 AM

i want this code to practice about encryption
(If this comment was disrespectful, please report it.)

 
10/5/2004 12:24:57 AM

its a very good sample code of encryption thanks!
(If this comment was disrespectful, please report it.)

 
5/11/2007 3:45:23 PM

I need help with the

cText = cText + ( char ) ( ( plainText.charAt ( i ) + Integer.parseInt(textField1.getText())) % 256 );

line of the code. I tried it, and it runs perfectly well, but I can't understand the "% 256" part. I'd be very happy if the author or someone else explained it to me. Thank You..
(If this comment was disrespectful, please report it.)

 
1/31/2008 12:50:51 PMD3adL!ne

You should catch the NumberFormatException and check if the TextArea contains something.
And give your components onvenient names ;D
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.