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 /** * Decryption 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 Decrypt 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 decrypt int iNumberTemp; //temporary parse buffer int iDig1000, iDig100, iDig10, iDig1; //four Parsed digits String strNumberDecrypted; //result of Decryption int iErr; //error during decryption = -1 otherwise 0 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) { DecryptInputData(); iErr = 0; } else { showStatus( "ERROR-USE FOUR CHARACTERS."); strInput = ""; iErr = -1; } repaint(); } public void DecryptInputData() { int iNumberDecrypted; //Result of decryption //--- 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; strNumberDecrypted = ""; 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; //--- Decrypt: Swap 1st and 3rd digits, 2nd and 4th iNumberTemp = iDig1000; iDig1000 = iDig10; iDig10 = iNumberTemp; iNumberTemp = iDig100; iDig100 = iDig1; iDig1 = iNumberTemp; //--- Decrypt: continued:( Num + 3 ) % 10 iDig1000 = (iDig1000 + 3) % 10; iDig100 = (iDig100 + 3) % 10; iDig10 = (iDig10 + 3) % 10; iDig1 = (iDig1 + 3) % 10; //--- Assemble digits into a single encrypted number. iNumberDecrypted = iDig1000*1000 + iDig100*100 + iDig10*10 + iDig1; //--- Must have leading zero's filled if (iNumberDecrypted > 999) strNumberDecrypted = Integer.toString(iNumberDecrypted); else if (iNumberDecrypted > 99) strNumberDecrypted = "0" + Integer.toString(iNumberDecrypted); else if (iNumberDecrypted > 9) strNumberDecrypted = "00" + Integer.toString(iNumberDecrypted); else strNumberDecrypted = "000" + Integer.toString(iNumberDecrypted); } } 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("Decrypted Value = " + strNumberDecrypted, 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 } }