Java Server and Android client encryption of messages

Source: Internet
Author: User

ImportJava.security.SecureRandom;ImportJavax.crypto.Cipher;ImportJavax.crypto.SecretKey;Importjavax.crypto.SecretKeyFactory;ImportJavax.crypto.spec.DESKeySpec;Importorg.apache.commons.codec.binary.Base64;Importorg.apache.commons.codec.binary.StringUtils;/*** Tool class for encrypting and decrypting the message body when the client communicates with the server *@authorLiutao **/ Public classDesbase64util {/*** Password length must be a multiple of 8*/     Public Final StaticString KEY = "RS35836780805258"; /*** Encryption Method*/     Public Final StaticString des = "des"; /*** Default Encoding*/     Public Final StaticString ENCODING = "UTF-8"; /*** DES encryption * *@paramsrc *@paramKey *@return     * @throwsException*/     Public Static byte[] Encrypt (byte[] SRC,byte[] key)throwsException {//The DES algorithm requires a trustworthy random number sourceSecureRandom sr =NewSecureRandom (); //Create a Deskeyspec object from the original key dataDeskeyspec DKs =NewDeskeyspec (key); //Create a key factory and use it to convert Deskeyspec to a Secretkey objectSecretkeyfactory keyfactory =secretkeyfactory.getinstance (DES); Secretkey SecureKey=Keyfactory.generatesecret (DKS); //The cipher object actually completes the cryptographic operationCipher Cipher =cipher.getinstance (DES); //Initialize the Cipher object with a keyCipher.init (Cipher.encrypt_mode, SecureKey, SR); //now, get the data and encrypt//formally perform cryptographic operations        returncipher.dofinal (SRC); }     /*** des decryption * *@paramsrc *@paramKey *@return     * @throwsException*/     Public Static byte[] Decrypt (byte[] SRC,byte[] key)throwsException {//The DES algorithm requires a trustworthy random number sourceSecureRandom sr =NewSecureRandom (); //Create a Deskeyspec object from the original key dataDeskeyspec DKs =NewDeskeyspec (key); //Create a key factory and use it to convert the Deskeyspec object into a//a Secretkey objectSecretkeyfactory keyfactory =secretkeyfactory.getinstance (DES); Secretkey SecureKey=Keyfactory.generatesecret (DKS); //The cipher object actually completes the decryption operationCipher Cipher =cipher.getinstance (DES); //Initialize the Cipher object with a keyCipher.init (Cipher.decrypt_mode, SecureKey, SR); //now, get the data and decrypt//formally perform decryption operations        returncipher.dofinal (SRC); }     /*** BASE64 Code * *@paramInfo *@return     */     Public StaticString Base64Encode (byte[] info) {        returnbase64.encodebase64string (info); }     /*** BASE64 Decoding * * *@paramInfo *@return     */     Public Static byte[] Base64decode (String info) {returnbase64.decodebase64 (info); }     /*** The message body is first des encoded and then BASE64 encoded *@paramInfo *@return     */     Public Staticstring Encodeinfo (string info) {Try {            byte[] temp =Encrypt (Info.getbytes (ENCODING), Key.getbytes (ENCODING)); returnBase64Encode (temp); } Catch(Exception e) {e.printstacktrace (); }        return""; }    /*** First BASE64 decoding the message body and then des decoding *@paramInfo *@return     */     Public Staticstring Decodeinfo (string info) {byte[] temp =Base64decode (info); Try {            byte[] buf =Decrypt (temp, key.getbytes (ENCODING)); returnStringutils.newstringutf8 (BUF); } Catch(Exception e) {e.printstacktrace (); }        return""; }      Public Static voidMain (string[] args) {String info= "I am a Chinese, I love the motherland This is the test data without setting no settings"; //Generate ciphertextString Encodeinfo =Encodeinfo (info);        System.out.println (Encodeinfo); //revert to originalString Decodeinfo =Decodeinfo (Encodeinfo);    System.out.println (Decodeinfo); }}

Main Dependency Package: Commons-codec-1.8.jar

Java Server and Android client encryption of messages

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.