这个压缩包里的都是超级经典的java例子
源代码在线查看: genmac.htm
Generating a Message Authentication Code (MAC) (Java Developers Almanac Example)
The Java Developers Almanac 1.4
Order this book from Amazon.
google_ad_client = "pub-6001183370374757";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel = "4777242811";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "6666CC";
google_color_url = "6666CC";
google_color_text = "000000";
//-->
Home
>
List of Packages
>
javax.crypto
[14 examples]
>
MAC
[3 examples]
e469. Generating a Message Authentication Code (MAC)
A MAC is like hash code for a sequence of bytes. Unlike a hash code,
a MAC uses a secret key to generate the hash code, or more
specifically, the message digest. A MAC is generally used to
check the integrity or validity of information based on a secret key.
try {
// Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104
// In practice, you would save this key.
KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey key = keyGen.generateKey();
// Create a MAC object using HMAC-MD5 and initialize with key
Mac mac = Mac.getInstance(key.getAlgorithm());
mac.init(key);
String str = "This message will be digested";
// Encode the string into bytes using utf-8 and digest it
byte[] utf8 = str.getBytes("UTF8");
byte[] digest = mac.doFinal(utf8);
// If desired, convert the digest into a string
String digestB64 = new sun.misc.BASE64Encoder().encode(digest);
} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
} catch (UnsupportedEncodingException e) {
}
Related Examples
e467.
Listing All Available Message Authentication Code (MAC) Key Generators
e468.
Generating a Message Authentication Code (MAC) Key
See also:
Encrypting and Decrypting
Key Agreement
Symmetric Keys
© 2002 Addison-Wesley.