AES加密 - 使用OpenSSL生成密钥 [英] AES Encryption -Key Generation with OpenSSL

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

问题描述

作为参考和作为继续的帖子:
如何使用OpenSSL解密Java AES加密数据?



我有以下问题。



我在C中使用OpenSSL库和编程来加密aes-cbc-128中的数据。
我给了任何输入的二进制数据,我必须加密。



我知道Java有一个CipherParameters接口来设置IV和KeyParameters。 p>

有没有办法使用openSSL生成IV和密钥?总而言之,如果在C程序中使用openSSL的随机生成器就可以用于这些目的。你可以提供一些文档/示例/链接吗?



谢谢

解决方案

AES密钥和对称加密的IV只是随机字节串。所以任何密码强的随机数发生器都会做到这一点。 OpenSSL提供了这样一个随机数字生成器(它本身就是在Windows上提供的任何操作系统,例如 CryptGenRandom() / dev / random / dev / urandom )。该功能是 RAND_bytes()。所以代码如下所示:

  #include< openssl / rand.h> 

/ * ... * /
unsigned char key [16],iv [16];

如果(!RAND_bytes(key,sizeof key)){
/ * OpenSSL报告失败,相应地执行* /
}
if(!RAND_bytes(iv ,sizeof iv)){
/ * OpenSSL报告失败,相应地执行* /
}


As a reference and as continuation to the post: how to use OpenSSL to decrypt Java AES-encrypted data?

I have the following questions.

I am using OpenSSL libs and programming in C for encrypting data in aes-cbc-128. I am given any input binary data and I have to encrypt this.

I learn that Java has a CipherParameters interface to set IV and KeyParameters too.

Is there a way to generate IV and a key using openSSL? In short how could one use in a C program to call the random generator of openSSL for these purposes. Can any of you provide some docs/examples/links on this?

Thanks

解决方案

An AES key, and an IV for symmetric encryption, are just bunchs of random bytes. So any cryptographically strong random number generator will do the trick. OpenSSL provides such a random number generator (which itself feeds on whatever the operating system provides, e.g. CryptGenRandom() on Windows or /dev/random and /dev/urandom on Linux). The function is RAND_bytes(). So the code would look like this:

#include <openssl/rand.h>

/* ... */
unsigned char key[16], iv[16];

if (!RAND_bytes(key, sizeof key)) {
    /* OpenSSL reports a failure, act accordingly */
}
if (!RAND_bytes(iv, sizeof iv)) {
    /* OpenSSL reports a failure, act accordingly */
}

这篇关于AES加密 - 使用OpenSSL生成密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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