温馨提示×

Linux C++如何加密

小樊
43
2026-07-17 06:20:01
栏目: 编程语言

在 Linux 下用 C++ 做“加密”,先明确你要做什么,因为不同场景用的技术完全不同。下面我按常见需求给你一个清晰、实用的指引,并附示例代码。


一、先问自己:你要的是哪种“加密”?

需求 推荐方案
保存密码(不可逆) 哈希 + 盐(SHA-256 / bcrypt / Argon2)
文件 / 数据加密(可逆) AES(对称加密)
网络通信安全 TLS / SSL(OpenSSL)
密钥交换 / 签名 RSA / ECC(非对称加密)
简单混淆(不是真加密) Base64、XOR(不安全)

二、最常见:AES 对称加密(文件 / 数据)

✅ 推荐库:OpenSSL

Linux 几乎都有,C++ 可直接用。

安装

sudo apt install libssl-dev

示例:AES-256-CBC 加密字符串

#include <openssl/evp.h>
#include <openssl/rand.h>
#include <cstring>
#include <iostream>

void handleErrors() {
    ERR_print_errors_fp(stderr);
    abort();
}

int encrypt(
    unsigned char *plaintext,
    int plaintext_len,
    unsigned char *key,
    unsigned char *iv,
    unsigned char *ciphertext
) {
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    if (!ctx) handleErrors();

    if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv))
        handleErrors();

    int len, ciphertext_len;

    if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
        handleErrors();
    ciphertext_len = len;

    if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
        handleErrors();
    ciphertext_len += len;

    EVP_CIPHER_CTX_free(ctx);
    return ciphertext_len;
}

int main() {
    unsigned char key[32];     // 256-bit
    unsigned char iv[16];      // 128-bit

    RAND_bytes(key, sizeof(key));
    RAND_bytes(iv, sizeof(iv));

    unsigned char plaintext[] = "Hello Linux C++ Encryption";
    unsigned char ciphertext[128];

    int len = encrypt(plaintext, strlen((char*)plaintext), key, iv, ciphertext);

    std::cout << "Encrypted length: " << len << std::endl;

    return 0;
}

编译:

g++ aes_example.cpp -o aes_example -lssl -lcrypto

三、密码存储(不可逆):哈希 + 盐

✅ 不推荐:MD5 / SHA1

✅ 推荐:SHA-256 + salt 或 bcrypt

SHA-256 示例(OpenSSL)

#include <openssl/sha.h>
#include <iostream>

int main() {
    const char *password = "mypassword";
    unsigned char hash[SHA256_DIGEST_LENGTH];

    SHA256((unsigned char*)password, strlen(password), hash);

    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
        printf("%02x", hash[i]);
    putchar('\n');
}

⚠️ 真实项目中一定要加盐!


四、非对称加密(RSA / ECC)

适合:

  • 登录认证
  • 密钥交换
  • 数字签名

示例:生成 RSA 密钥(命令行)

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem

C++ 使用 OpenSSL API 较复杂,一般:

  • OpenSSL CLI
  • 或用 libsodium(更简单)

五、现代推荐:libsodium(更简单、安全)

安装

sudo apt install libsodium-dev

示例:简单加密

#include <sodium.h>
#include <cstdio>

int main() {
    sodium_init();

    unsigned char key[crypto_secretbox_KEYBYTES];
    unsigned char nonce[crypto_secretbox_NONCEBYTES];
    unsigned char plaintext[] = "Secret";
    unsigned char ciphertext[sizeof(plaintext) + crypto_secretbox_MACBYTES];

    randombytes_buf(key, sizeof(key));
    randombytes_buf(nonce, sizeof(nonce));

    crypto_secretbox_easy(
        ciphertext,
        plaintext,
        sizeof(plaintext),
        nonce,
        key
    );

    printf("Encrypted successfully\n");
}

编译:

g++ sodium_example.cpp -lsodium

libsodium 几乎是 Linux C++ 加密首选


六、⚠️ 重要安全建议

❌ 不要自己实现加密算法
❌ 不要用 rand() 生成密钥
❌ 不要硬编码密钥
✅ 使用成熟库(OpenSSL / libsodium)
✅ 对称加密一定要随机 IV / nonce


七、我可以继续帮你

你可以直接告诉我:

  1. 加密什么?(文件 / 字符串 / 密码 / 网络)
  2. 是否需要解密?
  3. 是否用于生产环境?
  4. 是否已有 OpenSSL / libsodium?

我可以直接给你完整可用的工程级代码

0