Public classDes {/*** Encrypt the given string with the specified encoding and key *@paramSRCSTR string to encrypt *@paramCharSet character set, such as UTF8 *@paramSKey Key*/ Public Staticstring Encrypt (String srcstr, Charset Charset, String sKey) {byte[] src =srcstr.getbytes (CharSet); byte[] buf =des.encrypt (SRC, sKey); returndes.parsebyte2hexstr (BUF); } /*** Decryption of a given cipher with the specified encoding and key *@paramHexstr need to decrypt the ciphertext *@paramCharSet Character Set *@paramSKey Key *@returnthe original text after decryption *@throwsException*/ Public StaticString Decrypt (String hexstr, Charset Charset, String sKey)throwsException {byte[] src =Des.parsehexstr2byte (HEXSTR); byte[] buf =des.decrypt (SRC, sKey); return NewString (buf, CharSet); } Public Static byte[] Encrypt (byte[] data, String SKey) { Try { byte[] key =skey.getbytes (); Ivparameterspec IV=NewIvparameterspec (key); Deskeyspec Deskey=NewDeskeyspec (key); Secretkeyfactory keyfactory= Secretkeyfactory.getinstance ("DES"); Secretkey SecureKey=Keyfactory.generatesecret (Deskey); Cipher Cipher= Cipher.getinstance ("des/cbc/pkcs5padding"); Cipher.init (Cipher.encrypt_mode, SecureKey, iv); returncipher.dofinal (data); } Catch(Throwable e) {e.printstacktrace (); } return NULL; } /*** Decryption *@paramsrc *@paramSKey *@return * @throwsException*/ Public Static byte[] Decrypt (byte[] src, String sKey)throwsException {byte[] key =skey.getbytes (); //initialization VectorIvparameterspec IV =NewIvparameterspec (key); //Create a Deskeyspec objectDeskeyspec Deskey =NewDeskeyspec (key); //Create a key factorySecretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); //convert Deskeyspec objects to Secretkey objectsSecretkey SecureKey =Keyfactory.generatesecret (Deskey); //The cipher object actually completes the decryption operationCipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding"); //Initialize the Cipher object with a keyCipher.init (Cipher.decrypt_mode, SecureKey, iv); //actually start the decryption operation returncipher.dofinal (SRC); } /*** Convert binary to 16 binary * *@paramBUF *@return */ Public StaticString Parsebyte2hexstr (bytebuf[]) {StringBuffer SB=NewStringBuffer (); for(inti = 0; i < buf.length; i++) {String hex= Integer.tohexstring (Buf[i] & 0xFF); if(hex.length () = = 1) {hex= ' 0 ' +Hex; } sb.append (Hex.touppercase ()); } returnsb.tostring (); } /*** Convert 16 binary to binary * *@paramHexstr *@return */ Public Static byte[] Parsehexstr2byte (String hexstr) {if(Hexstr.length () < 1)return NULL; byte[] result =New byte[Hexstr.length ()/2]; for(inti = 0; I < Hexstr.length ()/2; i++) { intHigh = Integer.parseint (Hexstr.substring (i * 2, I * 2 + 1), 16); intLow = Integer.parseint (Hexstr.substring (i * 2 + 1, I * 2 + 2), 16); Result[i]= (byte) (High * 16 +Low ); } returnresult; }}
Use:
@RequestMapping ("/encode") public string encode (string data) { return Des.encrypt (data, Charset.forname ("UTF8"), "Testtest"); } @RequestMapping ("/decode") publicthrows exception{ return Des.decrypt (data, Charset.forname ("UTF8"), "Testtest"); }
This is not the point of use in the controller of the sprint boot, the point is:
Des.encrypt (data, Charset.forname ("UTF8"), "testtest");
Des.decrypt (data, Charset.forname ("UTF8"), "testtest");
The invocation of the above two methods.
Reference from: https://www.cnblogs.com/james0/p/7063941.html
Java DES encryption and decryption