如何使用OpenSSL做AES解密 [英] How to do AES decryption using OpenSSL

查看:266
本文介绍了如何使用OpenSSL做AES解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenSSL库解密一些AES数据。在code的访问密钥。该项目已经使用libopenssl别的东西,所以我想坚持到这个库。

我就直接寻找到 /usr/include/openssl/aes.h ,因为OpenSSL的网站上的文档光。唯一的解密功能是这一个:

 无效AES_DECRYPT(const的无符号字符*中,无符号字符*出来,常量AES_KEY *键);

不幸的是,这并没有办法在指针指定的的长度,所以我不知道如何将工作。

有,我相信采取一个数字PARM加密和解密区分其他一些功能。例如:

 无效AES_ecb_encrypt(*中,*出来,*键,ENC);
无效AES_cbc_encrypt(*中,*总分,长度,*键,* IVEC,ENC);
无效AES_cfb128_encrypt(*中,*总分,长度,*键,* IVEC,* NUM,ENC);
无效AES_cfb1_encrypt(*中,*总分,长度,*键,* IVEC,* NUM,ENC);
无效AES_cfb8_encrypt(*中,*总分,长度,*键,* IVEC,* NUM,ENC);
无效AES_cfbr_encrypt_block(*中,*总分,NBITS,*键,* IVEC,ENC);
无效AES_ofb128_encrypt(*中,*总分,长度,*键,* IVEC,* NUM);
无效AES_ctr128_encrypt(*中,*总分,长度,*键,IVEC [],ecount_buf [],* NUM);
无效AES_ige_encrypt(*中,*总分,长度,*键,* IVEC,ENC);
无效AES_bi_ige_encrypt(*中,*总分,长度,*键,*键2,* IVEC,ENC);

据我了解使用谷歌的 ENC PARM被设置为 AES_ENCRYPT AES_DECRYPT 来指定哪些行动需要发生。

这让我想起我的2个问题:


  1. 什么这些名字是什么意思?什么是ECB,CBC,cfb128,等等,怎么我决定我应该使用哪一个?

  2. 什么是无符号字符* IVEC PARM需要对大多数的这些了,我在哪里的?
  3. 得到它

解决方案

有没有给,因为AES块大小是固定大小的<击>基于密钥大小;你找到了<一个href=\"http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_$c$cbook_.28ECB.29\">ECB模式的实施,这是不适合于直接使用(除了作为教学工具)。

ECB,CBC,CFB128等,都是对于普遍使用操作的模式的所有短名称。他们有不同的属性,但如果你从来没有接触ECB模式,你应该是好的。

我建议住在低级别的code渐行渐远;使用 EVP _ * 接口相反,如果你可以,你可以将一些这些决定到一个文本配置文件,让用户可以很容易地在不同的密码,块大小和操作模式之间进行选择,如果有应该永远是一个很好的理由去改变从默认了。

我的同情,OpenSSL文档感觉比它差,这是不是很大。您可能会发现网络安全与OpenSSL的的有用的书。我希望我发现它迟早是我最后一次需要使用OpenSSL。 (不要让愚蠢的标题欺骗你 - 。它的的已题为只是OpenSSL的哦)

修改我忘了提初始向量的。它们被用来确保,如果你加密使用相同的密钥相同的数据,密文不会完全相同。您需要四对数据进行解密,但你并不需要保持IV的秘密。您应该生成一个随机为每个会话(与RSA或厄尔尼诺贾迈勒或DH-加密的会话密钥一起发送)或相同生成它的两个端点,或与该文件在本地存储,类似的东西。

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.

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);

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);

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. 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?

解决方案

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, 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.

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.

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.)

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解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆