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