Java Base64, AES, SHA1, MD5 encryption algorithms

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

 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

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.