An encryption and decryption tool class is used as follows:
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/>
The server needs a JSP and a Java class. jsp can directly call the Java class or directly write it in the servlet. The server is as follows:
Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstreamreader; <br/> Import Java. io. outputstreamwriter; <br/> Import java.net. malformedurlexception; </P> <p> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> Import COM. sohu. SCSI. pub. util. deshelper; </P> <p> public class server {<br/> Public static void post (httpser Vletrequest request, httpservletresponse response) {<br/> string result = ""; <br/> try {<br/> request. setcharacterencoding ("UTF-8"); <br/> bufferedreader BR = new bufferedreader (New inputstreamreader (request. getinputstream (); <br/> string line = ""; <br/> stringbuffer Buf = new stringbuffer (); <br/> while (line = BR. readline ())! = NULL) {<br/> Buf. append (line); <br/>}< br/> result = Buf. tostring (); <br/> system. out. println (deshelper. dowork (result, "01010101", 1); </P> <p> response. setheader ("cache-control", "No-Cache"); <br/> response. setcharacterencoding ("UTF-8"); <br/> response. setcontenttype ("text/html"); <br/> outputstreamwriter out = new outputstreamwriter (response. getoutputstream (); <br/> string xmlinfo = "Mobile: user number | type: Carrier ID | content: Hello"; <br/> out. write (deshelper. dowork (xmlinfo, "01010101", 0); <br/> out. flush (); <br/> out. close (); <br/> // system. out. println ("Return:" + xmlinfo); <br/>} catch (malformedurlexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> // return "toff:" + result; <br/>}< br/>
The server. jsp page is as follows:
<% @ Page contenttype = "text/html; charset = GBK" %> <br/> <% <br/> COM. sohu. SMS. bus. server. post (request, response); <br/> %>
Add the JSP to the local deployment. The address is http: // localhost: 8080/SMS/Media/bus/server. jsp.
The client implementation is as follows:
Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstreamreader; <br/> Import Java. io. outputstreamwriter; <br/> Import java.net. malformedurlexception; <br/> Import java.net. URL; <br/> Import java.net. urlconnection; </P> <p> Import COM. sohu. SCSI. pub. util. deshelper; </P> <p> public class client {<br/> Public static void post () {<br/> string result = ""; <br/> try {<br/> St Ring urlstr = "http: // localhost: 8080/SMS/Media/bus/server. JSP "; <br/> URL url = new URL (urlstr); <br/> urlconnection con = URL. openconnection (); <br/> con. setdooutput (true); <br/> con. setrequestproperty ("Pragma:", "No-Cache"); <br/> con. setrequestproperty ("cache-control", "No-Cache"); <br/> con. setrequestproperty ("Content-Type", "text/html"); </P> <p> outputstreamwriter out = new outputstreamwriter (Con. getoutputstream (); <br/> string xmlinfo = "Mobile: user number | type: Carrier ID | content: I am"; <br/> out. write (deshelper. dowork (xmlinfo, "01010101", 0); <br/> out. flush (); <br/> out. close (); </P> <p> bufferedreader BR = new bufferedreader (New inputstreamreader (con. getinputstream (); <br/> string line = ""; <br/> stringbuffer Buf = new stringbuffer (); <br/> while (line = BR. readline ())! = NULL) {<br/> Buf. append (line); <br/>}< br/> result = Buf. tostring (); <br/> result = deshelper. dowork (result, "01010101", 1); <br/> system. out. println ("reply:" + result); <br/> // result = new string (deshelper. dowork (result, "sopu01hz", 1); <br/> // system. out. println (result); <br/>} catch (malformedurlexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> Public static void main (string [] ARGs) {<br/> client. post (); <br/>}< br/>
The result of directly executing client. Java is as follows:
Reply: Mobile: user number | type: Operator logo | content: Hello
This instance is only simple to implement the service client mode, and the code needs to be optimized. Currently, there are multiple methods for sending content, such as direct socket implementation, or HTTP implementation, or by publishing WebService implementation, data transmission can be varied. Standard Data is generally transmitted in XML or JSON format. Currently, there are many JSON open-source frameworks that support JSON serialization and deserialization. I use an xstream open-source framework, java object and XML deserialization, and Java object and JSON deserialization.
For multi-platform interfaces, we recommend that you send standard XML or JSON data to improve scalability.