Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 149,016 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,023 people online right now. Registration is fast and FREE... Join Now!





Caesar Cipher Box (Encrypt/Decrpyt)

This is a small program that lets you enter either an encoded or decoded phrase and lets you encrypt or decrypt the phrase depending on what you want to do. There are two class files and two execution files listed below. You will need to have BreezySwing.jar available.

Submitted By: Whizgirl
Actions:
Rating:
Views: 13,385

Language: Java

Last Modified: February 1, 2006
Instructions: Seperate the three files and compile. Run the file listed as CeasarBox.java. Make sure

Snippet


  1. /**
  2. * CeasarBox.java
  3. *
  4. */
  5.  
  6. import javax.swing.*;
  7. import BreezySwing.*;
  8. import TerminalIO.*;
  9.  
  10. public class CeasarBox extends GBFrame {
  11.    
  12.     //Declare variables for the window objects.
  13.     private JLabel          encryptLabel;
  14.     private JLabel           decryptLabel;
  15.     private JTextField  encryptField;
  16.     private JTextField  decryptField;
  17.     private JButton          encryptButton;
  18.     private JButton      decryptButton;
  19.    
  20.     //Constructor
  21.     public CeasarBox(){
  22.          //Instantiate and add window objects to the window.
  23.          encryptLabel  = addLabel      ("Enter phrase to be encrypted:" ,1,1,2,1);
  24.          decryptLabel  = addLabel      ("Enter phrase to be decrypted:" ,1,3,2,1);         
  25.          encryptField  = addTextField (""        ,2,1,2,1);
  26.          decryptField  = addTextField (""        ,2,3,2,1);
  27.          encryptButton = addButton      ("Encrypt" ,3,1,2,1);
  28.          decryptButton = addButton      ("Decrypt" ,3,3,2,1);
  29.          KeyboardReader reader = new KeyboardReader();
  30.          
  31.     }
  32.    
  33.     //Respond to button clic events
  34.     public void buttonClicked (JButton buttonObj){
  35.          // Local variables
  36.          Encrypt eCrypt = new Encrypt();
  37.          Decrypt dCrypt = new Decrypt();
  38.          
  39.          // Determine which button was clicked.
  40.          if (buttonObj == encryptButton){
  41.              
  42.               //Encrypt the phrase
  43.               eCrypt.setString(encryptField.getText());
  44.               decryptField.setText(eCrypt.getString());
  45.          
  46.          }else{
  47.              
  48.               // Decrypt the phrase
  49.               dCrypt.setString(decryptField.getText());
  50.               encryptField.setText(dCrypt.getString());
  51.          }
  52.     }
  53.    
  54.     // Execution begins in the mehtod main as usual.
  55.     public static void main(String[] args) {
  56.         CeasarBox theGUI = new CeasarBox();
  57.         theGUI.setSize (350, 100);          //Set the window's size in pixels
  58.                                                 //  width = 350, height = 100
  59.         theGUI.setVisible (true);          //Make the window visible
  60.     }
  61. }
  62.  
  63. /**************End of CeasarBox.java*/
  64.  
  65. import java.awt.*;
  66. import java.awt.event.*;
  67.  
  68. /**
  69. * Sample application using Frame.
  70. *
  71. * @author
  72. * @version 1.00 05/03/21
  73. */
  74. public class CeasarBoxFrame extends Frame {
  75.    
  76.     /**
  77.      * The constructor.
  78.      */ 
  79.      public CeasarBoxFrame() {
  80.                
  81.         MenuBar menuBar = new MenuBar();
  82.         Menu menuFile = new Menu();
  83.         MenuItem menuFileExit = new MenuItem();
  84.        
  85.         menuFile.setLabel("File");
  86.         menuFileExit.setLabel("Exit");
  87.        
  88.         // Add action listener.for the menu button
  89.         menuFileExit.addActionListener
  90.         (
  91.             new ActionListener() {
  92.                 public void actionPerformed(ActionEvent e) {
  93.                     CeasarBoxFrame.this.windowClosed();
  94.                 }
  95.             }
  96.         );
  97.         menuFile.add(menuFileExit);
  98.         menuBar.add(menuFile);
  99.        
  100.         setTitle("CeasarBox");
  101.         setMenuBar(menuBar);
  102.         setSize(new Dimension(400, 400));
  103.        
  104.         // Add window listener.
  105.         this.addWindowListener
  106.         (
  107.             new WindowAdapter() {
  108.                 public void windowClosing(WindowEvent e) {
  109.                     CeasarBoxFrame.this.windowClosed();
  110.                 }
  111.             }
  112.         )
  113.     }
  114.    
  115.    
  116.     /**
  117.      * Shutdown procedure when run as an application.
  118.      */
  119.     protected void windowClosed() {
  120.          
  121.          // TODO: Check if it is safe to close the application
  122.          
  123.         // Exit application.
  124.         System.exit(0);
  125.     }
  126. }
  127.  
  128.  
  129. /**************End of CeasarBoxFrame.java*/
  130.  
  131.  
  132. public class Decrypt {
  133.      
  134.      public int stringLength;
  135.      public int square;
  136.      public String decryptedString = "";
  137.      public String inputedString = "";
  138.      
  139.      public String setString(String s) {
  140.           for(int i= 0; i < s.length(); i++) {
  141.                if(s.charAt(i) != ' ') {
  142.                     inputedString += s.charAt(i);
  143.                }
  144.           }
  145.           stringLength = inputedString.length();
  146.           for(int i = 1; i < (stringLength / 2); i++) {
  147.                if (i * i == stringLength) {
  148.                     square = i;
  149.                     break;
  150.                }
  151.           }
  152.           return inputedString;         
  153.      }
  154.      
  155.      public String getString() {
  156.           char[][] array = new char[square][square];
  157.           int counter = 0;
  158.           for(int i = 0; i < square; i++) {
  159.                for(int j = 0; j < square; j++) {
  160.                     array[i][j] = inputedString.charAt(counter);
  161.                     counter++;
  162.                }
  163.           }
  164.           for(int i = 0; i < square; i++) {
  165.                for(int j = 0; j < square; j++) {
  166.                     decryptedString += array[j][i];
  167.                }
  168.           }
  169.           return decryptedString;
  170.      }
  171. }
  172.  
  173. /*****End of decrypt.java*/
  174.  
  175.  
  176. public class Encrypt {
  177.      
  178.      public int stringLength;
  179.      public int square;
  180.      public String encryptedString = "";
  181.      public String inputedString = "";
  182.      
  183.      public String setString(String s) {
  184.           for(int i= 0; i < s.length(); i++) {
  185.                if(s.charAt(i) != ' ') {
  186.                     inputedString += s.charAt(i);
  187.                }
  188.           }
  189.           stringLength = inputedString.length();
  190.           for(int i = 1; i < (stringLength / 2); i++) {
  191.                if (i * i == stringLength) {
  192.                     square = i;
  193.                     break;
  194.                }
  195.           }
  196.           return inputedString;         
  197.      }
  198.      
  199.      public String getString() {
  200.           char[][] array = new char[square][square];
  201.           int counter = 0;
  202.           for(int i = 0; i < square; i++) {
  203.                for(int j = 0; j < square; j++) {
  204.                     array[j][i] = inputedString.charAt(counter);
  205.                     counter++;
  206.                }
  207.           }
  208.           for(int i = 0; i < square; i++) {
  209.                for(int j = 0; j < square; j++) {
  210.                     encryptedString += array[i][j];
  211.                }
  212.           }
  213.           return encryptedString;
  214.      }
  215. }
  216.  
  217. /*****End of encrypt.java*/
  218.  

Copy & Paste


Comments


potator 2007-12-02 15:46:33

It would've been better if you told us how you encrypted the text or what method you used. Can anyone figure this out. We can't e-mail or private message Whizgirl.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month