Java implementation Base64, MD5, MAC, HMAC encryption (RPM)

Source: Internet
Author: User
Tags base64 hmac md5 encryption

Beginning to those basic encryption is not very skilled, and then summed up some, wrote a test: Support Base64, MD5, MAC, HMAC encryption, long story short, we all prefer to understand, look at the code!

The format of the transmission UTF-8 ...

 Packagecodec;Importjava.security.MessageDigest;ImportJavax.crypto.KeyGenerator;ImportJavax.crypto.Mac;ImportJavax.crypto.SecretKey;ImportJavax.crypto.spec.SecretKeySpec;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;/*** Basic Cryptographic components * *@authorvisec Dana *@version1.0 *@since1.0*/ Public Abstract classCoder { Public Static FinalString Key_sha = "SHA";  Public Static FinalString key_md5 = "MD5"; /*** Mac algorithm can choose the following algorithms * * <pre> * HmacMD5 * HmacSHA1 * HmacSHA256 * HmacSHA384 * HmacSHA512 * </pre>*/     Public Static FinalString Key_mac = "HmacMD5"; /*** BASE64 Decryption * *@paramKey *@return     * @throwsException*/     Public Static byte[] decryptBASE64 (String key)throwsException {return(NewBase64decoder ()). Decodebuffer (key); }    /*** BASE64 Encryption * *@paramKey *@return     * @throwsException*/     Public StaticString encryptBASE64 (byte[] key)throwsException {return(NewBase64encoder ()). Encodebuffer (key); }    /*** MD5 Encryption * *@paramData *@return     * @throwsException*/     Public Static byte[] EncryptMD5 (byte[] data)throwsException {messagedigest MD5=messagedigest.getinstance (KEY_MD5);        Md5.update (data); returnmd5.digest (); }    /*** SHA Encryption * *@paramData *@return     * @throwsException*/     Public Static byte[] Encryptsha (byte[] data)throwsException {messagedigest sha=messagedigest.getinstance (Key_sha);        Sha.update (data); returnsha.digest (); }    /*** Initialize HMAC key * *@return     * @throwsException*/     Public StaticString Initmackey ()throwsException {keygenerator keygenerator=keygenerator.getinstance (KEY_MAC); Secretkey Secretkey=Keygenerator.generatekey (); returnencryptBASE64 (secretkey.getencoded ()); }    /*** Mac Encryption * *@paramData *@paramKey *@return     * @throwsException*/     Public Static byte[] Encrypthmac (byte[] data, String key)throwsException {secretkey Secretkey=NewSecretkeyspec (decryptBASE64 (key), KEY_MAC); Mac Mac=mac.getinstance (Secretkey.getalgorithm ());        Mac.init (Secretkey); returnmac.dofinal (data); }}

 Packagecodec;ImportJava.math.BigInteger; Public classCodertest { Public Static voidMain (string[] args)throwsException {codertest.test (); }     Public Static voidTest ()throwsException {String inputstr= "This is some simple cryptographic test"; System.err.println ("Original:" +inputstr); byte[] Inputdata =inputstr.getbytes (); String Code=coder.encryptbase64 (Inputdata); System.err.println ("BASE64 after encryption:" +code); byte[] Output =coder.decryptbase64 (code); String Outputstr=NewString (output); System.err.println ("BASE64 after decryption:" +outputstr); //Unit Test junit4[temporarily not ...]        /**//verify BASE64 encryption and decryption consistency assertequals (INPUTSTR, OUTPUTSTR);         * *//Verify that MD5 is consistent with the same content encryption Assertarrayequals (CODER.ENCRYPTMD5 (inputdata), * coder. ENCRYPTMD5 (Inputdata)); * *//Verify that SHA is consistent with the same content encryption Assertarrayequals (Coder.encryptsha (inputdata), * coder. Encryptsha (input         Data)); * * String key = Coder.initmackey ();         SYSTEM.ERR.PRINTLN ("Mac key:/n" + * key); * *//verify HMAC for the same content, the same key encryption is consistent * Assertarrayequals (Coder.encrypthmac (Inputdata, key), * Coder.enc         Rypthmac (Inputdata, key)); */BigInteger MD5=NewBigInteger (CODER.ENCRYPTMD5 (inputdata)); System.err.println ("MD5:" + md5.tostring (16)); BigInteger sha=NewBigInteger (Coder.encryptsha (inputdata)); System.err.println ("SHA:" + sha.tostring (32)); BigInteger mac=NewBigInteger (Coder.encrypthmac (Inputdata, inputstr)); System.err.println ("HMAC:" + mac.tostring (16)); }}

Output:

Original: This is some simple encryption test BASE64 after encryption:6l+z5piv5lia5lqb566a5y2v55qe5yqg5a+g5rwl6k+VBASE64 After decryption: This is some simple cryptographic test MD5 : 76757e30d128e82b14488b115794d959sha:6f7afslor1oev1k7k40um57cscuqkjtnhmac:782313e944a28a55fc20507e50a9d470 

Java implementation Base64, MD5, MAC, HMAC encryption (RPM)

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.