Des encryption demo in Java

Source: Internet
Author: User

Import Java. security. securerandom; <br/> Import javax. crypto. cipher; <br/> Import javax. crypto. secretkey; <br/> Import javax. crypto. secretkeyfactory; <br/> Import javax. crypto. spec. deskeyspec; </P> <p> import sun. misc. base64decoder; <br/> import sun. misc. base64encoder; </P> <p> public class deshelper {<br/>/** <br/> * encryption and decryption interface <br/> * @ Param data <br/> * @ Param password: the encryption and decryption password must be 8 bytes <br/> * @ Param flag: encryption, 1: decryption <br/> * @ return <br/> */<br/> Public static string dowork (string data, string password, int flag) {<br/> try {<br/> securerandom random = new securerandom (); <br/> deskeyspec validation ey = new deskeyspec (password. getbytes (); <br/> secretkeyfactory keyfactory = secretkeyfactory. getinstance ("des"); <br/> secretkey securekey = keyfactory. generatesecret (secret ey); <br/> cipher = cipher. getinstance ("des"); <br/> // <br/> If (flag = 0) {<br/> base64encoder base64encoder = new base64encoder (); <br/> cipher. init (cipher. encrypt_mode, securekey, random); <br/> return base64encoder. encode (cipher. dofinal (data. getbytes ("UTF-8"); <br/>}else {<br/> base64decoder base64decoder = new base64decoder (); <br/> byte [] encodebyte = base64decoder. decodebuffer (data); <br/> cipher. init (cipher. decrypt_mode, securekey, random); <br/> byte [] decoder = cipher. dofinal (encodebyte); <br/> return new string (decoder, "UTF-8"); <br/>}</P> <p >} catch (exception E) {<br/> E. printstacktrace (); <br/>}< br/> return NULL; <br/>}</P> <p>/** <br/> * test <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> try {<br/> // plaintext <br/> string STR = "Mobile: 15810557051 | type: 0 | content: Hello "; <br/> // password <br/> string Password = "01010101"; <br/> string DESC = deshelper. dowork (STR, password, 0); <br/> system. out. println ("ciphertext:" + DESC); <br/> // decrypt <br/> STR = deshelper. dowork (DESC, password, 1); <br/> system. out. println ("plaintext:" + Str); <br/>} catch (exception E1) {<br/> e1.printstacktrace (); <br/>}</P> <p >}< br/>}

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.