如何使用 OpenSSL 进行 AES 解密

How to do AES decryption using OpenSSL(如何使用 OpenSSL 进行 AES 解密)
本文介绍了如何使用 OpenSSL 进行 AES 解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想使用 OpenSSL 库来解密一些 AES 数据.该代码可以访问密钥.这个项目已经将 libopenssl 用于其他用途,所以我想坚持使用这个库.

I'd like to use the OpenSSL library to decrypt some AES data. The code has access to the key. This project already uses libopenssl for something else, so I'd like to stick to this library.

我直接查看了 /usr/include/openssl/aes.h,因为 OpenSSL 站点的文档很少.唯一的解密函数是这个:

I went looking directly into /usr/include/openssl/aes.h since the OpenSSL site is light on documentation. The only decrypt function is this one:

void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key);

不幸的是,这没有办法指定 in 指针的长度,所以我不确定它是如何工作的.

Unfortunately, this doesn't have a way to specify the length of the in pointer, so I'm not sure how that would work.

我相信还有其他几个函数采用数字参数来区分加密和解密.例如:

There are several other functions which I believe take a numeric parm to differentiate between encryption and decryption. For example:

void AES_ecb_encrypt(*in, *out, *key, enc);
void AES_cbc_encrypt(*in, *out, length, *key, *ivec, enc);
void AES_cfb128_encrypt(*in, *out, length, *key, *ivec, *num, enc);
void AES_cfb1_encrypt(*in, *out, length, *key, *ivec, *num, enc);
void AES_cfb8_encrypt(*in, *out, length, *key, *ivec, *num, enc);
void AES_cfbr_encrypt_block(*in, *out, nbits, *key, *ivec, enc);
void AES_ofb128_encrypt(*in, *out, length, *key, *ivec, *num);
void AES_ctr128_encrypt(*in, *out, length, *key, ivec[], ecount_buf[], *num);
void AES_ige_encrypt(*in, *out, length, *key, *ivec, enc);
void AES_bi_ige_encrypt(*in, *out, length, *key, *key2, *ivec, enc);

根据我使用 Google 的理解,enc 参数设置为 AES_ENCRYPTAES_DECRYPT 以指定需要执行的操作.

From what I understand using Google, the enc parm gets set to AES_ENCRYPT or AES_DECRYPT to specify which action needs to take place.

这让我想到了两个问题:

Which brings me to my 2 questions:

  1. 这些名字是什么意思?什么是 ecb、cbc、cfb128 等...,我该如何决定应该使用哪一个?
  2. 大多数这些所需的 unsigned char *ivec 参数是什么,我从哪里得到它?
  1. What do these names mean? What is ecb, cbc, cfb128, etc..., and how do I decide which one I should be using?
  2. What is the unsigned char *ivec parm needed for most of these, and where do I get it from?

推荐答案

没有给出大小,因为 AES 的块大小是固定的基于密钥大小;您发现了 ECB 模式实现,它不适合直接使用(除了作为一种教学工具).

There's no size given because the block sizes for AES are fixed based on the key size; you've found the ECB mode implementation, which isn't suitable for direct use (except as a teaching tool).

ECB、CBC、CFB128 等都是常用的操作模式的简称.它们有不同的属性,但如果你从不接触 ECB 模式,你应该没问题.

ECB, CBC, CFB128, etc, are all short names for the modes of operation that are in common use. They have different properties, but if you never touch ECB mode, you should be alright.

我建议远离底层代码;如果可以,请改用 EVP_* 接口将其中一些决定移动到文本配置文件中,以便您的用户可以轻松地在不同的密码、块大小和操作模式之间进行选择,如果有充分的理由改变默认设置.

I suggest staying further away from the low-level code; use the EVP_* interfaces instead, if you can, and you can move some of these decisions into a text configuration file, so your users could easily select between the different ciphers, block sizes, and modes of operation if there should ever be a good reason to change away from the defaults.

我的同情,OpenSSL 文档感觉比实际情况更糟,而且也不是那么好.您可能会发现OpenSSL 的网络安全是一本有用的书.我希望我上次需要使用 OpenSSL 时能早点找到它.(不要被愚蠢的标题欺骗了——它应该被命名为OpenSSL".哦,好吧.)

My sympathies, OpenSSL documentation feels worse than it is, and it isn't that great. You may find Network Security with OpenSSL a useful book. I wish I had found it sooner the last time I needed to use OpenSSL. (Don't let the silly title fool you -- it should have been titled just "OpenSSL". Oh well.)

编辑我忘了提及初始化向量.它们用于确保如果您使用相同的密钥加密相同的数据,则密文不会相同.您需要 IV 来解密数据,但您不需要对 IV 保密.您应该为每个会话随机生成一个(并将其与 RSA 或 El Gamal 或 DH 加密的会话密钥一起发送),或者在两个端点上以相同的方式生成它,或者将其与文件一起存储在本地,诸如此类.

Edit I forgot to mention the initialization vectors. They are used to make sure that if you encrypt the same data using the same key, the ciphertext won't be identical. You need the IV to decrypt the data, but you don't need to keep the IV secret. You should either generate one randomly for each session (and send it along with an RSA or El Gamal or DH-encrypted session key) or generate it identically on both endpoints, or store it locally with the file, something like that.

这篇关于如何使用 OpenSSL 进行 AES 解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)