Java安全与应用部署全解析
1. Java安全机制
1.1 消息认证码计算
消息认证码(MAC)的计算与使用 MessageDigest
引擎类计算消息摘要非常相似,主要区别在于需要一个密钥。以下是一个计算消息认证码的示例代码:
public static void main(String args[]) {
try {
String inputString = "Test input string";
KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey secretKey = keyGen.generateKey();
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
// the Mac class needs data in byte format
byte[] byteData = inputString.getBytes("UTF8");
// Compute the MAC for the data all in one operation
byte[] macBytes = mac.doFinal(byteData);
String macAsString = new sun.misc.BASE64Encoder().encod