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