C Openssl Generate Aes 256 Key

This post briefly describes how to utilise AES to encrypt and decrypt files with OpenSSL.

I am working on Ubuntu 14.10 and I am trying to get the same output using C and OpenSSL that I would get using the command:openssl enc -aes-256-cfb8 -in test -out test.enc -K $key -iv $ivI have. Each utility is easily broken down via the first argument of openssl. For instance, to generate an RSA key, the command to use will be openssl genpkey. Generate 2048-bit AES-256 Encrypted RSA Private Key.pem. The following command will result in an output file of private.pem in which will be a private RSA key in the PEM format.

AES - Advanced Encryption Standard (also known as Rijndael).

OpenSSL - Cryptography and SSL/TLS Toolkit

We’ll walk through the following steps:

  • Generate an AES key plus Initialization vector (iv) with openssl and
  • how to encode/decode a file with the generated key/iv pair

Note: AES is a symmetric-key algorithm which means it uses the same key during encryption/decryption.

Generating key/iv pair

We want to generate a 256-bit key and use Cipher Block Chaining (CBC).

C openssl generate aes 256 keys

The basic command to use is openssl enc plus some options:

C Openssl Generate Aes 256 Key
  • -P — Print out the salt, key and IV used, then exit
  • -k <secret> or -pass pass:<secret> — to specify the password to use
  • -aes-256-cbc — the cipher name

Note: We decided to use no salt to keep the example simple.

Issue openssl enc --help for more details and options (e.g. other ciphernames, how to specify a salt, …).

Aes 256 Java

Generate

Encoding

KeyOpenssl

Let's start with encoding Hello, AES! contained in the text file message.txt:

C Openssl Generate Aes 256 Key

Decoding

Decoding is almost the same command line - just an additional -d for decrypting:

Note: Beware of the line breaks

While working with AES encryption I encountered the situation where the encoder sometimes produces base 64 encoded data with or without line breaks...

C Openssl Generate Aes 256 Keys

Short answer: Yes, use the OpenSSL -A option.