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

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

问题描述

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

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

我有以下问题.

我正在使用 OpenSSL 库和 C 语言编程来加密 aes-cbc-128 中的数据.我得到了任何输入二进制数据,我必须对其进行加密.

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.

我了解到 Java 也有一个 CipherParameters 接口来设置 IV 和 KeyParameters.

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

有没有办法使用 openSSL 生成 IV 和密钥?简而言之,如何在 C 程序中使用来调用 openSSL 的随机生成器以实现这些目的.你们中的任何人都可以提供一些关于此的文档/示例/链接吗?

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?

谢谢

推荐答案

一个 AES 密钥和一个用于对称加密的 IV,只是一堆随机字节.因此,任何加密强的随机数生成器都可以解决问题.OpenSSL 提供了这样一个随机数生成器(它本身依赖于操作系统提供的任何内容,例如 Windows 上的 CryptGenRandom()/dev/random/dev/urandom 在 Linux 上).函数是RAND_bytes().所以代码看起来像这样:

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天全站免登陆