C 如何将字符串传递到此MD5程序。?

C 如何将字符串传递到此MD5程序。?,c,md5,argument-passing,cryptoapi,C,Md5,Argument Passing,Cryptoapi,我试图实现一个在下面使用MD5哈希的应用程序,我从互联网上获得了MD5哈希程序,但我不知道如何将字符串从我的程序(我的程序是用C编写的)传递到计算MD5哈希的程序 这是计算MD5哈希的程序的主要部分 // // MD5 Hashing Example - Using Windows Crypto API // // by Napalm @ NetCore2K // #include <stdio.h> #include <string.h> #include "winmd

我试图实现一个在下面使用MD5哈希的应用程序,我从互联网上获得了MD5哈希程序,但我不知道如何将字符串从我的程序(我的程序是用C编写的)传递到计算MD5哈希的程序

这是计算MD5哈希的程序的主要部分

//
// MD5 Hashing Example - Using Windows Crypto API
//
// by Napalm @ NetCore2K
//
#include <stdio.h>
#include <string.h>
#include "winmd5.h"
HCRYPTPROV hCryptProv;

int main(int argc, char *argv[])
{
    int i;
    CryptStartup();
    if(argc > 1){
        MD5Context ctx;
        MD5Init(&ctx);
        MD5Update(&ctx, (unsigned char *)argv[1], strlen(argv[1]));
        MD5Final(&ctx);
        for(i = 0; i < 16; i++)
            printf("%02x", ctx.digest[i]);
        printf("\n");
    }else
        printf("Usage: %s <string>\n", argv[0]);
    CryptCleanup();
    return 0;
}
//
//MD5哈希示例-使用Windows Crypto API
//
//通过凝固汽油弹@NetCore2K
//
#包括
#包括
#包括“winmd5.h”
HCRYPTPROV HCRYPTPROV;
int main(int argc,char*argv[])
{
int i;
CryptStartup();
如果(argc>1){
MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx,(未签名字符*)argv[1],strlen(argv[1]);
MD5最终版(&ctx);
对于(i=0;i<16;i++)
printf(“%02x”,ctx.digest[i]);
printf(“\n”);
}否则
printf(“用法:%s\n”,argv[0]);
密码清除();
返回0;
}
这是头文件内容

//
// MD5 Hashing Example - Using Windows Crypto API
//
// by Napalm @ NetCore2K
//
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern HCRYPTPROV hCryptProv;

void PrintMD5(const char *);

typedef struct {

unsigned char digest[16];
unsigned long hHash;
} MD5Context;
BOOL CryptStartup()
{


if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET) == 0){
    if(GetLastError() == NTE_EXISTS){
    if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) == 0){
            return FALSE;
        }   
    }
    else return FALSE;
}
return TRUE;
}
void CryptCleanup()
{
if(hCryptProv) CryptReleaseContext(hCryptProv, 0);
hCryptProv = NULL;
}
void inline MD5Init(MD5Context *ctx)
{ 
CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
}
void inline MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned len)
{
CryptHashData(ctx->hHash, buf, len, 0);
}
void inline MD5Final(MD5Context *ctx)
{
DWORD dwCount = 16;
CryptGetHashParam(ctx->hHash, HP_HASHVAL, ctx->digest, &dwCount, 0);
if(ctx->hHash) CryptDestroyHash(ctx->hHash);
ctx->hHash = 0;
}
void PrintMD5(const char *s)
{
MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx, (const unsigned char *)s, strlen(s));
MD5Final(&ctx);

int i;
for (i = 0; i < 16; i++) {
    printf("%02x", ctx.hHash[i]);
}

printf("\n");
 }
//
//MD5哈希示例-使用Windows Crypto API
//
//通过凝固汽油弹@NetCore2K
//
#包括
#包括
#包括
#包括
#包括
外部HCRYPTPROV HCRYPTPROV;
无效打印MD5(常量字符*);
类型定义结构{
无符号字符摘要[16];
无符号长hHash;
}MD5Context;
BOOL CryptStartup()
{
if(CryptAcquireContext(&hCryptProv,NULL,MS_ENHANCED_PROV,PROV_RSA_FULL,CRYPT_NEWKEYSET)==0){
如果(GetLastError()==NTE_存在){
if(CryptAcquireContext(&hCryptProv,NULL,MS_ENHANCED_PROV,PROV_RSA_FULL,0)=0){
返回FALSE;
}   
}
否则返回FALSE;
}
返回TRUE;
}
void cryptocleanup()
{
if(hCryptProv)cryptreasecontext(hCryptProv,0);
hCryptProv=NULL;
}
无效内联MD5Init(MD5Context*ctx)
{ 
CryptCreateHash(hCryptProv、CALG_MD5、0、0和ctx->hHash);
}
无效内联MD5Update(MD5Context*ctx,无符号字符常量*buf,无符号len)
{
CryptHashData(ctx->hHash,buf,len,0);
}
无效内嵌MD5Final(MD5Context*ctx)
{
DWORD dwCount=16;
CryptGetHashParam(ctx->hHash,HP_HASHVAL,ctx->digest,&dwCount,0);
如果(ctx->hHash)是散列(ctx->hHash);
ctx->hHash=0;
}
无效打印MD5(常量字符*s)
{
MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx,(常量无符号字符*)s,strlen(s));
MD5最终版(&ctx);
int i;
对于(i=0;i<16;i++){
printf(“%02x”,ctx.hHash[i]);
}
printf(“\n”);
}
我应该如何调用Main(),以及如何将字符串传递给它并获得结果。? 提前谢谢

关于您不再调用
main()
。您可以使用此程序作为示例来编写单独的函数,如下所示:

void PrintMD5(const char *s)
{
    MD5Context ctx;
    MD5Init(&ctx);
    MD5Update(&ctx, (const unsigned char *)s, strlen(s));
    MD5Final(&ctx);

    int i;
    for (i = 0; i < 16; i++) {
        printf("%02x", ctx.digest[i]);
    }

    printf("\n");
}

MD5Context
(您从
winmd5.h
中获取)的用法是否不言而喻?您要求我们告诉您如何使用(a)我们没有编写的代码,(b)您没有编写的代码,(c)操作系统和标准库没有提供的代码,以及(d)我们看不到的代码。关于如何使用WinCrypt生成MD5摘要,web上有成千上万的示例。你试过/读过这些吗?(或者我只是不明白这个问题,如果是这样的话,这不是第一次了。)谢谢你的帮助,先生,我已经成功地尝试过了,但是结果是这样的;(输出:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc。?t@chard我不确定问题出在哪里。。。也许调用CryptInit()和CryptCleanup()?谢谢兄弟,我刚刚找到了它,它是MD5函数开头的CryptStartup()函数。当做
PrintMD5("FooBarHelloWorld42");