package PackageEncryptDecrypt; /*-------------------------------------------------------------------* * NTU Student: Java With Engineering Applications, Spring 1999 * Assignment # 1 - Java How To Program 2nd edition, Deitel & Deitel * Problem 2.31 on page 223 * * Michael Kusmierz * Xerox Corporation * Rev 1.0 Apr 9, 1999 * * This code represents a students work in progress * and is not to be used beyond that purpose. *-------------------------------------------------------------------*/ import java.applet.*; import java.awt.*; import java.awt.event.*; //import the event pkg /** * Encryption Applet for four digit numbers. * This type was created in VisualAge.All other code added manually * without the help of the GUI "Composition" editor. */ public class Encrypt extends Applet implements ActionListener { Label lblPrompt; TextField txtInput; String strInput; //Input text from user filled by actionPerformed int iNumberInput; //number to store input value to encript int iNumberTemp; //temporary parse buffer int iDig1000, iDig100, iDig10, iDig1; //four Parsed digits String strNumberEncrypted; //result of encryption int iErr; //error during encryption = -1 otherwise 0 /** * Encrypt constructor comment. */ public Encrypt() { super(); } public void actionPerformed( ActionEvent e ) { //--- get the text entered strInput = e.getActionCommand(); showStatus( " Entered Integer Data = " + strInput); //--- text must be four characters if (strInput.length() == 4) { encryptInputData(); iErr = 0; } else { showStatus( "ERROR-USE 4 NUMBERS."); strInput = ""; iErr = -1; } repaint(); } public void encryptInputData() { int iNumberEncrypted; //Result of encryption stored here //--- get the number equivalent to the input string iNumberInput = Integer.parseInt(strInput); //--- 4 integers assumed, no negative numbers allowed if (iNumberInput < 0) { showStatus( "ERROR-USE POSITIVE NUMBERS."); iNumberInput = 0; strNumberEncrypted = ""; iErr = -1; } else { //--- parse the number into four digits iDig1000 = (iNumberInput / 1000); iNumberTemp = iNumberInput - (iDig1000 * 1000); iDig100 = (iNumberTemp / 100); iNumberTemp = iNumberTemp - (iDig100 * 100); iDig10 = (iNumberTemp / 10); iNumberTemp = iNumberTemp - (iDig10 * 10); iDig1 = iNumberTemp; //--- Encrypt: ( Num + 7 ) Modulus 10 iDig1000 = (iDig1000 + 7) % 10; iDig100 = (iDig100 + 7) % 10; iDig10 = (iDig10 + 7) % 10; iDig1 = (iDig1 + 7) % 10; //--- Encrypt continued: Swap 1st and 3rd digits, 2nd and 4th //--- Assemble digits into a single encrypted number. iNumberEncrypted = iDig10*1000 + iDig1*100 + iDig1000*10 + iDig100; //--- Construct a four character string //--- Must have leading zero's filled if (iNumberEncrypted > 999) strNumberEncrypted = Integer.toString(iNumberEncrypted); else if (iNumberEncrypted > 99) strNumberEncrypted = "0" + Integer.toString(iNumberEncrypted); else if (iNumberEncrypted > 9) strNumberEncrypted = "00" + Integer.toString(iNumberEncrypted); else strNumberEncrypted = "000" + Integer.toString(iNumberEncrypted); } } public void init() { //--- Add items to applet lblPrompt = new Label( "Enter a four digit integer, press Enter:" ); add( lblPrompt ); txtInput = new TextField( 10); add( txtInput ); //--- Initialize variables iErr = 1; //Idicator of fresh start...Waiting for input //--- This applet handles action events for TextFields txtInput.addActionListener( this ); } /** * This method was created in VisualAge. * @param g java.awt.Graphics */ public void paint(Graphics g) { g.drawString("Encrypted Value = " + strNumberEncrypted, 60, 100 ); switch (iErr) { case -1: g.drawString("Error",60,120); break; case 0: g.drawString("Done",60,120); break; case 1: g.drawString("...Waiting for input",60,120); break; } //---end switch on iErr } }