加密解密
言歸正傳
如基本的單向加密算法
● BASE
● MD
● SHA(Secure Hash Algorithm
● HMAC(Hash Message Authentication Code
復雜的對稱加密(DES
● DES(Data Encryption Standard
● PBE(Password
● RSA(算法的名字以發明者的名字命名
● DH(Diffie
● DSA(Digital Signature Algorithm
● ECC(Elliptic Curves Cryptography
本篇內容簡要介紹BASE
MD
BASE
按照RFC
常見於郵件
通過java代碼實現如下
/** *//**
* BASE
*
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptBASE
return (new BASE
}
/** *//**
* BASE
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE
return (new BASE
}
主要就是BASE
MD
MD
通過java代碼實現如下
/** *//**
* MD
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptMD
MessageDigest md
md
return md
}
通常我們不直接使用上述MD
SHA
SHA(Secure Hash Algorithm
通過java代碼實現如下
/** *//**
* SHA加密 ;;
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptSHA(byte[] data) throws Exception {
MessageDigest sha = MessageDigest
sha
return sha
}
}
HMAC
HMAC(Hash Message Authentication Code
通過java代碼實現如下
/** *//**
* 初始化HMAC密鑰 ;
*
* @return
* @throws Exception
*/
public static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator
SecretKey secretKey = keyGenerator
return encryptBASE
}
/** *//**
* HMAC加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptHMAC(byte[] data
SecretKey secretKey = new SecretKeySpec(decryptBASE
Mac mac = Mac
mac
return mac
}
給出一個完整類
import java
import javax
import javax
import javax
import sun
import sun
/** *//**
* 基礎加密組件 ;
*
* @author 梁棟
* @version
* @since
*/
public abstract class Coder {
public static final String KEY_SHA =
public static final String KEY_MD
/** *//**
* MAC算法可選以下多種算法
*
* <pre>
* HmacMD
* HmacSHA
* HmacSHA
* HmacSHA
* HmacSHA
* </pre>
*/
public static final String KEY_MAC =
/** *//**
* BASE
*
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptBASE
return (new BASE
}
/** *//**
* BASE
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE
return (new BASE
}
/** *//**
* MD
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptMD
MessageDigest md
md
return md
}
/** *//**
* SHA加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptSHA(byte[] data) throws Exception {
MessageDigest sha = MessageDigest
sha
return sha
}
/** *//**
* 初始化HMAC密鑰
*
* @return
* @throws Exception
*/
public static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator
SecretKey secretKey = keyGenerator
return encryptBASE
}
/** *//**
* HMAC加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptHMAC(byte[] data
SecretKey secretKey = new SecretKeySpec(decryptBASE
Mac mac = Mac
mac
return mac
}
}
再給出一個測試類
import static org
import org
/**
*
* @author 梁棟
* @version
* @since
*/
public class CoderTest {
@Test
public void test() throws Exception {
String inputStr =
System
byte[] inputData = inputStr
String code = Coder
System
byte[] output = Coder
String outputStr = new String(output);
System
// 驗證BASE
assertEquals(inputStr
// 驗證MD
assertArrayEquals(Coder
// 驗證SHA對於同一內容加密是否一致
assertArrayEquals(Coder
String key = Coder
System
// 驗證HMAC對於同一內容
assertArrayEquals(Coder
inputData
BigInteger md
System
BigInteger sha = new BigInteger(Coder
System
BigInteger mac = new BigInteger(Coder
System
}
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27564.html