Reprint Please specify http://blog.csdn.net/ly20116/article/details/51025437
Category:
1. One-way encryption algorithm
One-way encryption is non-reversible encryption, which is a non-decrypted encryption method.
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, safe hash Algorithm)
HMAC (Hash message authentication code, hash information authentication code)
2. Complex symmetric encryption (DES, PBE), asymmetric encryption algorithm
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)
This article details BASE64 encoding decoding, MD5,SHA,SHA-1,HMAC encryption.
I. BASE64 encoding and decoding
Base64 is one of the most common encoding methods for transmitting 8Bit bytes of code on the network. 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. Common in mail, HTTP encryption, interception of HTTP information, you will find the login operation of the user name, password field by BASE64 encryption.
Algorithm:
/** * BASE64 decoding * * @param key * @return * @throws Exception */ Public Static byte[]decryptBASE64(String key)throwsException {return(NewBase64decoder ()). Decodebuffer (key); }/** * BASE64 code * * @param key * @return * @throws Exception */ Public StaticStringencryptBASE64(byte[] key)throwsException {return(NewBase64encoder ()). Encodebuffer (key); }
The main is Base64encoder, base64decoder two classes, we just need to know how to use the corresponding method. Also, the number of bytes generated after base encryption is a multiple of 8 if the number of digits is not enough to be populated with the = sign.
Second, MD5
MD5 Message-digest Algorithm 5 (Information-Digest algorithm 5)
The MD5 algorithm has the following characteristics:
1, compressibility: Any length of data, calculated the length of the MD5 value is fixed.
2, easy to calculate: It is easy to calculate the MD5 value from the original data.
3, anti-modification: Any changes to the original data, even if only 1 bytes modified, the resulting MD5 value is very different.
4, strong anti-collision: The known raw data and its MD5 value, want to find a data with the same MD5 value (that is, forgery data) is very difficult
The role of MD5:
Allow bulk information to be "compressed" into a confidential format before signing a private key with a digital signature software (that is, converting an arbitrary-length byte string into a long hexadecimal string), MD5 is widely used in encryption and decryption techniques, often used for file verification.
Algorithm:
/** * MD5 Encryption * * @param data * @return */ Public StaticStringencryptMD5(String data) {String str=NULL;Try{//Get the MessageDigest object of MD5 Digest algorithmMessageDigest Md5=messagedigest.getinstance ("MD5");//Update summary with specified bytesMd5.update (Data.getbytes ());//Get ciphertext byte[] Bytes=md5.digest ();The //byte array is converted to 16 binary string, the method content is in the lastStr=bytestohexstring (bytes); }Catch(NoSuchAlgorithmException e) {//TODO auto-generated catch blockE.printstacktrace (); }returnStr }
Third, SHA
SHA (Secure Hash algorithm, secure Hash Algorithm), digital signature and other cryptographic applications of important tools, is widely used in e-commerce and other information security fields
Although both Sha and MD5 have been cracked by the collision method, Sha is still recognized as a secure cryptographic algorithm that is more secure than MD5.
Algorithm:
/** * SHA encryption * @param data * @return */ Public StaticStringEncryptsha(String data) {String str=NULL;Try{//Get MessageDigest object for Sha Digest algorithmMessageDigest Sha=messagedigest.getinstance ("SHA");//Update summary with specified bytesSha.update (Data.getbytes ());//Get ciphertext byte[] Bytes=sha.digest ();The //byte array is converted to 16 binary string, the method content is in the lastStr=bytestohexstring (bytes); }Catch(NoSuchAlgorithmException e) {//TODO auto-generated catch blockE.printstacktrace (); }returnStr }
SHA-1:
SHA-1 is a data encryption algorithm, the idea of the algorithm is to receive a piece of plaintext, and then in an irreversible way to convert it into a paragraph (usually smaller) ciphertext, can also be easily understood as a string of input code (called Pre-mapping or information), and convert them to a shorter length, A fixed number of bits of output sequence is the process of hashing values (also known as information digests or information authentication codes).
Algorithm:
/** * SHA1 encryption * @param data * @return */ Public StaticStringencryptSHA1(String data) {String str=NULL;Try{//Get MessageDigest object for Sha Digest algorithmMessageDigest Sha1=messagedigest.getinstance ("SHA-1");//Update summary with specified bytesSha1.update (Data.getbytes ());//Get ciphertext byte[] Bytes=sha1.digest ();The //byte array is converted to 16 binary string, the method content is in the lastStr=bytestohexstring (bytes); }Catch(NoSuchAlgorithmException e) {//TODO auto-generated catch blockE.printstacktrace (); }returnStr }
comparison of SHA-1 and MD5:
Because both are exported by MD4, SHA-1 and MD5 are very similar to each other. Correspondingly, their strength and other characteristics are similar, but there are several differences:
• Security for brute force attacks: the most significant and important difference is that the SHA-1 digest is 32 bits longer than the MD5 digest. Using the brute force technique, generating any message to make its digest equal to the difficulty of a given report digest is a 2^128 order of magnitude, while for SHA-1 it is a 2^160 order of magnitude of operation. MD5 In this way, the SHA-1 has greater strength for brute force attacks.
Security of password Analysis: Because of the MD5 design, vulnerable to password analysis attacks, SHA-1 appear to be vulnerable to such attacks.
L Speed: On the same hardware, the SHA-1 runs slower than MD5.
d. HMAC
HMAC (Hash message authentication code, hash messages identification code, cryptographic hash algorithm based on the key authentication protocol. The principle of authentication of message identification code is to use public functions and keys to generate a fixed-length value as a certification identifier, which is used to identify the integrity of the message. Use a key to generate a small, fixed-size block of data, the Mac, and add it to the message and transfer it. The receiver uses the key shared with the sender for authentication, and so on.
Algorithm:
/** * Initialize HMAC key * * @return * @throws Exception */ Public StaticStringInitmackey()throwsException {Keygenerator keygenerator = keygenerator.getinstance (KEY_MAC); Secretkey Secretkey = Keygenerator.generatekey ();returnEncryptBASE64 (secretkey.getencoded ()); }/** * HMAC Encryption * * @param data * @param key * @return * @throws /c4> Exception * / 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); }
Five, byte array converted to 16 binary string
/** * byte array to 16 binary string * @param bytes * @return * * Public StaticStringbytestohexstring(byte[] bytes) {StringBuffer sb=NewStringBuffer ();if(bytes==NULL|| bytes.length<=0){return NULL; } for(intI=0; i<bytes.length;i++) {inttemp=bytes[i]&0xFF;//and operations, converting byte to integerString hex=integer.tohexstring (temp);//int conversion to 16-binary string if(Hex.length () <2) {Sb.append (0); } sb.append (hex); }returnSb.tostring (); }
Summarize:
The encryption and decryption of BASE64 is bidirectional and can be reverse-solved.
MD5, Sha, and HMAC are one-way encryption, and any data that is encrypted will only produce a single encrypted string, often used to verify that the data has been modified during transmission. The HMAC algorithm has a key, enhances the security in the data transmission process, and strengthens the uncontrolled factor outside the algorithm.
One-way encryption is used primarily to verify that data is modified during transmission.
Java Common encryption algorithm