Java Base64, AES, SHA1, MD5 encryption algorithms

Source: Internet
Author: User
Tags decrypt md5 digest md5 encryption sha1

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.