利用OPENSSL 实现MD5加密。

#include <stdio.h>

#include "openssl/evp.h"

#include "openssl/md5.h"





#include<iostream>

using namespace std;



void main()

{

    bool do_encrypt(unsigned char *inBuffer,int inLen,unsigned char *outBuffer,int * pOutLen);

    void print_hex(unsigned char *buf,unsigned int len);









unsigned char inBuffer[200];

unsigned char OutBuffer[200];





char c=' ';

int i=0,outLen=0;





cout<<"请输入需要加密的明文,以*作为结束符:";

while(c!='*')

{

c=getchar();

inBuffer[i++]=c;

}

cout<<endl;

do_encrypt(inBuffer,i-1,OutBuffer,&outLen);



cout<<"使用MD进行加密,生成的密文为:";

print_hex(OutBuffer,outLen);

cout<<endl<<endl; 





system("pause");

}



   bool do_encrypt(unsigned char *inBuffer,int inLen,unsigned char *outBuffer,int *pOutLen)

{

    unsigned int tmplen;

    EVP_MD_CTX ctx; 



    EVP_MD_CTX_init(&ctx);//step1

    

    EVP_DigestInit(&ctx,EVP_md5());//step2

    

    if(!EVP_DigestUpdate(&ctx,inBuffer,inLen))//step3

    {

        return false;

    }

    

    if(!EVP_DigestFinal(&ctx,outBuffer+*pOutLen,&tmplen))//step4

    {

        return false;

    }

*pOutLen+=tmplen;

EVP_MD_CTX_cleanup(&ctx);

    return true;

}



 void print_hex(unsigned char *buf,unsigned int len)

{

    for(int i=0;i<len;i++)

        printf("%0.2x",buf[i]);

}

环境的配置在前面的文章已经讲过。

你可能感兴趣的:(OpenSSL)