91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

如何在C++中實現buffer的加密解密功能

c++
小樊
81
2024-08-07 20:03:25
欄目: 編程語言

在C++中實現buffer的加密解密功能通常使用加密算法庫,例如OpenSSL或Crypto++。以下是一個使用OpenSSL庫實現buffer的加密解密功能的示例代碼:

#include <openssl/evp.h>
#include <openssl/aes.h>

void encryptBuffer(const unsigned char* buffer, size_t bufferSize, const unsigned char* key, const unsigned char* iv, unsigned char* encryptedBuffer) {
    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);

    int len;
    int encryptedSize;

    EVP_EncryptUpdate(ctx, encryptedBuffer, &len, buffer, bufferSize);
    encryptedSize = len;

    EVP_EncryptFinal_ex(ctx, encryptedBuffer + len, &len);
    encryptedSize += len;

    EVP_CIPHER_CTX_free(ctx);
}

void decryptBuffer(const unsigned char* encryptedBuffer, size_t encryptedBufferSize, const unsigned char* key, const unsigned char* iv, unsigned char* decryptedBuffer) {
    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);

    int len;
    int decryptedSize;

    EVP_DecryptUpdate(ctx, decryptedBuffer, &len, encryptedBuffer, encryptedBufferSize);
    decryptedSize = len;

    EVP_DecryptFinal_ex(ctx, decryptedBuffer + len, &len);
    decryptedSize += len;

    EVP_CIPHER_CTX_free(ctx);
}

int main() {
    unsigned char key[] = "0123456789abcdef";
    unsigned char iv[] = "abcdefghijklmnop";

    unsigned char buffer[] = "Hello, world!";
    size_t bufferSize = strlen((const char*)buffer);
    size_t encryptedBufferSize = bufferSize + AES_BLOCK_SIZE;

    unsigned char encryptedBuffer[encryptedBufferSize];
    unsigned char decryptedBuffer[bufferSize];

    encryptBuffer(buffer, bufferSize, key, iv, encryptedBuffer);
    decryptBuffer(encryptedBuffer, encryptedBufferSize, key, iv, decryptedBuffer);

    decryptedBuffer[bufferSize] = '\0';
    printf("Decrypted buffer: %s\n", decryptedBuffer);

    return 0;
}

在這個示例中,我們使用AES加密算法對buffer進行加密和解密。首先使用encryptBuffer函數對buffer進行加密,然后使用decryptBuffer函數對加密后的buffer進行解密。在主函數中,我們提供了一個簡單的測試,對加密后的buffer進行解密并輸出結果。

0
麻阳| 依安县| 洛宁县| 岳池县| 科尔| 孝昌县| 云龙县| 南京市| 桂平市| 孟津县| 鹿泉市| 朝阳市| 丹凤县| 巩义市| 灯塔市| 华容县| 黄山市| 阿图什市| 平顶山市| 台安县| 庆阳市| 伊金霍洛旗| 大足县| 同心县| 漳浦县| 广水市| 怀集县| 祁阳县| 阿坝县| 桐梓县| 蓝田县| 水城县| 石阡县| 德阳市| 乳山市| 新化县| 黄山市| 阿合奇县| 临泽县| 洞口县| 珲春市|