2014-11-01 19 views
-4

進出口工作於Keccak(SHA-3)的實施方案,並且例如我得到這個:我的C程序打印0xE代替0x0E的在Keccak實施

Keccak( 「ABC」)= 3A985DA74FE225B2 5C172D6BD390BD855F 6E3E9D525B46BFE24511431532

相反(如:http://www.di-mgt.com.au/sha_testvectors.html)的

3A985DA74FE225B2 5C172D6BD390BD855F 6E3E9D525B46BFE24511431532

正如你可以看到我的程序錯過了一個「0x00」,我不知道我做錯了什麼。這裏

對不起球員是我的主要方法的代碼:

#include <stdio.h> 
#include <stdint.h> 
#include <inttypes.h> 
#include <stdlib.h> 
#include <string.h> 
#include "round.h" 
#include "keccak.h" 
#define TAM_MAX_ENTRADA 4096 /*Especificacion del numero de caracteres de la entrada*/ 
int main(int argc, char *argv[]) 
{ 

    int32_t sha3Tipo; /*Nos dice el tipo de SHA3*/ 
    char Mensaje[TAM_MAX_ENTRADA]; /*Array de caracteres donde guardamos el mensaje de entrada*/ 
    printf("Elige el tamaño de salida (224,256,384,512): "); 
    scanf("%d",&sha3Tipo); 
    if(argc == 1) 
    { 
     strcat(Mensaje,""); /*Si no nos han pasado argumentos significa que es un string vacio*/ 
    } 
    else 
    { 
     strcpy(Mensaje,argv[1]); /*Copiamos la primera palabra en el array de caracteres*/ 
    } 

    for(int32_t i = 2; i<argc; i++) 
    { 
     strcat(Mensaje," "); /*Si tenemos mas de una palabra entonces anadimos */ 
     strcat(Mensaje,argv[i]); 
    } 
    int32_t size = strlen(Mensaje); 
    int32_t *psize = &size; 
    uint8_t *newmessage; 

    newmessage=keccak((uint8_t *)Mensaje, *psize, sha3Tipo); 
    printf("Keccak(\"%s\") = ",Mensaje); 
    for(int32_t i =0; i<sha3Tipo/8; i++) 
    { 
     printf("%X", *(newmessage+i)); 
    } 
    printf("\n"); 

    return 0; 
} 
+1

我想你的代碼有問題。 – 2014-11-01 18:55:43

+0

謝謝,但我期待更好的解決方案。 – 2014-11-01 18:56:46

+1

除非您向我們展示您正在運行的代碼,否則我們無法提供任何建議。 – 2014-11-01 18:58:21

回答

3

也許你正在轉換的數字爲十六進制的字符串時失去前導零?例如,考慮the following code

#include <stdio.h> 
int main(int argc, char *argv[]) 
{ 
    printf("Without formatting: %x and %x\n", 12, 99); 
    printf("With formatting: %02x and %02x\n", 12, 99); 
    return 0; 
} 

運行時,輸出爲:

沒有格式:C,並與格式63 :0℃至63

我在想,您的48應該是0408,但它們的格式不正確。

+0

兩行中都有一個0,但在這種情況下,它是十六進制對中的第二個。丟失的零是十六進制對中的第一個。標出了答案。 – 2014-11-01 19:05:06

+0

@WeatherVane這就是發生了什麼。 – 2014-11-01 19:08:54

+0

謝謝你的幫助現在它的作品完美:D! – 2014-11-01 19:10:25