Java encryption Technology (i)--base64 and one-way encryption algorithm Md5&sha&mac

Source: Internet
Author: User
Tags base64 hmac md5 encryption asymmetric encryption



Basic one-way encryption algorithm:



BASE64 strictly speaking, it belongs to the encoding format, not the encryption algorithm



MD5 (Message Digest algorithm 5, Information Digest algorithm)



SHA (Secure Hash algorithm, security hashing algorithm)



HMAC (Hash message authentication code, hash messages authentication code)



Complex symmetric encryption (DES, PBE), asymmetric encryption algorithms:



DES (Data Encryption standard, encryption algorithm)



PBE (password-based encryption, password-based authentication)



RSA (the name of the algorithm is named after the inventor: Ron Rivest, Adishamir and Leonard Adleman)



DH (Diffie-hellman algorithm, key-consistent protocol)



DSA (digital Signature algorithm, digitally signed)



ECC (Elliptic Curves cryptography, Elliptic curve cipher coding)



The following is a brief introduction to BASE64, MD5, SHA, and HMAC in several ways:



MD5, SHA, HMAC these three kinds of encryption algorithms, can be described as non-reversible encryption, is not decrypted encryption method. We usually only use them as the basis for encryption. Simple encryption of more than three kinds is not reliable.



BASE64



As defined by RFC2045, Base64 is defined as: the Base64 content transfer encoding is designed to describe an arbitrary sequence of 8-bit bytes as a form that is not easily recognizable by humans.



(The Base64 content-transfer-encoding is designed to represent arbitrary sequences of octets in a form that need not being HU Manly readable.)



Common in mail, HTTP encryption, interception of HTTP information, you will find the login operation of the user name, password field by BASE64 encryption.



Java code is implemented as follows (JDK native implementation, third party self-implementation):

1 /**
 2 * BASE64 encryption
 3 * @param key
 4 * @return
 5 * /
 6 public static String encryptBASE64 (byte [] key) {
 7 BASE64Encoder base64Encoder = new BASE64Encoder ();
 8 String encode = base64Encoder.encode (key);
 9 return encode;
10}
11 / **
12 * BASE64 decryption
13 * @param key
14 * @return
15 * @throws Exception
16 * /
17 public static byte [] decryptBASE64 (String key) throws Exception {
18 BASE64Decoder base64Decoder = new BASE64Decoder ();
19 byte [] decodeBuffer = base64Decoder.decodeBuffer (key);
20 return decodeBuffer;
twenty one     }
Mainly BASE64Encoder, BASE64Decoder two categories, we only need to know to use the corresponding method. In addition, the number of bytes generated after BASE encryption is a multiple of 8, if there are not enough digits, the = sign is used.

MD5

MD5-abbreviation of message-digest algorithm 5 (message-digest algorithm), widely used in encryption and decryption technology, often used in file verification check? No matter how big the file is, after MD5 can generate a unique MD5 value. Like now

ISO verification is MD5 verification. how to use? Of course, the value of MD5 is generated after passing ISO to MD5. Generally friends who download linux-ISO have seen the MD5 string next to the download link. It is used to verify whether the files are consistent.

The following is achieved through java code:

 1 /**
 2 * MD5 encryption
 3 * @param data
 4 * @return
 5 * @throws Exception
 6 * /
 7 public static byte [] encryptMD5 (byte [] data) throws Exception {
 8 MessageDigest md5 = MessageDigest.getInstance ("MD5");
 9 md5.update (data);
10 byte [] result = md5.digest ();
11 return result;
12}
Usually we don't directly use the above MD5 encryption. Usually, the byte array generated by MD5 is handed over to BASE64 and encrypted to obtain the corresponding character string.

SHA

SHA (Secure Hash Algorithm), an important tool in cryptographic applications such as digital signatures, is widely used in e-commerce and other information security fields. Although both SHA and MD5 were cracked by the collision method, but

SHA is still recognized as a secure encryption algorithm and is more secure than MD5.

The following is achieved through java code:

 1 /**
 2 * SHA encryption
 3 * @param data
 4 * @return
 5 * @throws Exception
 6 * /
 7 public static byte [] encryptSHA (byte [] data) throws Exception {
 8 MessageDigest sha = MessageDigest.getInstance ("SHA");
 9 sha.update (data);
10 byte [] result = sha.digest ();
11 return result;
12}
HMAC

HMAC (Hash Message Authentication Code, Hash Message Authentication Code, Hash algorithm authentication protocol based on key. The principle of message authentication code is to use public function and key to generate a fixed length value as authentication

Certification mark, which is used to identify the integrity of the message. Use a key to generate a fixed-size small data block, that is, MAC, and add it to the message, and then transmit it. The receiver uses the shared key with the sender for authentication and authentication.

The following is achieved through java code:

 1 /**
 2 * Initialize HMAC key
 3 * @return
 4 * @throws Exception
 5 * /
 6 public static String initMacKey () throws Exception {
 7 KeyGenerator keyGenerator = KeyGenerator.getInstance (KEY_MAC);
 8 SecretKey secretKey = keyGenerator.generateKey ();
 9 String initKey = encryptBASE64 (secretKey.getEncoded ());
10 return initKey;
11}
12 / **
13 * HMAC encryption
14 * @param data
15 * @param key
16 * @return
17 * @throws Exception
18 * /
19 public static byte [] encryptHMAC (byte [] data, String key) throws Exception {
20 SecretKey secretKey = new SecretKeySpec (decryptBASE64 (key), KEY_MAC);
21 Mac mac = Mac.getInstance (secretKey.getAlgorithm ());
22 mac.init (secretKey);
23 byte [] res = mac.doFinal (data);
24 return res;
25}
BASE64Encoder and BASE64Decoder are unofficial JDK implementation classes. Although it can be found and used in the JDK, it cannot be found in the API. The classes of the sun and com.sun packages in the JRE are not documented, they are

Based on the foundation of java, javax class libraries, most of the implementations are related to the underlying platform and are generally not recommended.

 

The encryption and decryption of BASE64 is bidirectional, and it can be reversed.

MD5, SHA, and HMAC are one-way encryption. After data is encrypted, only one encrypted string is generated, which is usually used to check whether the data is modified during transmission. The HMAC algorithm has a key, which enhances the data transmission process

The security of the system strengthens the uncontrollable factors outside the algorithm.

The purpose of one-way encryption is mainly to verify whether the data is modified during transmission.

 

Java encryption technology (1)-BASE64 and one-way encryption algorithm MD5 & SHA & MAC

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.