Java Base64, AES, SHA1, MD5 encryption algorithms

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

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 class decripttest {      PUBLIC&NBSP;STATIC&NBSP;STRING&NBSP;SHA1 (String decript)  {         try {            MessageDigest  digest = java.security.messagedigest                  &Nbsp;  .getinstance ("SHA-1");             digest.update (Decript.getbytes ());             byte messagedigest[] = digest.digest ();             // Create Hex String             stringbuffer hexstring = new stringbuffer ();             //  byte array converted to   hexadecimal   number              for  (int i = 0; i <  messagedigest.length; i++)  {                 string shahex = integer.tohexstring (messagedigest[i] &  0xFF);                 if  (ShaHex.length ()  < 2)  {                     hexstring.append (0);                 }                 hexstring.append (Shahex);             }             return hexstring.tostring ();          } catch  (Nosuchalgorithmexception e)  {            e.printstacktrace ();         }        return  "";     }     public static string sha (String decript)  {         try {             MessageDigest digest = java.security.MessageDigest                     .getinstance ("SHA");             digest.update (Decript.getBytes ());             byte messageDigest[] =  Digest.digest ();            // create  hex string            stringbuffer  Hexstring = new stringbuffer ();             //  byte array conversion to   hex &NBSP; number             for  (int i =  0; i < messagedigest.length; i++)  {                 string shahex = integer.tohexstring ( MESSAGEDIGEST[I]&NBSP;&AMP;&NBSP;0XFF);                 if  (Shahex.length ()  < 2)  {                     hexstring.append (0);                 }                 hexstring.append (SHAHEX);             }              return hexstring.tostring ();          } catch   (nosuchalgorithmexception e)  {             e.printstacktrace ();        }         return  "";    }     public  STATIC&NBSP;STRING&NBSP;MD5 (string input)  {        try  {            //  obtaining the  messagedigest of MD5 Digest algorithm   Object             messagedigest mdinst  = messagedigest.getinstance ("MD5");             //  Update summary with the specified bytes              Mdinst.update (Input.getbytes ());             //  Get ciphertext              byte[] md = mdinst.digest ();             //  convert ciphertext to 16 binary string form              stringbuffer hexstring = new stringbuffer ();             //  byte array converted to   hexadecimal   number              for  (int i = 0; i <  md.length; i++)  {                 string shahex = integer.tohexstring (MD[I]&NBSP;&AMP;&NBSP;0XFF);                 if  ( Shahex.length ()  < 2)  {                     hexstring.append (0);                 }                 hexstring.append (Shahex);            }             return hexstring.tostring ();         } catch  (nosuchalgorithmexception e)  {             e.printstacktrace ();         }        return  "";     }     /**     *  Encryption      *      *  @param   content     *              content that needs to be encrypted      *  @param   password      *             Encryption Password       *  @return       */    public  static byte[] encryptaes (String content, string password)  {         try {             keygenerator kgen = keygenerator.getinstance ("AES");             kgen.init (128, new securerandom (Password.getBytes ()));             SecretKey secretKey =  Kgen.generatekey ();             byte[] encodeformat =  Secretkey.getencoded ();             secretkeyspec  key = new secretkeyspec (encodeformat,  "AES");             cipher cipher = cipher.getinstance ("AES");//  Creating a cipher             byte[] byteContent =  Content.getbytes ("Utf-8");             cipher.init ( Cipher.encrypt_mode, key);//  Initialization              byte[] result = cipher.dofinal (bytecontent);             return result; //  Encryption          } 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       *     *  @param   content     *              What to decrypt      *   @param   password     *              decryption keys      *  @return      */     public static byte[] decryptaes (byte[] content, string  Password)  {        try {              keygenerator kgen = keygenerator.getinstance ("AES");             kgen.init (128, new securerandom (Password.getBytes ()));             SecretKey secretKey =  Kgen.generatekey ();            byte[]  encodeformat = secretkey.getencoded ();             secretkeyspec key = new secretkeyspec (encodeformat,  "AES");             cipher cipher = cipher.getinstance ( "AES");//  Create the cipher             cipher.init ( Cipher.decrypt_mode, key);//  Initialization              byte[] result = cipHer.dofinal (content);            return  result; //  Encryption         } 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 static string decryptbase64 (String key)  {          return  "";    }     &NBSP;/**&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;BASE64 Encryption      *      *  @param  key     *  @return      * @ Throws exception&nbSp;    */    public static string encryptbase64 (String  key)  {         return  ";     }}

Java Base64, AES, SHA1, MD5 encryption algorithm

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.