| |
36. 25. 4. Generating a MAC from a text input using a random key. |
|
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import sun.misc.*;
public class MainClass {
public static void main (String[] args) throws Exception {
SecureRandom random = new SecureRandom();
byte[] keyBytes = new byte[20];
random.nextBytes(keyBytes);
SecretKeySpec key = new SecretKeySpec(keyBytes, "HMACSHA1");
System.out.println("Key:"+new
BASE64Encoder().encode(key.getEncoded()));
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(key);
mac.update("test".getBytes("UTF8"));
byte[] result = mac.doFinal();
System.out.println("MAC: "+new BASE64Encoder().encode(result));
}
}
|
|
36. 25. 苹果 | | 36. 25. 1. | Mac创造 | | | | 36. 25. 2. | 使用Mac | | | | 36. 25. 3. | 产生一个消息认证码 | | | | 36. 25. 4. | Generating a MAC from a text input using a random key. | | |
|