国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java中的mac計算

2019-11-17 03:59:59
字體:
來源:轉載
供稿:網友
   報文鑒別在身份認證中占重要位置,是認證系統的一個重要環節,在金融和商業系統中廣泛應用。

          報文鑒別常用報文鑒別碼(Message Authentication Code,即MAC)作為鑒別的基礎,

          其基本原理是:用一個密鑰((PRivate Key))生成一個小數據塊附加在要傳輸的報文后面。這種技術是假定通訊雙方共享一個密鑰K,當通訊的一方要給另一方傳輸數據M時,通過特定的算法計算出該報文M的MAC=F(K,M),這里的F就是指鑒別運算函數,然后報文數據M和MAC一起傳送給另一方。當另一方收到數據時,先用同樣的算法F計算MAC,和傳輸過來的MAC進行比較,如果相同,則認為數據是正確的。 鑒別函數常被認為是加密算法的一種,但是區別在于它不用于逆運算,有一定的安全性。 通常對于不同的應用系統采用各自特定的鑒別函數,這樣在一定程度上保證了數據的安全性。

       從j2sdk1.4以后,java提供了類javax.crypto.Mac類報文鑒別

view plaincopy to clipboardprint?
  byte[] macData = "this is a mac test".getBytes(); //get a instance of Mac, using Hmacmd5 algorithm    
  
Mac mac = Mac.getInstance("HmacMD5"); //init the IV of the algorithm, 8 bytes. may read from file    
  
byte[] ivInitKey = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07 };    
  
IvParameterSpec spec = new IvParameterSpec(ivInitKey); // the secrete key bytes of the Mac validation, May read from file too    
  
byte[] keyBytes = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f }; //generate the secrete Key Object using secrete bytes and DESede algorithm    
  
SecretKey key = new SecretKeySpec(keyBytes, "DESede"); // init Mac mac.init(key, spec);    
  
byte[] macCode = mac.doFinal(macData);   
  byte[] macData = "this is a mac test".getBytes(); //get a instance of Mac, using HmacMD5 algorithm

Mac mac = Mac.getInstance("HmacMD5"); //init the IV of the algorithm, 8 bytes. may read from file

byte[] ivInitKey = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07 };

IvParameterSpec spec = new IvParameterSpec(ivInitKey); // the secrete key bytes of the Mac validation, May read from file too

byte[] keyBytes = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f }; //generate the secrete Key Object using secrete bytes and DESede algorithm

SecretKey key = new SecretKeySpec(keyBytes, "DESede"); // init Mac mac.init(key, spec);

byte[] macCode = mac.doFinal(macData);

          

macCode 就是生成的Mac,用同樣的密鑰能產生同樣的mac。

通訊雙方只要保證用同樣的密鑰就能保證得到同樣的Mac,達到報文驗證的目的。

看來看去,這和網絡上常用的checksum原理是一樣的。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 静安区| 赤水市| 九龙坡区| 兰溪市| 孟津县| 淮安市| 灵丘县| 丰原市| 泰州市| 开阳县| 乌兰浩特市| 枣强县| 宣威市| 竹北市| 庆安县| 铜山县| 安新县| 临夏县| 小金县| 铁力市| 财经| 玛曲县| 屏山县| 大冶市| 互助| 宜都市| 顺义区| 茌平县| 揭东县| 沽源县| 阿拉善右旗| 渭源县| 大兴区| 威信县| 桑日县| 赤水市| 麦盖提县| 江陵县| 临沭县| 上思县| 隆子县|