如何在C ++中使用openssl/md5加密字符串? [英] How can I use openssl/md5 in C++ to crypt a string?

查看:646
本文介绍了如何在C ++中使用openssl/md5加密字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在程序中的md5中加密一个字符串. 有lib openssl,但我是新手. 怎么可能用它来加密一个字符串,在哪里可以找到一个好的文档来教我如何使用此lib以及其他函数(例如aes)?

I need to crypt in md5 a string in my program. There is the lib openssl but I'm a newbie about it. How is possible crypt a string using that and where I can find a good doc that teach me how to use this lib, also with other function like aes?

我尝试了以下代码:

int main()
{
    unsigned char result[MD5_DIGEST_LENGTH];
    const unsigned char* str;
    str = (unsigned char*)"hello";
    unsigned int long_size = 100;
    MD5(str,long_size,result);
}

但是编译器告诉我: 未定义对MD5的引用.

But the compiler told me: undefined reference to MD5.

为什么存在对MD5的未定义引用?

Why is there and undefined reference to MD5?

推荐答案

您应该看看

You should take a look at the documentation. An option is to use this function:

#include <openssl/md5.h>
unsigned char *MD5(const unsigned char *d, 
                   unsigned long n,
                   unsigned char *md);

他们声明:

MD2(),MD4()和MD5()计算dn字节的MD2,MD4和MD5消息摘要,并将其放置在中(必须具有用于MD2_DIGEST_LENGTH =的空间. = MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16个字节的输出).如果md为NULL,则摘要将放置在静态数组中.

MD2(), MD4(), and MD5() compute the MD2, MD4, and MD5 message digest of the n bytes at d and place it in md (which must have space for MD2_DIGEST_LENGTH == MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16 bytes of output). If md is NULL, the digest is placed in a static array.

对于AES,如果您还想使用OpenSSL,请查看此示例如何使用它.请注意,您必须添加

As for AES, if you also want to use OpenSSL, then take a look at EVP doc and this example of how to use it. Just note that you have to add

#define AES_BLOCK_SIZE 16

但是在文件顶部,它可以正常工作.

In the top of the file for it to work, though.

顺便说一句.我真的可以推荐 Crypto ++库,因为它很棒并且具有各种加密原语. AES,椭圆曲线,MAC,公共密钥加密等.

Btw. I can really recommend the Crypto++ library since it's great and has all kinds of cryptographic primitives; AES, Elliptic Curves, MAC, public-key crypto and so on.

这篇关于如何在C ++中使用openssl/md5加密字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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