Directly post code:
PackageCom.segerp.tygl.weixin.common;Importjava.io.UnsupportedEncodingException;Importjava.security.GeneralSecurityException;ImportJava.security.SecureRandom;ImportJavax.crypto.Cipher;ImportJavax.crypto.KeyGenerator;ImportJavax.crypto.SecretKey;ImportJavax.crypto.spec.SecretKeySpec;/*** String encryption and decryption class **/ Public classAesstrutils {//private static final String Secretkey = "f0d7432cfdb62e21f8df70cf47f06948"; Private Static FinalString Secretkey = "Bb6d93b3bd1fb173ddadb78748e0c6d3"; Public Static voidMain (string[] args)throwsException {//EncryptString encryptresultstr = Encryptstr ("123", Secretkey); System.out.println ("After encryption:" +encryptresultstr); //decryptionString Decryptresultstr =decryptstr (Encryptresultstr, Secretkey); System.out.println ("After decryption:" +decryptresultstr); } /*** String Encryption * *@paramSRCSTR * Encrypt String *@paramPassword * Encryption key **/ Public Staticstring Encryptstr (string srcstr, string password) {byte[] Encryptresult =Encryptdata_aes (srcstr, password); String Encryptresultstr=parsebyte2hexstr (Encryptresult); returnEncryptresultstr; } /*** String Decryption * *@paramSrcstr * Decrypting a String *@paramPassword * Encryption key **/ Public Staticstring Decryptstr (string srcstr, String password) {string ReturnValue= ""; Try { byte[] Decryptfrom =Parsehexstr2byte (SRCSTR); byte[] Decryptresult =Decryptdata_aes (decryptfrom, password); ReturnValue=NewString (Decryptresult, "Utf-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } returnreturnvalue; } /*** Convert binary to 16 binary * *@paramBUF *@return */ Private 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 (); } /*** Encryption * *@paramcontent * What you need to encrypt *@paramPassword * Encrypted password *@return */ Private Static byte[] Encryptdata_aes (string content, string password) {Try { //Secretkey secretkey = getKey (password); //byte[] Encodeformat = secretkey.getencoded (); byte[] Encodeformat =parsehexstr2byte (password); Secretkeyspec Key=NewSecretkeyspec (Encodeformat, "AES"); Cipher Cipher= Cipher.getinstance ("AES");//Create a password device byte[] bytecontent = Content.getbytes ("Utf-8"); Cipher.init (Cipher.encrypt_mode, key);//Initialize byte[] result =cipher.dofinal (bytecontent); returnResult//Encrypt}Catch(Exception e) {e.printstacktrace (); } return NULL; } /*** Convert 16 binary to binary * *@paramHexstr *@return */ Private 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; } /*** Generate the key for the specified string * *@paramSecret the character to generate the key *@returnSecretkey the generated key *@throwsgeneralsecurityexception*/ Private StaticSecretkey GetKey (String secret)throwsgeneralsecurityexception {Try{keygenerator _generator= Keygenerator.getinstance ("AES"); SecureRandom SecureRandom= Securerandom.getinstance ("Sha1prng"); Securerandom.setseed (Secret.getbytes ()); _generator.init (128, SecureRandom); return_generator.generatekey (); } Catch(Exception e) {Throw NewRuntimeException ("Initialization key exception occurred"); } } /*** Decryption * *@paramcontent * What to decrypt *@paramPassword * Decryption key *@return */ Private Static byte[] Decryptdata_aes (byte[] content, String password) { Try { //Secretkey secretkey = getKey (password); //byte[] Encodeformat = secretkey.getencoded (); byte[] Encodeformat =parsehexstr2byte (password); Secretkeyspec Key=NewSecretkeyspec (Encodeformat, "AES"); Cipher Cipher= Cipher.getinstance ("AES");//Create a password deviceCipher.init (Cipher.decrypt_mode, key);//Initialize byte[] result =cipher.dofinal (content); returnResult//Encrypt}Catch(Exception e) {e.printstacktrace (); } return NULL; }}
Getkey This method is not used later, because the method that is provided on the web is still going to change the seed generated under Linux.
I was thinking, anyway, to provide the seed, in the Windows environment, generate a key, the generated key as a seed, anyway, the generated key will not change
In the Linux environment to try a bit, it can be done, do it!
AES encryption generates random key solutions under Linux