如何正确地将TAG值转换为正确的格式,以便验证HMAC? [英] How to correctly convert TAG value to the right format so that to Verify HMAC?

查看:224
本文介绍了如何正确地将TAG值转换为正确的格式,以便验证HMAC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究HMAC的生成和验证以检查数据的完整性。我可以正确地生成MAC值,但通过套接字发送到另一个程序进行验证时,我遇到了格式不匹配问题。我感谢您的支持。

$ $ $ $ $ $ $ $ $ $ $ $无符号字符* MAC(无符号字符*键,无符号字符*消息)
{
无符号字符*结果;
unsigned int result_len = 32;
int i;

result =(unsigned char *)malloc(sizeof(char)* result_len);
result = HMAC(EVP_sha256(),key,strlen(key),message,strlen(message),NULL,NULL);

返回结果;

$ b int verifyMAC(unsigned char * key,unsigned char * message,unsigned char * receivedTag)
{
printf(\\\
\\\
= =================== MAC验证================== \\\
\\\
);
unsigned char * newHash; //新生成的散列值
unsigned int newHash_len = 32;
int i,flag = 0;

newHash =(unsigned char *)malloc(sizeof(char)* newHash_len);
newHash = HMAC(EVP_sha256(),key,strlen(key),message,strlen(message),NULL,NULL);

for(i = 0; i!= newHash_len; i ++)
{
if(receivedTag [i]!= newHash [i])
{
printf(DATA MISMATCH:found%02X instead of%02X at index%d!\\\
,newHash [i],receivedTag [i],i);
休息;


if(i == newHash_len)
{
printf(MAC verified!\\\
);
flag = 1;
}

返回标志;

$ b $ int main(int argc,char * argv [])
{

unsigned char * key =1234567890;
unsigned char * message =(unsigned char *)hello world;
....
}

控制台结果:

散列数据:E4 5F 60 72 61 7C CE 5E 06 A9 5B E4 81 C4 33 51 02 3D 99 23 35 99 EA C9 FD AF FC 95 81 42 62 9A

==================== MAC验证==================



DATA MISMATCH:在索引0找到E4而不是65!
ERROR:data is modified

解决方案

我认为这个问题有点有趣,所以我经历了麻烦重新创建场景。也许这是不对的。但是,我认为问题的一个简单例子是:

pre code $ void main(int argc,char * argv [])
{
//原始哈希
unsigned char newHash [] = {0xE4,0x5F,0x60,0x72,0x61,0x7C,0xCE,0x5E,0x06,0xA9,0x5B,0xE4,0x81,0xC4 ,0x33,0x51,0 $ b $ 0x02,0x3D,0x99,0x23,0x35,0x99,0xEA,0xC9,0xFD,0xAF,0xFC,0x95,0x81,0x42,0x62,0x9A};然后,
//我认为是从套接字接收的
unsigned char * receivedTag =e45f6072617cce5e06a95be481c43351023d99233599eac9fdaffc958142629a;

如果(receivedTag [i]!= newHash [i])
{
printf(DATA MISMATCH:Found%02X instead of%02X at index%d!\\\
,newHash [i],receivedTag [i],i);
休息;
}
}
return;
}

,输出为

  DATA MISMATCH:在索引0找到E4而不是65! 

所以,我认为解决方案只是将Hex数组转换为字符串来自套接字。



也许这不是最优雅的做事方式。但一个解决方案无论如何。



$ p $ char $ hexStringToCharString(unsigned char hash [],int length);
void main(int argc,char * argv [])
{
//原始哈希
unsigned char newHash [] = {0xE4,0x5F,0x60,0x72,0x61 ,0x7C,0xCE,0x5E,0x06,0xA9,0x5B,0xE4,0x81,0xC4,0x33,0x51,0 $ b 0x02,0x3D,0x99,0x23,0x35,0x99,0xEA,0xC9,0xFD,0xAF,0xFC,0x95 ,0x81,0x42,0x62,0x9A};
//我认为是从套接字接收的
unsigned char * receivedTag =e45f6072617cce5e06a95be481c43351023d99233599eac9fdaffc958142629a;

char * newString = hexStringToCharString(newHash,32);

(int i = 0; i!= strlen(newString); i ++)
{
if(receivedTag [i]!= newString [i])
{
printf(DATA MISMATCH:Found%02X instead of%02X at index%d!\\\
,newHash [i],receivedTag [i],i);
休息;
}
}
free(newString);

printf(Yay \\\
);
return;


char * hexStringToCharString(unsigned char hash [],int length){

char temp [3];
//需要长度* 2个字符,64位,加一个空值!
char * theString =(char *)malloc(sizeof(char)*((length * 2)+1));
strcpy(theString,);
for(int i = 0; i< length; i ++){
sprintf(temp,%02x,hash [i]);
strcat(theString,temp);
}
返回theString;






这种情况下的输出是

  Yay 

这完全是错误的。但是,如果您发现此解决方案需要编辑,请在下面评论。


I'm working on HMAC generation and verifying to check data integrity. I can correctly generate the MAC value but when sending it through socket to another program for verification, I faced with formatting mismatch. I appreciate your support. Thanks.

unsigned char* MAC(unsigned char* key,unsigned char* message)  
{
    unsigned char* result;
    unsigned int result_len = 32;  
    int i;

    result = (unsigned char*) malloc(sizeof(char) * result_len);
    result = HMAC(EVP_sha256 (), key , strlen (key), message , strlen(message) , NULL, NULL);

return result;
} 

int verifyMAC(unsigned char* key,unsigned char* message, unsigned char* receivedTag)
{
printf("\n\n ==================== MAC Verification ==================\n\n");
    unsigned char* newHash; // newly generated hash value
    unsigned int newHash_len = 32;  
    int  i,flag=0;

    newHash = (unsigned char*) malloc(sizeof(char) * newHash_len);
newHash = HMAC(EVP_sha256 (), key , strlen (key), message , strlen(message) , NULL, NULL);

for (i=0; i!=newHash_len; i++)
    {
            if (receivedTag[i]!=newHash[i])
            {
                    printf("DATA MISMATCH: Found %02X instead of %02X at index %d!\n", newHash[i], receivedTag[i], i);
                    break;
            }
    }
    if (i==newHash_len)
    {
            printf("MAC verified!\n");
    flag = 1;
    } 

return flag;
}

int main(int argc, char *argv[])
{

unsigned char* key = "1234567890";
unsigned char* message = (unsigned char*) "hello world";
  ....
}

Console result:
Hashed data: E4 5F 60 72 61 7C CE 5E 06 A9 5B E4 81 C4 33 51 02 3D 99 23 35 99 EA C9 FD AF FC 95 81 42 62 9A

==================== MAC Verification ==================

DATA MISMATCH: Found E4 instead of 65 at index 0! ERROR: data is modified

解决方案

I thought this problem was somewhat interesting so I went through the trouble to recreate the scenario. Maybe this is not even right. But a simple case of what I thought the problem is:

void main(int argc, char *argv[])
{
    //the original hash
    unsigned char newHash[] = {0xE4, 0x5F, 0x60, 0x72, 0x61, 0x7C, 0xCE, 0x5E, 0x06, 0xA9, 0x5B, 0xE4, 0x81, 0xC4, 0x33, 0x51, 
        0x02, 0x3D, 0x99, 0x23, 0x35, 0x99, 0xEA, 0xC9, 0xFD, 0xAF, 0xFC, 0x95, 0x81, 0x42, 0x62, 0x9A};
    //what I think is recieved from the socket
    unsigned char* receivedTag = "e45f6072617cce5e06a95be481c43351023d99233599eac9fdaffc958142629a";

    for (int i=0; i!=32; i++)
    {
            if (receivedTag[i]!=newHash[i])
            {
                    printf("DATA MISMATCH: Found %02X instead of %02X at index %d!\n", newHash[i], receivedTag[i], i);
                    break;
            }
    }
    return;
}

and the output was

DATA MISMATCH: Found E4 instead of 65 at index 0!

So, I thought the solution would be to just convert the Hex array to string just like it was received from the socket.

Maybe this is not the most elegant of ways to do things. But a solution None the less.

char* hexStringToCharString(unsigned char hash[], int length);
void main(int argc, char *argv[])
{
    //the original hash
    unsigned char newHash[] = {0xE4, 0x5F, 0x60, 0x72, 0x61, 0x7C, 0xCE, 0x5E, 0x06, 0xA9, 0x5B, 0xE4, 0x81, 0xC4, 0x33, 0x51, 
        0x02, 0x3D, 0x99, 0x23, 0x35, 0x99, 0xEA, 0xC9, 0xFD, 0xAF, 0xFC, 0x95, 0x81, 0x42, 0x62, 0x9A};
    //what I think is recieved from the socket
    unsigned char* receivedTag = "e45f6072617cce5e06a95be481c43351023d99233599eac9fdaffc958142629a";

    char *newString = hexStringToCharString(newHash, 32);

    for (int i=0; i!=strlen(newString); i++)
    {
            if (receivedTag[i]!=newString[i])
            {
                    printf("DATA MISMATCH: Found %02X instead of %02X at index %d!\n", newHash[i], receivedTag[i], i);
                    break;
            }
    }
    free(newString);

    printf("Yay\n");
    return;
}

char* hexStringToCharString(unsigned char hash[], int length){

    char temp[3];
    //need length*2 characters which is 64 plus one for null!
    char *theString = (char *)malloc(sizeof(char)*((length*2)+1));
    strcpy(theString, "");
    for(int i=0;i<length;i++){
        sprintf(temp, "%02x", hash[i]);
        strcat(theString, temp);
    }
    return theString;

}

The output in this case

Yay

So, Maybe this is entirely wrong. But if you find this solution needs editing then comment below.

这篇关于如何正确地将TAG值转换为正确的格式,以便验证HMAC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆