Base64 and one-way encryption algorithm MD5 & Sha & Mac

Source: Internet
Author: User
Tags hmac asymmetric encryption

To put it bluntly, here we mainly describe some encryption and decryption algorithms implemented by Java, and finally introduce digital certificates. For example, the basic one-way encryption algorithm:

Base64 strictly speaking, it belongs to the encoding format, rather than the MD5 (Message Digest algorithm 5, information digest algorithm) Sha (secure hash algorithm, Security Hash Algorithm) HMAC (Hash Message Authentication code, hash message authentication code)

Complex Symmetric encryption (DES, PBE) and asymmetric encryption algorithms:

Des (Data Encryption Standard, data encryption algorithm) PBE (password-based encryption, password-based verification) RSA (algorithm name: Ron Rivest, adishamir and Leonard Adleman) DH (Diffie-Hellman algorithm, key consistency protocol) DSA (digital signature algorithm, digital signature) ECC (elliptic curves cryptography, Elliptic Curve Cryptography)

This article briefly introduces several methods of base64, MD5, Sha, and HMAC. MD5, Sha, and HMAC encryption algorithms are non-reversible encryption methods that cannot be decrypted. We usually only use them as the basis for encryption. The preceding three types of encryption are not reliable.

Base64, according to rfc2045, is defined as base64 content Transfer Encoding. It is designed to describe the 8-bit bytes of any sequence as a form that is not easily recognized by people. (The base64 content-transfer-encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable .) common in mail and HTTP encryption, intercepting HTTP information, you will find that the username and password fields for login operations are encrypted by base64.

/*** Base64 decryption ** @ Param key * @ return * @ throws exception */public static byte [] decryptbase64 (string key) throws exception {return (New base64decoder ()). decodebuffer (key );}

/*** Base64 encryption ** @ Param key * @ return * @ throws exception */public static string encryptbase64 (byte [] key) throws exception {return (New base64encoder ()). encodebuffer (key );}

 

The main classes are base64encoder and base64decoder. We only need to know how to use them. In addition, the number of BITs generated after base encryption is a multiple of 8. If the number of BITs is not enough, fill the bits with =.

MD5-message-Digest algorithm 5 (Information-Digest algorithm) is widely used in encryption and decryption technologies and is often used for file verification. Verification? No matter how large the file is, the unique MD5 value can be generated after MD5. For example, the current ISO verification is MD5 verification. How to use it? Of course, the ISO is generated after MD5. A friend who downloads Linux-ISO has seen the MD5 string next to the download link. Is used to verify whether the files are consistent.

 

/*** MD5 encryption **

@ Param data

* @ Return * @ throws exception */

Public static byte [] encryptmd5 (byte [] data) throws exception {

Messagedigest MD5 = messagedigest. getinstance (key_md5); md5.update (data );

Return md5.digest ();

}

 

 

We usually do not directly use the above MD5 encryption. Usually, the byte array generated by MD5 is sent to base64 and then encrypted to obtain the corresponding string.

Sha (secure hash algorithm, Security Hash Algorithm), digital signatures, and other important cryptographic tools are widely used in information security fields such as e-commerce. Although Sha and MD5 are cracked through the collision method, Sha is still recognized as a secure encryption algorithm, which is more secure than MD5.

 

/** <Span style = "white-space: pre"> </span>

* Sha encryption <span style = "white-space: pre"> </span>

* & Nbsp; <span style = "white-space: pre"> </span>

* @ Param data <span style = "white-space: pre"> </span>

* @ Return <span style = "white-space: pre"> </span>

* @ Throws exception <span style = "white-space: pre"> </span>

*/<Span style = "white-space: pre"> </span>

Public static byte [] encryptsha (byte [] data) throws exception {

<Span style = "white-space: pre">

</Span> messagedigest Sha = messagedigest. getinstance (key_sha );

<Span style = "white-space: pre"> </span> Sha. Update (data );

<Span style = "white-space: pre"> </span> return Sha. Digest ();

<Span style = "white-space: pre"> </span>

}

}

 

HMAC (Hash Message Authentication code, hash message authentication code, and key-based hash algorithm authentication protocol. The message authentication code uses a public function and a key to generate a fixed-length value as the authentication identifier, which identifies the integrity of the message. Use a key to generate a small data block of a fixed size, that is, Mac, and add it to the message for transmission. The receiver uses the key shared with the sender for authentication.

 

/*** Initialize the HMAC key *

* @ Return * @ throws exception */

Public static string initmackey () throws exception {

Keygenerator = keygenerator. getinstance (key_mac );

Secretkey = keygenerator. generatekey (); Return encryptbase64 (secretkey. getencoded ());

}

/*** HMAC encryption *

* @ Param data *

@ Param key

* @ Return * @ throws exception */

Public static byte [] encrypthmac (byte [] data, string key) throws exception {

Secretkey = new secretkeyspec (decryptbase64 (key), key_mac); Mac = Mac. getinstance (secretkey. getalgorithm (); Mac. INIT (secretkey );

Return Mac. dofinal (data );

}

 

 

 

 

Import java. Security. messagedigest;

Import javax. crypto. keygenerator;

Import javax. crypto. Mac;

Import javax. crypto. secretkey;

Import sun. Misc. base64decoder;

Import sun. Misc. base64encoder;

/*** Basic encryption component *

* @ Author liangdong *

@ Version 1.0

* @ Since 1.0 */

Public abstract class coder {

Public static final string key_sha = "Sha ";

Public static final string key_md5 = "MD5 ";

/*** The MAC Algorithm can be any of the following algorithms *

* <PRE> * hmacmd5 * hmacsha1 * hmacsha256 * hmacsha384 * hmacsha512 * </PRE> */

Public static final string key_mac = "hmacmd5 ";

/*** Base64 decryption ** @ Param key * @ return * @ throws exception */

Public static byte [] decryptbase64 (string key) throws exception {

Return (New base64decoder (). decodebuffer (key );

}

/*** Base64 encryption ** @ Param key * @ return * @ throws exception */

Public static string encryptbase64 (byte [] Key) throws exception {

Return (New base64encoder (). encodebuffer (key );

}

/* ** MD5 encryption ** @ Param data * @ return * @ throws exception */

Public static byte [] encryptmd5 (byte [] data) throws exception {

Messagedigest MD5 = messagedigest. getinstance (key_md5 );

Md5.update (data); Return md5.digest ();

}

/*** Sha encryption *

* @ Param data * @ return * @ throws exception */

Public static byte [] encryptsha (byte [] data) throws exception {

Messagedigest Sha = messagedigest. getinstance (key_sha );

Sha. Update (data); Return Sha. Digest ();

}

/*** Initialize the HMAC key *

* @ Return * @ throws exception */

Public static string initmackey () throws exception {

Keygenerator = keygenerator. getinstance (key_mac );

Secretkey = keygenerator. generatekey ();

Return encryptbase64 (secretkey. getencoded ());

}

/*** HMAC encryption *

* @ Param data * @ Param key * @ return * @ throws exception */

Public static byte [] encrypthmac (byte [] data, string key) throws exception {

Secretkey = new secretkeyspec (decryptbase64 (key), key_mac );

Mac = Mac. getinstance (secretkey. getalgorithm ());

Mac. INIT (secretkey); Return Mac. dofinal (data );

}

}

 

Import static org. JUnit. Assert .*;

Import org. JUnit. test;

/***** @ Author liangdong * @ version 1.0 * @ since 1.0 */public class codertest {

@ Test public void test () throws exception {string inputstr = "simple encryption"; system. Err. println ("Original: \ n" + inputstr );

Byte [] inputdata = inputstr. getbytes (); string code = coder. encryptbase64 (inputdata );

System. Err. println ("base64 encrypted: \ n" + Code );

Byte [] Output = coder. decryptbase64 (CODE );

String outputstr = new string (output );

System. Err. println ("after base64 decryption: \ n" + outputstr );

// Verify base64 encryption/Decryption consistency assertequals (inputstr, outputstr );

// Verify that MD5 encryption for the same content is consistent with assertarrayequals (CODER. encryptmd5 (inputdata), coder. encryptmd5 (inputdata ));

// Verify whether Sha encrypts the same content in the same assertarrayequals (CODER. encryptsha (inputdata), coder. encryptsha (inputdata ));

String key = coder. initmackey (); system. Err. println ("Mac key: \ n" + key );

// Verify that HMAC encrypts the same key for the same content in the same assertarrayequals (CODER. encrypthmac (inputdata, Key), coder. encrypthmac (inputdata, key ));

Biginteger MD5 = new biginteger (CODER. encryptmd5 (inputdata); system. Err. println ("MD5: \ n" + md5.tostring (16 ));

Biginteger Sha = new biginteger (CODER. encryptsha (inputdata); system. Err. println ("Sha: \ n" + Sha. tostring (32 ));

Biginteger MAC = new biginteger (CODER. encrypthmac (inputdata, inputstr); system. Err. println ("HMAC: \ n" + Mac. tostring (16 ));}}

 

Original article: simple encrypted base64: 566a5y2v5yqg5a + G

After base64 decryption: simple Mac encryption key: ugxdhc + 6 ylrdaik ++ leftgwimbuyuj6mqhwyhsgf4trvkvbbsqvy/a22xu8xt1ruemdcww155bke pbipkd7qhg =

MD5:-550b4d90349ad4629462113e7934de56 Sha: 91k9vo7p400cjkgfhjh0ia9qthsjagfn HMAC: 2287d192387e94254bdbba2fa941009a

Base64 and one-way encryption algorithm MD5 & Sha & Mac

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.