Http://www.java2s.com/Code/Java/Security/CatalogSecurity.htm
Java Base64, AES, SHA1, MD5 encryption algorithms
Package com.example.decript; Import Java.io.unsupportedencodingexception;import Java.security.invalidkeyexception;import Java.security.messagedigest;import Java.security.nosuchalgorithmexception;import Java.security.SecureRandom; Import Javax.crypto.badpaddingexception;import Javax.crypto.cipher;import javax.crypto.IllegalBlockSizeException; Import Javax.crypto.keygenerator;import Javax.crypto.nosuchpaddingexception;import Javax.crypto.secretkey;import Javax.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 ""; } /** * Encrypt * * @param content * requires encrypted contents * @param password * encrypted password * @retu RN*/ Public Static byte[] Encryptaes (string content, string password) {Try{keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init ( -,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; } /** * Decrypt * * @param content * to decrypt contents * @param password * Decryption key * @return */ Public Static byte[] Decryptaes (byte[] content, String password) { Try{keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init ( -,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 * * @param key * @return * @throws Exception*/ Public Staticstring decryptBASE64 (String key) {return ""; } /** * BASE64 Encryption * * @param key * @return * @throws Exception*/ Public Staticstring encryptBASE64 (String key) {return ""; }}
Java Base64, AES, SHA1, MD5 encryption algorithms