openssl解密字符串c++代码
时间: 2023-11-15 22:59:14 浏览: 166
以下是使用OpenSSL在C++中解密字符串的代码示例:
```
#include <openssl/aes.h>
#include <string.h>
#include <iostream>
int main() {
unsigned char key[] = "12345678";
const int BITS = 128;
unsigned char ciphertext[] = "encrypted string";
std::cout << "密文:\n" << ciphertext << std::endl;
/* 解密 */
AES_KEY aes_key_de;
unsigned char plaintext[17] = {'\0'};
AES_set_decrypt_key(key, BITS, &aes_key_de);
AES_decrypt(ciphertext , plaintext, &aes_key_de);
std::cout << "解密:\n" << plaintext << std::endl;
return 0;
}
```
其中,key是用于加密和解密的密钥,BITS是密钥的位数,ciphertext是需要解密的字符串。在解密过程中,使用AES_set_decrypt_key函数设置解密密钥,然后使用AES_decrypt函数进行解密操作。解密后的明文存储在plaintext数组中。
相关问题
c++ openssl rsa 字符串公私钥加解密示例代码
在C++中使用OpenSSL库处理RSA加密解密,你需要包含相关的头文件,并利用`BIO`和`RSA`结构来操作公钥和私钥。下面是一个简单的示例,展示了如何生成密钥对、加密字符串、然后用私钥解密:
```cpp
#include <openssl/bio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <string>
// 生成RSA密钥对
std::string generate_key_pair() {
RSA *rsa = RSA_new();
BIGNUM *e = BN_new(), *n = BN_new();
// 生成大素数n和e(通常e=65537)
BN_rand_range(n, RSA_NBITS);
BN_set_word(e, RSA_F4);
// 创建RSA结构并设置参数
RSA_generate_key_ex(rsa, RSA modulus bits, e, n, NULL, NULL);
// 将公钥和私钥转换为PEM格式的字符串
BIO* bio_pub = BIO_new(BIO_s_mem());
BIO* bio_priv = BIO_new(BIO_s_mem());
PEM_write_bio_RSA_PUBKEY(bio_pub, rsa);
PEM_write_bio_RSAPrivateKey(bio_priv, rsa, NULL, NULL);
std::string pub_str = BIO_get_mem_data(bio_pub), priv_str = BIO_get_mem_data(bio_priv);
RSA_free(rsa);
BN_free(e);
BN_free(n);
BIO_free_all(bio_pub);
BIO_free_all(bio_priv);
return "Public Key:\n" + pub_str + "\nPrivate Key:\n" + priv_str;
}
// 加密函数
std::string encrypt(const std::string& plaintext, const std::string& public_key) {
RSA *rsaPub = RSA_new();
rsaPub = PEM_read_RSA_PUBKEY(RSA_new(), (char*)public_key.c_str(), NULL, NULL);
BIO* bio_in = BIO_new(BIO_f_base64());
BIO* bio_out = BIO_new(BIO_s_mem());
BIO_write(bio_in, plaintext.c_str(), plaintext.length());
BIO_push(bio_out, bio_in);
RSA_public_encrypt(plaintext.size(), BIO_get_mem_ptr(bio_out), rsaPub, RSA_PKCS1_OAEP_PADDING);
std::string ciphertext = BIO_get_mem_data(bio_out);
BIO_free_all(bio_out);
RSA_free(rsaPub);
return ciphertext;
}
// 解密函数
std::string decrypt(const std::string& ciphertext, const std::string& private_key) {
RSA *rsaPri = RSA_new();
rsaPri = PEM_read_PrivateKey(RSA_new(), (char*)private_key.c_str(), NULL, NULL);
BIO* bio_in = BIO_new(BIO_f_base64());
BIO* bio_out = BIO_new(BIO_s_mem());
BIO_write(bio_in, ciphertext.c_str(), ciphertext.length());
BIO_push(bio_out, bio_in);
RSA_private_decrypt(ciphertext.size(), BIO_get_mem_ptr(bio_in), rsaPri, RSA_PKCS1_OAEP_PADDING);
std::string plaintext = BIO_get_mem_data(bio_out);
BIO_free_all(bio_out);
RSA_free(rsaPri);
return plaintext;
}
int main() {
auto key_pairs = generate_key_pair();
std::cout << "Keys generated.\n";
std::string encrypted_text = encrypt("Hello, World!", key_pairs.substr(key_pairs.find("\n")+1));
std::cout << "Text encrypted.\n";
std::string decrypted_text = decrypt(encrypted_text, key_pairs.substr(0, key_pairs.find("\n")));
std::cout << "Decrypted text: " << decrypted_text << std::endl;
return 0;
}
```
注意:这个示例仅供参考,实际应用中你需要处理错误检查和异常处理。此外,为了安全存储和传输私钥,建议将其保存在安全的地方,而不是明文显示。
c++ openssl aes ecb加解密长字符串 示例代码
AES (Advanced Encryption Standard) 是一种对称加密算法,而 ECB (Electronic Codebook) 模式是最简单的加密模式,它将明文分块并在不解密的情况下直接存储。在 OpenSSL 中,我们可以使用 `AES-ECB` 对长字符串进行加密和解密。以下是一个基本的 C++ 示例代码:
```cpp
#include <openssl/aes.h>
#include <string>
std::string encrypt(const std::string &plaintext, const unsigned char *key, const size_t key_length) {
AES_KEY aes_key;
AES_set_encrypt_key(key, key_length * 8, &aes_key); // 设置密钥
int len = plaintext.length();
unsigned char iv[AES_BLOCK_SIZE]; // 初始化向量,对于ECB可以任意设置,这里是0
memset(iv, 0, AES_BLOCK_SIZE);
unsigned char ciphertext[len + AES_BLOCK_SIZE];
int ciphertext_len;
AES_cbc_encrypt(reinterpret_cast<const unsigned char*>(plaintext.c_str()), ciphertext, len, &aes_key, iv, AES_ENCRYPT);
ciphertext_len = len; // 原长度,因为ECB模式不需要填充
return std::string((const char*)ciphertext, ciphertext_len);
}
std::string decrypt(const std::string &ciphertext, const unsigned char *key, const size_t key_length) {
AES_KEY aes_key;
AES_set_decrypt_key(key, key_length * 8, &aes_key); // 设置密钥
int len = ciphertext.length();
unsigned char iv[AES_BLOCK_SIZE] = {0}; // 使用之前设置的初始化向量
unsigned char decrypted[len];
AES_cbc_encrypt(ciphertext.data(), decrypted, len, &aes_key, iv, AES_DECRYPT);
return std::string((char*)decrypted, len);
}
int main() {
const std::string secret = "这是一个需要加密的长字符串";
unsigned char key[AES_BLOCK_SIZE] = {'A', 'E', 'S', 'K', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
std::string encrypted = encrypt(secret, key, sizeof(key));
std::cout << "Encrypted string: " << encrypted << "\n";
std::string decrypted = decrypt(encrypted, key, sizeof(key));
if (decrypted == secret) {
std::cout << "Decrypted successfully!\n";
} else {
std::cout << "Decryption failed.\n";
}
return 0;
}
```
注意:ECB模式不适合加密大量数据,因为它不提供信息隐藏,每个块都是独立加密的,如果两个块内容相同,加密后的结果也一样,这在实际应用中可能存在安全隐患。在生产环境中,建议使用其他模式如CBC、CTR等。
阅读全文