Java encryption technology One (one-way encryption algorithm) __ algorithm

Source: Internet
Author: User
Tags hmac md5 md5 encryption

</pre>java encryption of the one-way encryption algorithm: Md5,sha,hmac. <p></p><p> MD5 encryption algorithm: </p><p></p><pre code_snippet_id= "1747607" snippet_ File_name= "blog_20160706_2_8502972" name= "code" class= "java" >/**
     * MD5 encryption Algorithm
     * *
     @param data to encrypt
     *
    /public static byte[] Md5encrypt (byte[] data) {
        byte[] b = null;
        try {
            MessageDigest MD = messagedigest.getinstance ("MD5");
            Md.update (data);
            b = md.digest ();
        } catch (NoSuchAlgorithmException e) {
            e.printstacktrace ();
        }
        return b;
    }

Two, SHA encryption algorithm

/**
     * SHA encryption Algorithm
     * *
     @param data to be encrypted by
    /public static byte[] Shaencrypt (byte[] data) {
        byte[] b = null;
        try {
            MessageDigest MD = messagedigest.getinstance ("SHA");
            Md.update (data);
            b = md.digest ();
        } catch (NoSuchAlgorithmException e) {
            e.printstacktrace ();
        }
        return b;
    }
Three, HMAC encryption algorithm

HMAC (hash messages authentication code, hash message discriminator, authentication protocol based on the hash algorithm of the key). The principle of the authentication code is to use the public function and the key to produce a fixed length value as the authentication identification, and use this identity to identify the integrity of the message. Use a key to generate a fixed-size small block of data, the Mac, and add it to the message, and then transfer. The receiver uses the key that is shared with the sender to authenticate the identity.

3.1. Initialize secret key

/**
     * Initialize HMAC secret
     *
     * @return/Public
    static string Inihmackey () {
        string key = "";
        try {
            Keygenerator keygenerator = keygenerator.getinstance (Hmac_key);
            Secretkey Secretkey = Keygenerator.generatekey ();
            Base64encoder encoder = new Base64encoder ();
            Key = Encoder.encode (secretkey.getencoded ());
        } catch (NoSuchAlgorithmException e) {
            e.printstacktrace ();
        }
        Return key;
    }
3.2. Encrypt data with HMAC's secret key
/**
     *
     @param data Encryption
     * @param key key
     * @return
     /public
    static byte[] Hmacencrypt (byte[] Dat A, String key) {
        Secretkey secretkey = new Secretkeyspec (BASE64D (key), hmac_key);
        Byte[] b = null;
        try {
            Mac Mac = Mac.getinstance (Secretkey.getalgorithm ());
            Mac.init (Secretkey);
            b = mac.dofinal (data);

        } catch (NoSuchAlgorithmException e) {
            e.printstacktrace ();
        } catch (InvalidKeyException e) {
            E.printstacktrace ();
        }
        return b;
    }




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.