Java basics-an example of encryption and decryption principles
Last Update:2017-08-12
Source: Internet
Author: User
Java basics-an example of the principles of encryption and decryption-general Linux technology-Linux programming and kernel information. For more information, see the following. Package lockunlock;
Import java. awt .*;
Import java. awt. event .*;
Import java. applet .*;
Import javax. swing .*;
Import java. util .*;
Public class LockUnlock extends JApplet {
Private boolean isStandalone = false;
// Get a parameter value
Public String getParameter (String key, String def ){
Return isStandalone? System. getProperty (key, def ):
(GetParameter (key )! = Null? GetParameter (key): def );
}
// Construct the applet
Public LockUnlock (){
}
// Initialize the applet
Public void init (){
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
// Component initialization
Private void jbInit () throws Exception {
ContentPane = (JPanel) this. getContentPane ();
JLabel1.setText ("String ");
JLabel1.setBounds (new Rectangle (35, 36, 57, 21 ));
ContentPane. setLayout (null );
This. setSize (new Dimension (400,300 ));
JLabel2.setText ("String length ");
JLabel2.setBounds (new Rectangle (29, 73, 69, 22 ));
JTextField1.setText ("");
JTextField1.setBounds (new Rectangle (108, 40,166, 17 ));
JTextField2.setText ("");
JTextField2.setBounds (new Rectangle (107, 72, 56, 21 ));
JButton1.setBounds (new Rectangle (30,236,137, 27 ));
JButton1.setText ("Exercise 3 ");
JButton1.addActionListener (new lockunlock_jbutton?actionadapter (this ));
JButton2.setBounds (new Rectangle (218,237,131, 27 ));
JButton2.setText ("Exercise 4 ");
JButton2.addActionListener (new LockUnlock_jButton2_actionAdapter (this ));
JTextField3.setText ("");
JTextField3.setBounds (new Rectangle (106,105, 58, 21 ));
JLabel3.setText ("MoShu ");
JLabel3.setBounds (new Rectangle (36,106, 86, 18 ));
ContentPane. add (jLabel1, null );
ContentPane. add (jButton2, null );
ContentPane. add (jButton1, null );
ContentPane. add (jLabel3, null );
ContentPane. add (jTextField2, null );
ContentPane. add (jLabel2, null );
ContentPane. add (jTextField3, null );
ContentPane. add (jTextField1, null );
}
// Get Applet information
Public String getAppletInfo (){
Return "Applet Information ";
}
// Get parameter info
Public String [] [] getParameterInfo (){
Return null;
}
// Main method
Public static void main (String [] args ){
LockUnlock applet = new LockUnlock ();
Applet. isStandalone = true;
JFrame frame = new JFrame ();
// EXIT_ON_CLOSE = 3
Frame. setdefaclocloseoperation (3 );
Frame. setTitle ("Applet Frame ");
Frame. getContentPane (). add (applet, BorderLayout. CENTER );
Applet. init ();
Applet. start ();
Frame. setSize (400,320 );
Dimension d = Toolkit. getdefatooltoolkit (). getScreenSize ();
Frame. setLocation (d. width-frame. getSize (). width)/2, (d. height-frame. getSize (). height)/2 );
Frame. setVisible (true );
}
// Static initializer for setting look & feel
Static {
Try {
// UIManager. setLookAndFeel (UIManager. getSystemLookAndFeelClassName ());
// UIManager. setLookAndFeel (UIManager. getCrossPlatformLookAndFeelClassName ());
}
Catch (Exception e ){
}
}
// Declare DataMember
Int index;
//-----------------------------------------------------
JPanel contentPane;
JLabel jLabel1 = new JLabel ();
JLabel jLabel2 = new JLabel ();
JTextField jTextField2 = new JTextField ();
JTextField jTextField1 = new JTextField ();
JButton jButton1 = new JButton ();
JButton jButton2 = new JButton ();
JTextField jTextField3 = new JTextField ();
JLabel jLabel3 = new JLabel ();
// ---------------------- N! ------------------------------
Public int function (int N ){
If (N = 1)
Return 1;
Else {
Return N * function (N-1 );
/* Not RETURN function (N-1 );
But N * function (N-1 );*/
}
}
// ----------- Use recursion to find the full arrangement of a string -----------------------
Public void Arrange (String prefix, String suffix, int [] Array ){
String newPrefix, newSuffix;
Int numOfChars = suffix. length ();
If (numOfChars = 1 ){
Array [index] = Integer. parseInt (prefix + suffix );
Index ++;
}
Else {
For (int I = 1; I <= numOfChars; I ++ ){
NewSuffix = suffix. substring (1, numOfChars );
NewPrefix = prefix + suffix. charAt (0 );
Arrange (newPrefix, newSuffix, Array );
Suffix = newSuffix + suffix. charAt (0 );
}
}
}
// ---------- Arrange From the Min to the Max ------------------
/* Public void RankForArrange (int [] Array ){
Int bottom = Array. length-1;
Int temp;
For (int I = bottom; I> 0; I --){
For (int j = 0; jif (Array [j]> Array [j + 1]) {
Temp = Array [j];
Array [j] = Array [j + 1];
Array [j + 1] = temp;
}
}
}
}
*/
// ------------------- Find the aim number ----------------------
Public int FindAim (int aim, int [] Array ){
Boolean isFound = false;
Int location = 0;
Int length = Array. length;
For (int I = 0; iif (Array
==Aim ){ Location = I; IsFound = true; } } If (isFound) Return location; Else System. out. println ("Not Found "); Return location; /* Can't return in if? */ } // ------------------ Creat String ------------------------------- Public String CreatString (int length ){ StringBuffer BufString = new StringBuffer (); For (int I = 1; I <= length; I ++ ){ BufString. append (I ); } Return BufString. toString (); } // ----------- OutPut Result -------------------- Public void OutPutResult1 (){ Index = 0; // clear to 0 String AimString, prefix; AimString = jTextField1.getText (); Int Length = AimString. length (); String strLength = String. valueOf (Length ); Int Aim = Integer. parseInt (AimString ); /* The method. parseInt is converted to the int type. Instead of. getInteger */ Int [] EachArrange = new int [this. function (Length)]; JTextField2.setText (strLength ); Prefix = ""; // Make an empty String If (AimString. length ()> 2 && AimString. length () <9 & AimString! = ""){ Arrange (prefix, AimString, EachArrange ); // RankForArrange (EachArrange ); Arrays. sort (EachArrange ); String result = String. valueOf (FindAim (Aim, EachArrange )); JTextField3.setText (result ); } Else { System. out. println ("Your String is too short "); } } // ---------- Out put result 2 --------------------- Public void OutPutRestlt2 (){ Index = 0; // Let index come back to 0 String strLength, strMoShu, AimString, prefix, suffix; Int Length, MoShu, limit; StrLength = jTextField2.getText (); StrMoShu = jTextField3.getText (); Length = Integer. parseInt (strLength ); MoShu = Integer. parseInt (strMoShu ); Limit = function (Length ); Int [] EachArrange = new int [this. function (Length)]; If (Length> 2 & Length <9 && StrLength! = "" & StrMoShu! = "" & MoShuprefix = ""; Suffix = CreatString (Length ); Arrange (prefix, suffix, EachArrange ); Arrays. sort (EachArrange ); String strResult = String. valueOf (EachArrange [MoShu]); JTextField1.setText (strResult ); } Else System. out. println ("Input Ouf MoShu, Try again "); } Void jbutton?action=med (ActionEvent e ){ This. OutPutResult1 (); } Void jbutton2_actionreceivmed (ActionEvent e ){ This. OutPutRestlt2 (); } //----------------------------------------------------------- } Class lockunlock_jbutton?actionadapter implements java. awt. event. ActionListener { LockUnlock adaptee; Lockunlock_jbutton?actionadapter (LockUnlock adaptee ){ This. adaptee = adaptee; } Public void actionreceivmed (ActionEvent e ){ Adaptee. jbutton?action=med (e ); } } Class LockUnlock_jButton2_actionAdapter implements java. awt. event. ActionListener { LockUnlock adaptee; LockUnlock_jButton2_actionAdapter (LockUnlock adaptee ){ This. adaptee = adaptee; } Public void actionreceivmed (ActionEvent e ){ Adaptee. jbutton2_actionreceivmed (e ); } }