使用openssl命令加解密 aes-128-cbc的简单示例

网上找了下openssl 加解密 aes-128-cbc相关命令, 发现都比较含糊, 这里是摸索出的一个aes-12b-cbc加解密的实例.

  1. 将要加密的内容输入到plain.txt
    echo "1234567890abc" > plain.txt

  2. 使用openssl加密. -p 表示打印出加密用的salt, key, iv. salt就是所谓的加盐, 防止同样的内容产生同样的加密数据. iv和key是openssl 的cbc模式需要的参数. 关于aes加密, 详情可见漫画:什么是 AES 算法?,漫画:AES 算法的底层原理, AES加密算法动画演示.
    openssl enc -aes-128-cbc -in plain.txt -out encrypt.txt -iv f123 -K 1223 -p
    salt=E0DEB1EAFE7F0000
    key=12230000000000000000000000000000
    iv =F1230000000000000000000000000000

  3. 输出加密前和加密后内容的十六进制. 这里使用xxd和hexdump都可以.
    xxd plain.txt
    00000000: 3132 3334 3536 3738 3930 6162 630a 1234567890abc.
    xxd encrypt.txt
    00000000: c5af 18cb ddee 9923 0374 6a21 9bb6 3f99 …#.tj!..?.

  4. 解密加密后的数据
    openssl aes-128-cbc -d -in encrypt.txt -out encrypt_decrypt.txt -S E0DEB1EAFE7F0000 -iv F1230000000000000000000000000000 -K 12230000000000000000000000000000
    -S salt Salt to use, specified as a hexidecimal string
    -salt Use a salt in the key derivation routines (default)

  5. 查看解密后的数据和原始数据是否一致.
    xxd encrypt_decrypt.txt
    00000000: 3132 3334 3536 3738 3930 6162 630a 1234567890abc.

  • 11
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是示例代码,使用 OpenSSL 库进行 AES-CBC 加密和解密: 加密: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> void aes_encrypt_cbc(const unsigned char *plaintext, int plaintext_len, const unsigned char *key, const unsigned char *iv, unsigned char *ciphertext) { AES_KEY aes_key; AES_set_encrypt_key(key, 128, &aes_key); AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aes_key, iv, AES_ENCRYPT); } int main() { const unsigned char *plaintext = (const unsigned char *)"待加密的明文"; const unsigned char *key = (const unsigned char *)"密钥"; const unsigned char *iv = (const unsigned char *)"初始化向量"; unsigned char ciphertext[AES_BLOCK_SIZE * ((strlen((const char *)plaintext) + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE)]; memset(ciphertext, 0, sizeof(ciphertext)); aes_encrypt_cbc(plaintext, strlen((const char *)plaintext), key, iv, ciphertext); printf("加密结果:"); for (int i = 0; i < sizeof(ciphertext); i++) { printf("%02x", ciphertext[i]); } printf("\n"); return 0; } ``` 解密: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> void aes_decrypt_cbc(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key, const unsigned char *iv, unsigned char *plaintext) { AES_KEY aes_key; AES_set_decrypt_key(key, 128, &aes_key); AES_cbc_encrypt(ciphertext, plaintext, ciphertext_len, &aes_key, iv, AES_DECRYPT); } int main() { const unsigned char *ciphertext = (const unsigned char *)"待解密的密文"; const unsigned char *key = (const unsigned char *)"密钥"; const unsigned char *iv = (const unsigned char *)"初始化向量"; unsigned char plaintext[AES_BLOCK_SIZE * ((strlen((const char *)ciphertext) + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE)]; memset(plaintext, 0, sizeof(plaintext)); aes_decrypt_cbc(ciphertext, strlen((const char *)ciphertext), key, iv, plaintext); printf("解密结果:%s\n", plaintext); return 0; } ``` 需要注意的是,以上代码使用的是 CBC 模式和 PKCS7 填充方式。同时,需要在编译时链接 OpenSSL 库,可以使用以下命令进行编译: ``` gcc aes_cbc_encrypt.c -o aes_cbc_encrypt -lcrypto gcc aes_cbc_decrypt.c -o aes_cbc_decrypt -lcrypto ``` 其中,`-lcrypto` 参数表示链接 OpenSSL 库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值