Bootstrap

利用openssl对数据进行md5加密 (Linux/C++)

一、Linux进行md5加密

1、md5加密

(1)交互式输入

$ openssl 

OpenSSL> md5       
1234abcd(stdin)= ef73781effc5774100f87fe2f437a435  #输入完后按crtl+d三次

OpenSSL> md5
1234abcd                                           
(stdin)= 3d7e6a56bd7757aee22a30a1ca75cdd0 #输入完后按回车,再按crtl+d两次,MD5计算时包含了回车符

(2)管道标准输入处理

$ echo 1234abcd | openssl md5 #MD5计算时包含了回车符
(stdin)= 3d7e6a56bd7757aee22a30a1ca75cdd0

$ echo -n 1234abcd | openssl md5  #-n 不带回车符,才是正确的
(stdin)= ef73781effc5774100f87fe2f437a435

(3)对文件处理

$ cat test.log
1234bacd

$ openssl md5 test.log
MD5(test.log)= 3d7e6a56bd7757aee22a30a1ca75cdd0

2、md5sum

$ echo 1234abcd | md5sum
3d7e6a56bd7757aee22a30a1ca75cdd0  -

$ echo -n 1234abcd | md5sum
ef73781effc5774100f87fe2f437a435  -

$ md5sum test.log
3d7e6a56bd7757aee22a30a1ca75cdd0  test.log

3、SHA-1

$ echo '1234abcd' | openssl sha1
(stdin)= ebfc3c70862c391e281495fa84b14bc90c9aec55

$ openssl sha1 test.log
SHA1(test.log)= ebfc3c70862c391e281495fa84b14bc90c9aec55
#Calculate SHA-1 checksum
shasum -a 1 /tmp/a.txt
#Calculate SHA-256 checksum
shasum -a 256 /tmp/a.txt

 4、base64加密/解密

$ echo 1234abcd |openssl base64 #加密
MTIzNGFiY2QK
$ echo MTIzNGFiY2QK | openssl base64 -d #解密
1234abcd

$ openssl base64 -in test.log
MTIzNGFiY2QK
$ openssl base64 -in test.log >> /tmp/tmp.log #加密
$ openssl base64 -d -in /tmp/tmp.log #解密
1234abcd

5、AES加密/解密

$ echo '1234abcd' |openssl aes-128-cbc -k 000 -base64 #使用密钥000,输出结果以base64编码格式给出
U2FsdGVkX1+saPmAIQv2DCNJBWGgD8KQeqEvD46rfQs=

$ echo 'U2FsdGVkX1+saPmAIQv2DCNJBWGgD8KQeqEvD46rfQs=' | openssl aes-128-cbc -d -k 000 -base64
1234abcd

6、DES3加密/解密

$ echo '1234abcd' |openssl des3 -k 000 -base64 #使用密钥000,输出结果以base64编码格式给出
U2FsdGVkX1/NS1u8ZwltRyX2BEzXzFxn3GML+nnbjvw=
$ echo 'U2FsdGVkX1/NS1u8ZwltRyX2BEzXzFxn3GML+nnbjvw=' | openssl des3 -d -k 000 -base64
1234abcd

二、C++进行md5加密

 main.cc:

#include <iostream>
#include <string.h>
#include <openssl/md5.h>

int main(int argc, char *argv[]) {
    const char data[] = "1234abcd";
    unsigned char md5[MD5_DIGEST_LENGTH];

    // 第一种方式
    MD5(reinterpret_cast<const unsigned char*>(data), strlen(data), md5); //直接产生字符串的MD5

    // 第二种方式
    // MD5_CTX ctx;
    // MD5_Init(&ctx); //初始化MD5上下文结构
    // MD5_Update(&ctx, data, strlen(data)); //刷新MD5,将文件连续数据分片放入进行MD5刷新
    // MD5_Final(md5, &ctx); //产生最终的MD5数据

    std::string md5_hex;
    const char map[] = "0123456789abcdef";
    for (size_t i = 0; i < MD5_DIGEST_LENGTH; ++i) {
        std::cout << int(md5[i]) << " ";
        md5_hex += map[md5[i] / 16];
        md5_hex += map[md5[i] % 16];
    }
    std::cout << "\n" << md5_hex << std::endl;
    return 0;
}

main.cc:(加密文件)

#include <iostream>
#include <fstream>
#include <string.h>
#include <openssl/md5.h>

int main(int argc, char *argv[]) {
    std::string file_path = "/tmp/test.log";
    std::ifstream fin(file_path.c_str(), std::ios::in | std::ios::binary);
    if (fin.fail()) {
        return -1;    
    }

    unsigned char md5[MD5_DIGEST_LENGTH];
    MD5_CTX ctx;
    MD5_Init(&ctx);
    std::string line;
    while(getline(fin, line))
    {
        MD5_Update(&ctx, line.c_str(), line.size());
    }       
    MD5_Final(md5, &ctx);

    std::string md5_hex;
    const char map[] = "0123456789abcdef";
    for (size_t i = 0; i < MD5_DIGEST_LENGTH; ++i) {
        std::cout << int(md5[i]) << " ";
        md5_hex += map[md5[i] / 16];
        md5_hex += map[md5[i] % 16];
    }
    std::cout << "\n" << md5_hex << std::endl;
    return 0;
}

 CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(test)

set(CMAKE_CXX_STANDARD 11)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

find_package(OpenSSL REQUIRED)

add_executable(test main.cc)
target_link_libraries(test OpenSSL::SSL)

输出:

239 115 120 30 255 197 119 65 0 248 127 226 244 55 164 53 
ef73781effc5774100f87fe2f437a435

说明:

一定要使用unsigned char,和char的区别是,unsigned char 0~255 ,char -127~127,使用char会出问题。

这个MD5加密函数,返回16个十进制数,范围在0~255间,把它转换为十六进制就是32位的md5编码了。

 

参考:

https://www.openssl.org/docs/man1.1.0/man3/MD5_Init.html

How can I use openssl/md5 in C++ to crypt a string?

Include OpenSSL in a CMakeList.txt file

;