使用openssl进行AES 128 CTR 加密、解密

本文提供了一个使用 OpenSSL 库实现 AES 128 CTR 模式的对称加密和解密的 C 语言示例代码。通过该示例可以了解如何初始化加密上下文、进行加密及解密操作,并展示了完整的加密解密过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文章

https://blog.youkuaiyun.com/yasi_xi/article/details/13997337

对称加密如非对称加密

AES128-CTR方式,属于对称加密的一种。
这里有一篇讲解对称和非对称加密非常通俗易懂的文章:
https://segmentfault.com/a/1190000004461428

代码示例

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

void handleErrors(void)
{
    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;

    int len;

    int ciphertext_len;

    /* Create and initialise the context */
    if (!(ctx = EVP_CIPHER_CTX_new()))
        handleErrors();

    /* Initialise the encryption operation. IMPORTANT - ensure you use a key
     * and IV size appropriate for your cipher
     * In this example we are using 256 bit AES (i.e. a 256 bit key). The
     * IV size for *most* modes is the same as the block size. For AES this
     * is 128 bits */
    if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
        handleErrors();

    /* Provide the message to be encrypted, and obtain the encrypted output.
     * EVP_EncryptUpdate can be called multiple times if necessary
     */
    if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
        handleErrors();
    ciphertext_len = len;

    /* Finalise the encryption. Further ciphertext bytes may be written at
     * this stage.
     */
    if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
        handleErrors();
    ciphertext_len += len;

    /* Clean up */
    EVP_CIPHER_CTX_free(ctx);

    return ciphertext_len;
}

int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
            unsigned char *iv, unsigned char *plaintext)
{
    EVP_CIPHER_CTX *ctx;

    int len;

    int plaintext_len;

    /* Create and initialise the context */
    if (!(ctx = EVP_CIPHER_CTX_new()))
        handleErrors();

    /* Initialise the decryption operation. IMPORTANT - ensure you use a key
     * and IV size appropriate for your cipher
     * In this example we are using 256 bit AES (i.e. a 256 bit key). The
     * IV size for *most* modes is the same as the block size. For AES this
     * is 128 bits */
    if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
        handleErrors();

    /* Provide the message to be decrypted, and obtain the plaintext output.
     * EVP_DecryptUpdate can be called multiple times if necessary
     */
    if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
        handleErrors();
    plaintext_len = len;

    /* Finalise the decryption. Further plaintext bytes may be written at
     * this stage.
     */
    if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
        handleErrors();
    plaintext_len += len;

    /* Clean up */
    EVP_CIPHER_CTX_free(ctx);

    return plaintext_len;
}

int main(int argc, char const *argv[])
{
    /* A 256 bit key */
    unsigned char *key = (unsigned char *)"01234567890123456789012345678901";

    /* A 128 bit IV */
    unsigned char *iv = (unsigned char *)"0123456789012345";

    /* Message to be encrypted */
    unsigned char *plaintext = "The quick brown fox jumps over the lazy dog";

    /* Buffer for ciphertext. Ensure the buffer is long enough for the
     * ciphertext which may be longer than the plaintext, dependant on the
     * algorithm and mode
     */
    unsigned char ciphertext[128];

    /* Buffer for the decrypted text */
    unsigned char decryptedtext[128];

    int decryptedtext_len, ciphertext_len;

    printf("Plaintext is:\n%s~\n", plaintext);

    /* Encrypt the plaintext */
    ciphertext_len = encrypt(plaintext, strlen(plaintext), key, iv,
                             ciphertext);

    /* Do something useful with the ciphertext here */
    printf("Ciphertext is %d bytes long:\n", ciphertext_len);
    BIO_dump_fp(stdout, ciphertext, ciphertext_len);

    /* Decrypt the ciphertext */
    decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
                                decryptedtext);

    /* Add a NULL terminator. We are expecting printable text */
    decryptedtext[decryptedtext_len] = '\0';

    /* Show the decrypted text */
    printf("Decrypted text is:\n");
    printf("%s~\n", decryptedtext);

    return 0;
}

编译

gcc openssl-aes-128-ctr.c -lssl -lcrypto

运行效果

Plaintext is:
The quick brown fox jumps over the lazy dog~
Ciphertext is 43 bytes long:
0000 - 0b 15 95 9f 61 4f e5 29-8d da cf eb 69 db 55 0a   ....aO.)....i.U.
0010 - 00 58 58 fa 7c 6f d0 52-52 53 73 61 1b 08 2e 03   .XX.|o.RRSsa....
0020 - a2 d1 16 0c 98 4b f5 c7-43 6c ca                  .....K..Cl.
Decrypted text is:
The quick brown fox jumps over the lazy dog~
### 使用 Visual Studio Code 在 Kali Linux 上实现恶意软件免杀技巧 #### 重要声明 开发和部署任何类型的恶意软件都是非法且道德不可接受的行为。本回答仅提供技术信息用于教育目的,旨在帮助安全研究人员了解防御机制。 #### 技术背景 Visual Studio Code (VS Code) 是一款强大的多平台代码编辑器,在 Kali Linux 中可以通过多种方式安装并配置环境来编写不同编程语言的应用程序[^1]。对于研究性质的工作而言,理解如何使某些二进制文件绕过基本检测机制具有一定的学习价值。 #### 编写混淆脚本 为了防止被简单特征码扫描工具识别出来,可以采用字符串加密、函数重命名等方式对源代码进行变形处理: ```python import base64 def obfuscate_string(input_str): encoded_bytes = input_str.encode('utf-8') encrypted_data = base64.b64encode(encoded_bytes).decode() return f"exec(__import__('base64').b64decode('{encrypted_data}'))" obfuscated_code = obfuscate_string(""" print("This is an example of string obfuscation.") """) print(obfuscated_code) ``` 此段Python代码展示了基础的Base64编码作为简单的演示[^3]。 #### 修改PE头或其他元数据 针对Windows可移植执行体(Portable Executable, PE),改变其头部结构或者嵌入额外资源能够有效干扰静态分析过程。然而请注意,这类操作通常涉及汇编层面的知识以及特定库的支持,如`pefile` Python模块。 #### 动态加载与反射注入 通过动态链接库(DLL)或内存映射技术载入必要的功能组件而非直接包含于主程序体内;这种方法增加了逆向工程难度的同时也使得传统的基于签名的安全产品难以捕捉到完整的攻击模式[^5]。 #### 创建自定义打包方案 利用PyInstaller等工具将解释型语言转换成独立运行的原生应用,并加入加壳保护措施进一步增强隐蔽效果。不过需要注意的是,过度复杂的封装可能会引起高级防护系统的怀疑。 #### 配置 VS Code 支持上述工作流程 确保已按照官方指南完成VS Code及其扩展插件的设置,以便支持目标编程语言特性及调试需求[^4]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值