找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3539|回复: 0

[原]VC++网络安全编程范例(3)-消息鉴别码MAC算法编程

[复制链接]
发表于 2012-3-8 14:43:46 | 显示全部楼层 |阅读模式

消息鉴别码(Message Authentication Code)也叫密码校验和(cryptographic checksum),鉴别函数的一种.

  消息鉴别码实现鉴别的原理是,用公开函数和密钥产生一个固定长度的值作为认证标识,用这个标识鉴别消息的完整性.使用一个密钥生成一个固定大小的小数据块,即MAC,并将其加入到消息中,然后传输.接收方利用与发送方共享的密钥进行鉴别认证等.

  MAC是与明文信息M一同发送给对方,目的方使用事先协商好的密钥K对明文信息M进行完整性认证,由于密钥K仅通信双方知道,所以也可判定消息是由对方发送的(而不是攻击者)。这是MAC与传统单向函数的区别。

  消息鉴别码(MAC)仅仅认证消息M的完整性(不会被篡改)和可靠性(不会是虚假的消息或伪造的消息),并不负责信息M是否被安全传输。之所以要放弃信息的保密性(使用公钥加密私钥签名的对称密码协议可以很好的保证信息M的保密性、完整性和可靠性),是因为在某些场合(政府部门公告、网络管理通知等)并不需要对信息进行加密;或者是有些场合(例如广播信息等)需要长时间传输大量信息。由于MAC函数是单向函数,因此对明文M进行摘要计算的时间远比使用对称算法或公开密钥算法对明文加密的时间要小。

  例如:A要给B发信,A将明文M使用Hash算法进行摘要提取,提取结果为Hash(M),之后用A的私钥对摘要进行签名SA[Hash(M)],之后将M(当然M可以使用对称算法加密)和SA[Hash(M)]一同发给B。其中SA[Hash(M)]便可称之为消息鉴别码(MAC)。

下面代码实现了消息鉴别码MAC算法编程。详细见代码注释 。
  1.   #include <stdio.h>
  2. #include <string.h>
  3. #include <openssl/evp.h>
  4. #include <openssl/hmac.h>
  5. //以十六进制打印无符号提示字符串
  6. void print(const char *promptStr,unsigned char *data,int len)
  7. {
  8.     int i;
  9.     if(promptStr!=NULL)
  10.     {
  11.         printf("======%s[out len=%d]======\n",promptStr,len);
  12.     }
  13.     for(i = 0; i < len; i++) printf("%02x", data[i]);
  14.     if(promptStr!=NULL)
  15.     {
  16.         printf("\n===============\n");
  17.     }
  18. }
  19. //以十六进制打印无符号提示字符串
  20. void printDirectly(unsigned char *data,int len)
  21. {
  22.     print(NULL,data,len);
  23. }
  24. /* Return the actual lenght be read out.
  25.    return 0 when the file is meet the end.
  26.    and -1 when error occurs.
  27.    @param len--the actual lenght be read out.
  28.    @parma buf--the buffer to hold the content.
  29.    @parma buflen--the capacity of the buffer
  30. */
  31. unsigned int read_file(FILE *f,unsigned char * buf, int buflen)
  32. {
  33.     int actualReadLen=0;
  34.     if(buflen<=0)
  35.     {
  36.         printf("缓冲区长度无效\n");
  37.         return -1;
  38.     }
  39.     actualReadLen = (int)fread(buf, sizeof(unsigned char), buflen, f);
  40.     if (actualReadLen > 0)
  41.     {
  42.        return actualReadLen;
  43.     }
  44.     if ( feof(f) )
  45.     {
  46.         return 0;
  47.     }
  48.     return -1;
  49. }
  50. //需从命令行中输入文件
  51. void main(int argc, char *argv[])
  52. {
  53. //这是一个例子,跟实际安全的方法是利用随机机制得到主密钥
  54.     char key[]="simple_key";
  55.     const char* digest="sha";
  56.     const char* srcfile="mac.c";//tobe calculate mac
  57.     FILE *f=NULL;
  58.     EVP_MD_CTX mdctx;
  59.     const EVP_MD *md;
  60.     unsigned char mac_value[EVP_MAX_MD_SIZE];
  61.     int mac_len=0;
  62.     HMAC_CTX mac_ctx;
  63.     unsigned char buffer[2048];
  64.     int actualReadLen=0;
  65.     OpenSSL_add_all_digests();
  66.     md = EVP_get_digestbyname(digest);
  67.     if(!md) {
  68.         printf("不能识别的信息摘要: %s\n", digest);
  69.         exit(1);
  70.     }
  71.     EVP_MD_CTX_init(&mdctx);
  72.     EVP_DigestInit_ex(&mdctx, md, NULL);
  73.     if( (f=fopen(srcfile, "rb"))==NULL )
  74.     {
  75.         printf("打开文件%s时失败\n", srcfile);
  76.         exit(1);
  77.     }
  78.     HMAC_Init(&mac_ctx, key, sizeof(key), md);
  79.    
  80.     for(;;)
  81.     {
  82.         actualReadLen=read_file(f, buffer, sizeof(buffer));
  83.         if( actualReadLen==0) break;//finish reading.
  84.         if( actualReadLen<0)
  85.         {
  86.             printf("错误发生在文件 [%s]\n", srcfile);
  87.             exit(1);
  88.         }
  89.         HMAC_Update(&mac_ctx, buffer, actualReadLen);
  90.     }
  91.     HMAC_Final(&mac_ctx, mac_value, &mac_len);
  92.     HMAC_cleanup(&mac_ctx);
  93.     printf("HMAC(%s,%s)=", srcfile,key);
  94.     printDirectly(mac_value, mac_len);
  95.     printf("\n");
  96.     printf("\n click any key to continue.");
  97. //相当于暂停,便于观察运行结果
  98.     getchar();
  99. }
复制代码

作者:yincheng01 发表于2011-10-7 22:00:34 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-3 15:51 , Processed in 0.010923 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表