C语言 如何在 C++ 中使用 openssl/md5 来加密字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7860362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use openssl/md5 in C++ to crypt a string?
提问by Morten Kristensen
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?
我需要在我的程序中在 md5 中加密一个字符串。有 lib openssl 但我是新手。怎么可能使用它来加密一个字符串,我在哪里可以找到一个好的文档来教我如何使用这个库,以及其他函数,比如 aes?
I've tried this code:
我试过这个代码:
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);
}
But the compiler told me:
undefined reference to MD5.
但是编译器告诉我:对MD5.
Why is there and undefined reference to MD5?
为什么有未定义的引用MD5?
回答by Morten Kristensen
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);
To which they state:
他们声明:
MD2(), MD4(), and MD5() compute the MD2, MD4, and MD5 message digest of the
nbytes atdand place it inmd(which must have space for MD2_DIGEST_LENGTH == MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16 bytes of output). Ifmdis NULL, the digest is placed in a static array.
MD2()、MD4() 和 MD5() 计算
n字节的 MD2、MD4 和 MD5 消息摘要d并将其放入md(必须有空间用于 MD2_DIGEST_LENGTH == MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16 个字节的输出) . 如果md为 NULL,则摘要放置在静态数组中。
As for AES, if you also want to use OpenSSL, then take a look at EVP docand this exampleof how to use it. Just note that you have to add
至于 AES,如果您还想使用 OpenSSL,请查看EVP 文档和如何使用它的示例。请注意,您必须添加
#define AES_BLOCK_SIZE 16
In the top of the file for it to work, though.
但是,在文件的顶部使其工作。
Btw. I can really recommend the Crypto++ librarysince it's great and has all kinds of cryptographic primitives; AES, Elliptic Curves, MAC, public-key crypto and so on.
顺便提一句。我真的可以推荐Crypto++ 库,因为它很棒并且拥有各种加密原语;AES、椭圆曲线、MAC、公钥加密等。

