BASE64,MD5,SHA,HMAC Encryption and decryption algorithm (Java)

Source: Internet
Author: User
Tags decrypt hmac md5 encryption


 
package com.ice.webos.util.security;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.Key;
import java.security.MessageDigest;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/ **
* <ul>
* <li> The encryption and decryption of BASE64 is two-way, you can find the inverse solution. </ li>
* <li> MD5, SHA and HMAC are one-way encryption. Any data encrypted will only generate a unique encrypted string, which is usually used to verify whether the data has been modified during transmission. </ li>
* <li> HMAC algorithm has a key, which enhances the security during data transmission and strengthens uncontrollable factors outside the algorithm. </ li>
* <li> DES DES-Data Encryption Standard, which is the data encryption algorithm.
* There are three entry parameters for the DES algorithm: Key, Data, and Mode.
* <ul>
* <li> Key: 8 bytes and 64 bits, which is the working key of DES algorithm;
* <li> Data: 8 bytes and 64 bits, which is the data to be encrypted or decrypted; </ li>
* <li> Mode: DES works in two ways: encryption or decryption. </ li>
* </ ul>
* </ li>
* <ul>
*
* @author Ice_Liu
*
* /
public class CryptUtil {
private static final String KEY_MD5 = "MD5";
private static final String KEY_SHA = "SHA";
/ **
* MAC algorithm can choose from 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 HMAC key
*
* @return
* @throws Exception
* /
public static String initMacKey () throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance (KEY_MAC);
SecretKey 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 secretKey = new SecretKeySpec (decryptBASE64 (key), KEY_MAC);
Mac mac = Mac.getInstance (secretKey.getAlgorithm ());
mac.init (secretKey);
return mac.doFinal (data);
}

/ **
* DES algorithm <br>
* Can be replaced with any of the following algorithms, and the size of the key value is changed accordingly.
*
* <pre>
* DES key size must be equal to 56
* DESede (TripleDES) key size must be equal to 112 or 168
* AES key size must be equal to 128, 192 or 256, but 192 and 256 bits may not be available
* Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
* RC2 key size must be between 40 and 1024 bits
* RC4 (ARCFOUR) key size must be between 40 and 1024 bits
* </ pre>
* /
public static final String ALGORITHM = "DES";

/ **
* DES algorithm conversion key <br>
*
* @param key
* @return
* @throws Exception
* /
private static Key toKey (byte [] key) throws Exception {
SecretKey secretKey = null;
if (ALGORITHM.equals ("DES") || ALGORITHM.equals ("DESede")) {
DESKeySpec dks = new DESKeySpec (key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance (ALGORITHM);
secretKey = keyFactory.generateSecret (dks);
} else {
// When using other symmetric encryption algorithms, such as AES, Blowfish, etc., replace the above three lines of code with
secretKey = new SecretKeySpec (key, ALGORITHM);
}
return secretKey;
}

/ **
* DES algorithm decryption
*
* @param data
* @param key
* @return
* @throws Exception
* /
public static byte [] decrypt (byte [] data, String key) throws Exception {
Key k = toKey (decryptBASE64 (key));
Cipher cipher = Cipher.getInstance (ALGORITHM);
cipher.init (Cipher.DECRYPT_MODE, k);
return cipher.doFinal (data);
}

/ **
* DES algorithm encryption
*
* @param data
* @param key
* @return
* @throws Exception
* /
public static byte [] encrypt (byte [] data, String key) throws Exception {
Key k = toKey (decryptBASE64 (key));
Cipher cipher = Cipher.getInstance (ALGORITHM);
cipher.init (Cipher.ENCRYPT_MODE, k);
return cipher.doFinal (data);
}

/ **
* DES algorithm generates keys
*
* @return
* @throws Exception
* /
public static String initKey () throws Exception {
return initKey (null);
}

/ **
* DES algorithm generates keys
*
* @param seed
* @return
* @throws Exception
* /
public static String initKey (String seed) throws Exception {
SecureRandom secureRandom = null;
if (seed! = null) {
secureRandom = new SecureRandom (decryptBASE64 (seed));
} else {
secureRandom = new SecureRandom ();
}
KeyGenerator kg = KeyGenerator.getInstance (ALGORITHM);
kg.init (secureRandom);
SecretKey secretKey = kg.generateKey ();
return encryptBASE64 (secretKey.getEncoded ());
}

public static void main (String [] args) {
try {
String s = "Abocaine's coverage";
String b = CryptUtil.encryptBASE64 (s.getBytes ("UTF-8"));
System.out.println ("Base64 encrypted:" + b);
byte [] c = CryptUtil.decryptBASE64 (b);
System.out.println ("After BASE64 decryption:" + new String (c, "UTF-8"));

c = encryptMD5 (s.getBytes ());
System.out.println ("After MD5 encryption:" + new BigInteger (c) .toString (16));

c = encryptSHA (s.getBytes ());
System.out.println ("After SHA encryption:" + new BigInteger (c) .toString (16));

String key = initMacKey ();
System.out.println ("HMAC key:" + key);
c = encryptHMAC (s.getBytes (), key);
System.out.println ("After HMAC encryption:" + new BigInteger (c) .toString (16));

key = initKey ();
System.out.println (ALGORITHM + "Key: \ t" + key);
c = encrypt (s.getBytes ("UTF-8"), key);
System.out.println (ALGORITHM + "After encryption:" + new BigInteger (c) .toString (16));
c = decrypt (c, key);
System.out.println (ALGORITHM + "After decryption:" + new String (c, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
}




Forwarding http://www.cnblogs.com/liubin0509/archive/2012/01/29/2331066.html


BASE64,MD5,SHA,HMAC Encryption and decryption algorithm (Java)


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.