/* Encrypt.java Title: Encryption Example Author: Jon Voiculescu Description: Simple method for encryption/decryption of a text Ascii codes of the key are added to the Ascii codes of the source text The result is shown in the text area. For decryption the addition is replaced by a substraction */ package Encrypt; // import java.awt.*; import java.awt.event.*; import java.applet.*; public class Encrypt extends Applet { // variables & constants String keyText = new String(); // Encription key String sourceText = new String(); // Text to be encrypted / decrypted String resultText = new String(); // The result of the encryption/decryption operation // BEGIN GENERATED CODE // member declarations java.awt.TextField textKey = new java.awt.TextField(); java.awt.Label labelKey = new java.awt.Label(); java.awt.TextArea textAreaSource = new java.awt.TextArea(); java.awt.Button bttnEncrypt = new java.awt.Button(); java.awt.Button bttnDecrypt = new java.awt.Button(); // END GENERATED CODE boolean isStandalone = false; public Encrypt() { } // Retrieve the value of an applet parameter public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } // Get info on the applet parameters public String[][] getParameterInfo() { return null; } // Get applet information public String getAppletInfo() { return "Applet Information"; } // Initialize the applet public void init() { try { initComponents(); } catch (Exception e) { e.printStackTrace(); } } public void initComponents() throws Exception { // BEGIN GENERATED CODE // the following code sets the frame's initial state textKey.setSize(new java.awt.Dimension(210, 20)); textKey.setVisible(true); textKey.setText("Key"); textKey.setLocation(new java.awt.Point(130, 10)); labelKey.setSize(new java.awt.Dimension(90, 20)); labelKey.setVisible(true); labelKey.setText("Encryption Key"); labelKey.setLocation(new java.awt.Point(30, 10)); labelKey.setFont(new java.awt.Font("dialog.bold", 1, 12)); textAreaSource.setSize(new java.awt.Dimension(630, 440)); textAreaSource.setVisible(true); textAreaSource.setText("Source Text"); textAreaSource.setLocation(new java.awt.Point(30, 60)); bttnEncrypt.setSize(new java.awt.Dimension(100, 20)); bttnEncrypt.setVisible(true); bttnEncrypt.setLocation(new java.awt.Point(450, 10)); bttnEncrypt.setLabel("Encrypt"); bttnEncrypt.setFont(new java.awt.Font("dialog.bold", 1, 12)); bttnDecrypt.setSize(new java.awt.Dimension(100, 20)); bttnDecrypt.setVisible(true); bttnDecrypt.setLocation(new java.awt.Point(560, 10)); bttnDecrypt.setLabel("Decrypt"); bttnDecrypt.setFont(new java.awt.Font("dialog.bold", 1, 12)); setSize(new java.awt.Dimension(697, 555)); setLayout(null); setLocation(new java.awt.Point(0, 0)); add(textKey); add(labelKey); add(textAreaSource); add(bttnEncrypt); add(bttnDecrypt); bttnEncrypt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { bttnEncryptActionPerformed(e); } }); bttnDecrypt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { bttnDecryptActionPerformed(e); } }); // END GENERATED CODE setBackground(Color.white); }// end init //------------------------------------------------Events------------------------------------------ // // // ENCRYPT // //------------------------------------------------------------------------------------------ public void bttnEncryptActionPerformed(java.awt.event.ActionEvent e) { int operation = 1; Action(operation); return; }// end encrypt // DECRYPT //------------------------------------------------------------------------------------------- public void bttnDecryptActionPerformed(java.awt.event.ActionEvent e) { int operation = -1; Action(operation); return; }// end decrypt //------------------------------------------------------------------------------------------- void Action(int oper) { keyText = textKey.getText(); //read the text in the Key Field keyText = keyText.trim(); //remove leading or ending white spaces int keyLength = keyText.length(); //number of chars in Key sourceText = textAreaSource.getText(); //read the input text int sourceLength = sourceText.length(); //number of chars in the input text char[] result = new char[sourceLength]; //char array to store the result for (int i=0; i