PackageCom.example.decript;Importjava.io.UnsupportedEncodingException;Importjava.security.InvalidKeyException;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;Importjavax.crypto.BadPaddingException;ImportJavax.crypto.Cipher;Importjavax.crypto.IllegalBlockSizeException;ImportJavax.crypto.KeyGenerator;Importjavax.crypto.NoSuchPaddingException;ImportJavax.crypto.SecretKey;ImportJavax.crypto.spec.SecretKeySpec; Public classDecripttest { Public Staticstring SHA1 (String decript) {Try{MessageDigest Digest=java.security.MessageDigest. getinstance ("SHA-1"); Digest.update (Decript.getbytes ()); byteMessagedigest[] =digest.digest (); //Create Hex StringStringBuffer hexstring =NewStringBuffer (); //convert byte array to hexadecimal number for(inti = 0; i < messagedigest.length; i++) {String Shahex= Integer.tohexstring (Messagedigest[i] & 0xFF); if(Shahex.length () < 2) {hexstring.append (0); } hexstring.append (Shahex); } returnhexstring.tostring (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } return""; } Public Staticstring SHA (String decript) {Try{MessageDigest Digest=java.security.MessageDigest. getinstance ("SHA"); Digest.update (Decript.getbytes ()); byteMessagedigest[] =digest.digest (); //Create Hex StringStringBuffer hexstring =NewStringBuffer (); //convert byte array to hexadecimal number for(inti = 0; i < messagedigest.length; i++) {String Shahex= Integer.tohexstring (Messagedigest[i] & 0xFF); if(Shahex.length () < 2) {hexstring.append (0); } hexstring.append (Shahex); } returnhexstring.tostring (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } return""; } Public Staticstring MD5 (String input) {Try { //MessageDigest object for obtaining the MD5 digest algorithmMessageDigest mdinst = messagedigest.getinstance ("MD5"); //updates the digest with the specified bytesmdinst.update (Input.getbytes ()); //Get ciphertext byte[] MD =mdinst.digest (); //convert ciphertext to 16-binary string formStringBuffer hexstring =NewStringBuffer (); //convert byte array to hexadecimal number for(inti = 0; i < md.length; i++) {String Shahex= Integer.tohexstring (Md[i] & 0xFF); if(Shahex.length () < 2) {hexstring.append (0); } hexstring.append (Shahex); } returnhexstring.tostring (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } return""; } /*** Encryption * *@paramcontent * What you need to encrypt *@paramPassword * Encrypted password *@return */ Public Static byte[] Encryptaes (string content, string password) {Try{keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init (128,NewSecureRandom (Password.getbytes ())); Secretkey Secretkey=Kgen.generatekey (); byte[] Encodeformat =secretkey.getencoded (); 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(nosuchalgorithmexception e) {e.printstacktrace (); } Catch(nosuchpaddingexception e) {e.printstacktrace (); } Catch(InvalidKeyException e) {e.printstacktrace (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(illegalblocksizeexception e) {e.printstacktrace (); } Catch(badpaddingexception e) {e.printstacktrace (); } return NULL; } /*** Decryption * *@paramcontent * What to decrypt *@paramPassword * Decryption key *@return */ Public Static byte[] Decryptaes (byte[] content, String password) { Try{keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init (128,NewSecureRandom (Password.getbytes ())); Secretkey Secretkey=Kgen.generatekey (); byte[] Encodeformat =secretkey.getencoded (); 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(nosuchalgorithmexception e) {e.printstacktrace (); } Catch(nosuchpaddingexception e) {e.printstacktrace (); } Catch(InvalidKeyException e) {e.printstacktrace (); } Catch(illegalblocksizeexception e) {e.printstacktrace (); } Catch(badpaddingexception e) {e.printstacktrace (); } return NULL; } /*** BASE64 Decryption * *@paramKey *@return * @throwsException*/ Public Staticstring decryptBASE64 (String key) {return""; } /*** BASE64 Encryption * *@paramKey *@return * @throwsException*/ Public Staticstring encryptBASE64 (String key) {return""; }}
Java Base64, AES, SHA1, MD5 encryption algorithms