在Linux系統中,C++可以通過多種方式實現加密。以下是一些常用的加密庫和方法:
OpenSSL是一個強大的開源加密庫,提供了廣泛的加密算法和工具。
sudo apt-get update
sudo apt-get install libssl-dev
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <iostream>
#include <vector>
void encrypt(const std::string& plaintext, std::vector<unsigned char>& ciphertext) {
AES_KEY enc_key;
unsigned char key[AES_KEYLEN_256];
unsigned char iv[AES_BLOCK_SIZE];
// Generate a random key and IV
if (!RAND_bytes(key, sizeof(key)) || !RAND_bytes(iv, sizeof(iv))) {
throw std::runtime_error("Failed to generate key or IV");
}
// Set up the encryption key
AES_set_encrypt_key(key, sizeof(key) * 8, &enc_key);
// Allocate space for the ciphertext
ciphertext.resize(plaintext.size() + AES_BLOCK_SIZE);
// Perform the encryption
AES_cbc_encrypt(reinterpret_cast<const unsigned char*>(plaintext.c_str()),
ciphertext.data(),
plaintext.size(),
&enc_key,
iv,
AES_ENCRYPT);
}
void decrypt(const std::vector<unsigned char>& ciphertext, std::string& plaintext) {
AES_KEY dec_key;
unsigned char key[AES_KEYLEN_256];
unsigned char iv[AES_BLOCK_SIZE];
// Use the same key and IV used for encryption
// In a real application, you should store the key and IV securely
if (!RAND_bytes(key, sizeof(key)) || !RAND_bytes(iv, sizeof(iv))) {
throw std::runtime_error("Failed to generate key or IV");
}
// Set up the decryption key
AES_set_decrypt_key(key, sizeof(key) * 8, &dec_key);
// Allocate space for the decrypted text
plaintext.resize(ciphertext.size());
// Perform the decryption
AES_cbc_encrypt(ciphertext.data(),
reinterpret_cast<unsigned char*>(&plaintext),
ciphertext.size(),
&dec_key,
iv,
AES_DECRYPT);
}
int main() {
std::string plaintext = "Hello, World!";
std::vector<unsigned char> ciphertext;
try {
encrypt(plaintext, ciphertext);
std::cout << "Ciphertext: ";
for (auto byte : ciphertext) {
std::cout << std::hex << byte;
}
std::cout << std::endl;
std::string decrypted_text;
decrypt(ciphertext, decrypted_text);
std::cout << "Decrypted text: " << decrypted_text << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
Crypto++是一個純C++編寫的加密庫,提供了豐富的加密算法和工具。
sudo apt-get update
sudo apt-get install libcrypto++-dev
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/filters.h>
#include <iostream>
#include <vector>
void encrypt(const std::string& plaintext, std::vector<unsigned char>& ciphertext) {
CryptoPP::AES::Encryption aesEncryption((const byte*)key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, (const byte*)iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
stfEncryptor.Put(reinterpret_cast<const byte*>(plaintext.c_str()), plaintext.size());
stfEncryptor.MessageEnd();
}
void decrypt(const std::vector<unsigned char>& ciphertext, std::string& plaintext) {
CryptoPP::AES::Decryption aesDecryption((const byte*)key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, (const byte*)iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(plaintext));
stfDecryptor.Put(ciphertext.data(), ciphertext.size());
stfDecryptor.MessageEnd();
}
int main() {
std::string key = "0123456789abcdef"; // 128-bit key
std::string iv = "abcdef0123456789"; // 128-bit IV
std::string plaintext = "Hello, World!";
std::vector<unsigned char> ciphertext;
try {
encrypt(plaintext, ciphertext);
std::cout << "Ciphertext: ";
for (auto byte : ciphertext) {
std::cout << std::hex << byte;
}
std::cout << std::endl;
std::string decrypted_text;
decrypt(ciphertext, decrypted_text);
std::cout << "Decrypted text: " << decrypted_text << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
GnuPG是一個用于加密和解密的工具,也可以通過命令行或編程接口使用。
sudo apt-get update
sudo apt-get install gnupg
#include <iostream>
#include <string>
#include <cstdlib>
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
pclose(pipe);
return result;
}
void encrypt(const std::string& plaintext, const std::string& recipient) {
std::string cmd = "echo -n '" + plaintext + "' | gpg --encrypt --recipient " + recipient;
std::string ciphertext = exec(cmd.c_str());
std::cout << "Ciphertext: " << ciphertext << std::endl;
}
void decrypt(const std::string& ciphertext, const std::string& passphrase) {
std::string cmd = "echo -n '" + ciphertext + "' | gpg --decrypt --passphrase-fd 0";
setenv("GNUPG_PASSPHRASE", passphrase, 1);
std::string decrypted_text = exec(cmd.c_str());
std::cout << "Decrypted text: " << decrypted_text << std::endl;
}
int main() {
std::string plaintext = "Hello, World!";
std::string recipient = "your-recipient@example.com";
std::string passphrase = "your-passphrase";
try {
encrypt(plaintext, recipient);
std::string ciphertext = "your-ciphertext-here"; // Replace with actual ciphertext
decrypt(ciphertext, passphrase);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
以上是幾種在Linux系統中使用C++進行加密的方法。選擇哪種方法取決于你的具體需求和環境。