Package Encrypt;import java.security.*; Import javax.crypto.*; Import sun.misc.*;/** * Using DES encryption and decryption, the byte[],string type can be encrypted and decrypted * ciphertext can be stored using string,byte[]. * Method: * void GetKey (string strkey) generates a key from strkey Note * String getencstring (string strming) to encrypt strming , returns a string cipher * string getdesstring (string strmi) to decrypt the strmin, returning a string plaintext * byte[] Getenccode (byte[] byteS) byte[ ] Type of encryption * byte[] Getdescode (byte[] byted) byte[] Type decryption */public class encrypt{private key key; Private byte[] Bytemi = null; Private byte[] byteming = null; Private String strmi= ""; Private String strm= ""; Generate key public void Setkey (String strkey) According to the parameters {try{Keygenerator _generator = Keygenerator.getinstance (" DES "); _generator.init (New SecureRandom (Strkey.getbytes ())); This.key = _generator.generatekey (); _generator=null; } catch (Exception e) {e.printstacktrace (); }}//Encrypt string plaintext outputInput, string ciphertext output public void setencstring (string strming) {Base64encoder base64en = new Base64encoder (); try {this.byteming = strming.getbytes ("UTF8"); This.bytemi = This.getenccode (this.byteming); This.strmi = Base64en.encode (This.bytemi); } catch (Exception e) {e.printstacktrace (); } finally {this.byteming = null; This.bytemi = null; }}//Encrypt with byte[] plaintext input, byte[] ciphertext output private byte[] Getenccode (byte[] byteS) {byte[] Bytefina = null; Cipher Cipher; try {cipher = cipher.getinstance ("DES"); Cipher.init (Cipher.encrypt_mode,key); Bytefina = Cipher.dofinal (ByteS); } catch (Exception e) {e.printstacktrace (); } finally {cipher = NULL; } return Bytefina; }//decryption: Input in string cipher, string plaintext output public void setdesstring (string strmi) {Base64decoder base64de = new Base64decoder ( ); try {This.bytemi = Base64de.decodebuffer (strMi); this.byteming = This.getdescode (Bytemi); THIS.STRM = new String (byteming, "UTF8"); } catch (Exception e) {e.printstacktrace (); } finally {base64de = null; byteming = null; Bytemi = null; }}//Decrypt with byte[] ciphertext input, byte[] plaintext output private byte[] Getdescode (byte[] byted) {Cipher Cipher; Byte[] Bytefina=null; try{cipher = cipher.getinstance ("DES"); Cipher.init (Cipher.decrypt_mode,key); Bytefina = Cipher.dofinal (byted); } catch (Exception e) {e.printstacktrace (); } finally {cipher=null; } return Bytefina; }//Returns encrypted ciphertext Strmi public String Getstrmi () {return strmi; }//Returns the decrypted plaintext public String GETSTRM () {return strM; } }