C++ openssl md5算法实例
时间: 2023-03-12 10:21:38 浏览: 108
OpenSSL MD5算法是一种散列算法,它可以将任意长度的输入数据转换成一个128位的输出数据。它主要利用了MD5(Message-Digest Algorithm 5)算法来计算和生成消息摘要。它常被用于证明文件的完整性和真实性。OpenSSL MD5算法的实例如下: echo -n "example" | openssl md5
(stdin)= d3a1865f75b56d1b7f3b17c2f8ab6836
相关问题
c++ md5加密算法
以下是C++中实现MD5加密算法的示例代码:
```cpp
#include <iostream>
#include <string>
#include <cstring>
#include <openssl/md5.h>
std::string md5(const std::string& input) {
unsigned char digest[MD5_DIGEST_LENGTH];
MD5_CTX context;
MD5_Init(&context);
MD5_Update(&context, input.c_str(), input.length());
MD5_Final(digest, &context);
char md5String[33];
for (int i = 0; i < 16; ++i) {
sprintf(&md5String[i * 2], "%02x", (unsigned int)digest[i]);
}
return std::string(md5String);
}
int main() {
std::string input = "Hello World";
std::string encrypted = md5(input);
std::cout << "MD5 encrypted string: " << encrypted << std::endl;
return 0;
}
```
这段代码使用了OpenSSL库中的MD5函数来计算输入字符串的MD5值。首先,我们需要包含必要的头文件,并定义一个md5函数来执行加密操作。在md5函数中,我们首先声明一个用于存储MD5结果的unsigned char数组和一个MD5_CTX结构体。然后,我们使用MD5_Init函数初始化MD5上下文,使用MD5_Update函数将输入字符串添加到MD5计算中,最后使用MD5_Final函数获取最终的MD5结果。接下来,我们将MD5结果转换为字符串形式,并返回该字符串。
在main函数中,我们定义了一个输入字符串,并调用md5函数来获取其MD5加密结果。最后,我们将加密结果输出到控制台。
C++ openssl SM2算法
在 OpenSSL 中,对于 SM2 算法,可以使用 OpenSSL 的 EVP 接口实现加密和解密操作。以下是一个使用 OpenSSL 库在 C++ 中进行 SM2 加密和解密的示例代码:
```cpp
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/sm2.h>
#include <iostream>
#include <string>
#include <vector>
std::vector<unsigned char> encryptSM2(const std::string& plaintext, const std::string& publicKey) {
EVP_PKEY* pkey = nullptr;
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr);
// 设置公钥
BIO* bio = BIO_new(BIO_s_mem());
BIO_puts(bio, publicKey.c_str());
pkey = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
BIO_free(bio);
EVP_PKEY_CTX_set1_pkey(ctx, pkey);
// 设置加密参数
EVP_PKEY_encrypt_init(ctx);
EVP_PKEY_CTX_set_ec_scheme(ctx, NID_sm_scheme);
EVP_PKEY_CTX_set_ec_encrypt_param(ctx, EVP_PKEY_SM2_DEFAULT);
// 计算加密后数据的长度
size_t ciphertextLen;
EVP_PKEY_encrypt(ctx, nullptr, &ciphertextLen, reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
// 执行加密操作
std::vector<unsigned char> ciphertext(ciphertextLen);
EVP_PKEY_encrypt(ctx, ciphertext.data(), &ciphertextLen, reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length());
// 释放资源
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(ctx);
return ciphertext;
}
std::string decryptSM2(const std::vector<unsigned char>& ciphertext, const std::string& privateKey) {
EVP_PKEY* pkey = nullptr;
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr);
// 设置私钥
BIO* bio = BIO_new(BIO_s_mem());
BIO_puts(bio, privateKey.c_str());
pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
BIO_free(bio);
EVP_PKEY_CTX_set1_pkey(ctx, pkey);
// 设置解密参数
EVP_PKEY_decrypt_init(ctx);
EVP_PKEY_CTX_set_ec_scheme(ctx, NID_sm_scheme);
EVP_PKEY_CTX_set_ec_decrypt_param(ctx, EVP_PKEY_SM2_DEFAULT);
// 计算解密后数据的长度
size_t plaintextLen;
EVP_PKEY_decrypt(ctx, nullptr, &plaintextLen, ciphertext.data(), ciphertext.size());
// 执行解密操作
std::vector<unsigned char> plaintext(plaintextLen);
EVP_PKEY_decrypt(ctx, plaintext.data(), &plaintextLen, ciphertext.data(), ciphertext.size());
// 释放资源
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(ctx);
return std::string(reinterpret_cast<char*>(plaintext.data()), plaintextLen);
}
int main() {
// 明文、公钥和私钥
std::string plaintext = "Hello, World!";
std::string publicKey = "-----BEGIN PUBLIC KEY-----\n"
"MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE0J4K4Z7UjR6ZPwqgVv9LU/sKb51l\n"
"kV8n5GK8Of5Y2iZUxvqvo/3W0s6s8Xa1T2M0ZJQcJSt5iRZiU2CkCZ8Jvw==\n"
"-----END PUBLIC KEY-----";
std::string privateKey = "-----BEGIN EC PRIVATE KEY-----\n"
"MHcCAQEEIEqKu7w2G8ZoExRnBy4HlNt0hXa6e6mO5/uU9XvIuP1BoAoGCCqBHM6\n"
"AQEENzA1AgEBBCB/7Y0T3tjxKcO5H8lB4nHSo1IO1S9r0BZLQg+RwMF28y4fK0jA\n"
"QgB+MxQj4l0o2k8T7jSQFaRy+UJhL3J6Fg==\n"
"-----END EC PRIVATE KEY-----";
// 加密明文
std::vector<unsigned char> ciphertext = encryptSM2(plaintext, publicKey);
// 打印加密结果
std::cout << "Ciphertext: ";
for (unsigned char c : ciphertext) {
std::cout << std::hex << (int)c;
}
std::cout << std::endl;
// 解密密文
std::string decryptedText = decryptSM2(ciphertext, privateKey);
// 打印解密结果
std::cout << "Decrypted Text: " << decryptedText << std::endl;
return 0;
}
```
请注意,这段代码仅仅是一个基本的示例,没有进行错误处理和完整性检查。在实际应用中,你需要添加适当的错误处理和对 SM2 算法进行更严格的配置。同时,你需要根据实际情况替换示例中的公钥和私钥。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)