这里有以下数据,我必须使用openssl找到sha1摘要。
数据:

AwAIAOwIAAABABwAgAIAABYAAAAAAAAAAAAAAHQAAAAAAAAAAAAAAAgAAAAkAAAAQgAAAFQAAABsAAAAhgAAAJgAAACuAAAAwgAAAM4AAADsAAAAAgEAAAwBAAAoAQAARgEAAFgBAACwAQAAtAEAANABAADkAQAA+gEAAAIAaQBkAAAADABsAGEAeQBvAHUAdABfAHcAaQBkAHQAaAAAAA0AbABhAHkAbwB1AHQAXwBoAGUAaQBnAGgAdAAAAAcAZwByAGEAdgBpAHQAeQAAAAoAYgBhAGMAawBnAHIAbwB1AG4AZAAAAAsAbwByAGkAZQBuAHQAYQB0AGkAbwBuAAAABwBwAGEAZABkAGkAbgBnAAAACQB0AGUAeAB0AEMAbwBsAG8AcgAAAAgAdABlAHgAdABTAGkAegBlAAAABAB0AGUAeAB0AAAADQBwAGEAZABkAGkAbgBnAEIAbwB0AHQAbwBtAAAACQBzAGMAYQBsAGUAVAB5AHAAZQAAAAMAcwByAGMAAAAMAHAAYQBkAGQAaQBuAGcAUgBpAGcAaAB0AAAADQBsAGEAeQBvAHUAdABfAHcAZQBpAGcAaAB0AAAABwBhAG4AZAByAG8AaQBkAAAAKgBoAHQAdABwADoALwAvAHMAYwBoAGUAbQBhAHMALgBhAG4AZAByAG8AaQBkAC4AYwBvAG0ALwBhAHAAawAvAHIAZQBzAC8AYQBuAGQAcgBvAGkAZAAAAAAAAAAMAEwAaQBuAGUAYQByAEwAYQB5AG8AdQB0AAAACABUAGUAeAB0AFYAaQBlAHcAAAAJAEkAbQBhAGcAZQBWAGkAZQB3AAAABgBCAHUAdAB0AG8AbgAAAAAAgAEIAEQAAADQAAEB9AABAfUAAQGvAAEB1AABAcQAAQHVAAEBmAABAZUAAQFPAQEB2QABAR0BAQEZAQEB2AABAYEBAQEAARAAGAAAABEAAAD/////DwAAABAAAAACARAAsAAAABEAAAD//////////xIAAAAUABQABwAAAAAAAAAQAAAAAwAAAP////8IAAAREQAAABAAAAAFAAAA/////wgAABABAAAAEAAAAAAAAAD/////CAAAAR0AB38QAAAABAAAAP////8IAAABEQAGfxAAAAAGAAAA/////wgAAAUBEAAAEAAAAAEAAAD/////CAAAEP////8QAAAAAgAAAP////8IAAAQ/////wIBEACcAAAAGgAAAP//////////EwAAABQAFAAGAAAAAAAAABAAAAAIAAAA/////wgAAAUCEgAAEAAAAAcAAAD/////CAAAARAABn8QAAAACgAAAP////8IAAAFARgAABAAAAABAAAA/////wgAABD/////EAAAAAIAAAD/////CAAAEP7///8QAAAACQAAAP////8IAAABRwAIfwMBEAAYAAAAIAAAAP//////////EwAAAAIBEAB0AAAAIgAAAP//////////EgAAABQAFAAEAAAAAAAAABAAAAAFAAAA/////wgAABAAAAAAEAAAAAQAAAD/////CAAAAREABn8QAAAAAQAAAP////8IAAAQ/////xAAAAACAAAA/////wgAABD+////AgEQAIgAAAAoAAAA//////////8UAAAAFAAUAAUAAAAAAAAAEAAAAA0AAAD/////CAAABQEYAAAQAAAAAQAAAP////8IAAAQ/v///xAAAAACAAAA/////wgAABD+////EAAAAAwAAAD/////CAAAAQEAAn8QAAAACwAAAP////8IAAAQBQAAAAMBEAAYAAAALQAAAP//////////FAAAAAIBEAB0AAAALwAAAP//////////EgAAABQAFAAEAAAAAAAAABAAAAAFAAAA/////wgAABABAAAAEAAAAAEAAAD/////CAAABQEAAAAQAAAAAgAAAP////8IAAAQ/v///xAAAAAOAAAA/////wgAAAQAAIA/AgEQAHQAAAA1AAAA//////////8VAAAAFAAUAAQAAAAAAAAAEAAAAAAAAAD/////CAAAASgAB38QAAAAAQAAAP////8IAAAQ/////xAAAAACAAAA/////wgAABD+////EAAAAAkAAAD/////CAAAARUACH8DARAAGAAAADgAAAD//////////xUAAAACARAAdAAAADoAAAD//////////xUAAAAUABQABAAAAAAAAAAQAAAAAAAAAP////8IAAABKgAHfxAAAAABAAAA/////wgAABD/////EAAAAAIAAAD/////CAAAEP7///8QAAAACQAAAP////8IAAABGgAIfwMBEAAYAAAAPQAAAP//////////FQAAAAMBEAAYAAAAPwAAAP//////////EgAAAAIBEAB0AAAAQQAAAP//////////EgAAABQAFAAEAAAAAAAAABAAAAAFAAAA/////wgAABABAAAAEAAAAAEAAAD/////CAAABQEAAAAQAAAAAgAAAP////8IAAAQ/v///xAAAAAOAAAA/////wgAAAQAAIA/AgEQAHQAAABHAAAA//////////8VAAAAFAAUAAQAAAAAAAAAEAAAAAAAAAD/////CAAAASkAB38QAAAAAQAAAP////8IAAAQ/////xAAAAACAAAA/////wgAABD+////EAAAAAkAAAD/////CAAAARYACH8DARAAGAAAAEoAAAD//////////xUAAAACARAAdAAAAEwAAAD//////////xUAAAAUABQABAAAAAAAAAAQAAAAAAAAAP////8IAAABKwAHfxAAAAABAAAA/////wgAABD/////EAAAAAIAAAD/////CAAAEP7///8QAAAACQAAAP////8IAAABGQAIfwMBEAAYAAAATwAAAP//////////FQAAAAMBEAAYAAAAUQAAAP//////////EgAAAAMBEAAYAAAAUwAAAP//////////EgAAAAMBEAAYAAAAVQAAAP//////////EgAAAAEBEAAYAAAAVQAAAP////8PAAAAEAAAABgAAAA9AAAA//////////8fAAAAAgEQAGAAAAA/AAAA//////////8eAAAAFAAUAAMAAAAAAAAAGQAAAAUAAAD/////CAAAEAAAAAAZAAAAAAAAAP////8IAAAQ/v///xkAAAABAAAA/////wgAABD+////AgEQAMQAAABEAAAA//////////8gAAAAFAAUAAgAAAAAAAAAGQAAABIAAAD/////CAAABQIOAAAZAAAAEQAAAP////8IAAARAQAAABkAAAAQAAAA/////wgAAAEGAAZ/GQAAAAIAAAD/////CAAAARIAB38ZAAAAEwAAAP////8IAAAFAQQAABkAAAAAAAAA/////wgAABD+////GQAAAAEAAAD/////CAAAEP7///8ZAAAADwAAAP////8IAAABMwAIfwMBEAAYAAAASwAAAP//////////IAAAAAIBEACIAAAATQAAAP//////////IAAAABQAFAAFAAAAAAAAABkAAAASAAAA/////wgAAAUCDgAAGQAAABAAAAD/////CAAAAQYABn8ZAAAAAgAAAP////8IAAABEwAHfxkAAAAAAAAA/////wgAABD+////GQAAAAEAAAD/////CAAAEP7///8DARAAGAAAAFEAAAD//////////yAAAAADARAAGAAAAFMAAAD//////////x4AAAACARAAYAAAAFUAAAD//////////x4AAAAUABQAAwAAAAAAAAAZAAAABQAAAP////8IAAAQAAAAABkAAAAAAAAA/////wgAABD+////GQAAAAEAAAD/////CAAAEP7///8CARAAxAAAAFoAAAD//////////yAAAAAUABQACAAAAAAAAAAZAAAAEgAAAP////8IAAAFAg4AABkAAAARAAAA/////wgAABEBAAAAGQAAABAAAAD/////CAAAAQYABn8ZAAAAAgAAAP////8IAAABFAAHfxkAAAATAAAA/////wgAAA

给我的摘要是:
WK2PJNOereHSELMW4TMX+RJHSQQ=
但当我使用(f1=我复制上述数据的文件)时:
base64-d f1.txt openssl dgst-sha1-二进制base64
我得到了一个“base64:invalid input”错误和下面看起来完全不同的摘要:(
Barldid73Rybfmgqvec8g+gfbbu型=
如果有什么错误,有人能确认并解释吗?是吗?
更新:
场景:客户端的二进制文件是base64编码并发送到服务器。服务器对此进行解码并计算sha1摘要。因为我有客户机的base64编码的sha1摘要,所以服务器也将摘要编码为base64。现在这两个应该匹配了。但事实并非如此!我收到所有数据。我重新检查过了。我将在这里提出守则的一部分:
//RCVBUFSIZE = 1024 (defined)
void HandleClient(int clntSocket)
{
char echoBuffer[RCVBUFSIZE] ;        /* Buffer for echo string */
    memset(echoBuffer, 0, RCVBUFSIZE);
    char inBuffer;        /* Buffer for first string */
    char recv_data;
    int recvMsgSize = 0;                    /* Size of received message */
    char replyBuffer[32];
    int bytes_received = 0;
    int rv = 0;
    int connected = clntSocket;
    int len= 0;
    int i = 0;
     EVP_MD_CTX md_ctx;
     const EVP_MD *md;
     unsigned char md_value[EVP_MAX_MD_SIZE];
     unsigned int md_len;
     OpenSSL_add_all_digests();
     md = EVP_get_digestbyname("sha1");
     EVP_MD_CTX_init(&md_ctx);
     EVP_DigestInit_ex(&md_ctx, md, NULL);

    /* Receive message from client */
    while (((bytes_received = recv(connected,&inBuffer,1,0)) > 0) && (inBuffer != '\n')){

    /* Send received string and receive again until end of transmission */
    if (bytes_received  > 0)      /* zero indicates end of transmission */
    {
    printf("Message received from Client is : %c\n", inBuffer);
    char n = inBuffer;
    int indicator =  0;
    int current = 0;
    unsigned long fileLen;

    if(n =='6'){
        if ((recvMsgSize = recv(connected, echoBuffer, RCVBUFSIZE, 0)) < 0)
        DieWithError("recv() failed");
        printf("no. of bytes got : %d\n", recvMsgSize);
        if (recvMsgSize > 0)
        echoBuffer[recvMsgSize] = '\0';
        len= atoi(echoBuffer);
        char *data =NULL;
        printf("length of following message : %d\n", len);
        if(len>0){
    for( i = RCVBUFSIZE; i < (len+RCVBUFSIZE); i=i+RCVBUFSIZE){
                if(i>len)
                recvMsgSize = recv(connected, echoBuffer, (len - (i-RCVBUFSIZE)), 0);
                else
                recvMsgSize = recv(connected, echoBuffer, RCVBUFSIZE, 0);
                echoBuffer[recvMsgSize] = '\0';
                decode(echoBuffer, recvMsgSize, "file_out");
                data = readFileBuffer("file_out");
                EVP_DigestUpdate(&md_ctx, data, strlen(data));
            }
        }
        len = 0;
        memset(echoBuffer, 0, RCVBUFSIZE);
        recvMsgSize = 0;

    }


    if (n =='5'){
            printf("Update Digest Over- Calculate Final Dgst!!!!! \n");
            n= 0;

            EVP_DigestFinal_ex(&md_ctx, md_value, &md_len); //retrieve digest from ctx unto md_value and #bytes written is copied into   md_len
            EVP_MD_CTX_cleanup(&md_ctx);
            FILE *f;
             f = fopen("file_sha1", "w");
            printf("\n");
            printf("******************************************************\n ");
            printf("Digest is: ");
            for(i = 0; i < md_len; i++){
                if ( f !=NULL){
                    fputc(md_value[i], f);
                    }
            printf("%02x", md_value[i]);
            }
            printf("\n");
            printf("******************************************************\n ");
            fclose(f);
    }
printf("socket closing\n");
    close(connected);    /* Close client socket */
}
}

char *readFileBuffer(char *name)
{
    FILE *file;
    char *buffer = NULL;
    unsigned long fileLen;
    //Open file
    file = fopen(name, "rb");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", name);
        return;
    }
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    printf("file length  = %ld\n", fileLen);
    fseek(file, 0, SEEK_SET);

    //printf("Allocate memory\n");
    buffer=(char *)malloc(fileLen+1);
    printf("length of write buffer = %d\n", strlen(buffer));
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");

    }

    long int n = fread(buffer,1, fileLen,file);
    buffer[n] = '\0';
    printf("Read no. of bytes = %ld into buffer \n", n);
    printf("len of buffer  %d  \n", strlen(buffer));
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
    }
    fclose(file);
    //free(name);
    return buffer;

}

// reads b64 encoded msg (ReadBuffer) and writes to WriiteFile.
void decode(char *ReadBuffer, int Length, char *WriteFile)
{
    char *msg = (char *)malloc(Length);
    memset(msg, 0x00, Length);
    int readbytes = -1;

    printf("buffer write file %s\n", WriteFile);

    // the decode msg is written to this bio
    BIO *fileWrBIO = BIO_new_file(WriteFile, "w");

    BIO *b64 = BIO_new(BIO_f_base64());
    BIO *bio = BIO_new_mem_buf(ReadBuffer, Length);
    bio = BIO_push(b64, bio);
    BIO_set_flags(bio,BIO_FLAGS_BASE64_NO_NL);

    while ((readbytes = BIO_read(bio, msg, Length)) > 0)
    {
    printf("readbytes:  %d\n", readbytes);
       BIO_write(fileWrBIO, msg, readbytes);
       BIO_flush(fileWrBIO);
       memset(msg, 0x00, sizeof(msg));
    }
    free(msg);
    BIO_free_all(bio);
    BIO_free_all(fileWrBIO);
}

最佳答案

您的数据无效,可能是部分的。有效的Base64编码字符串的长度应为4的倍数。所以不同的摘要输出是预期的。

关于linux - base64摘要-无效输入?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5066800/

10-15 01:27