Des encryption and decryption algorithm implemented by Java

Source: Internet
Author: User

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;   } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.