Openssl Generate Aes 256 Key Base64

Posted on by

Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. 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.

gistfile1.txt
  • Bindings to OpenSSL. This crate provides a safe interface to the popular OpenSSL cryptography library. OpenSSL versions 1.0.1 through 1.1.1 and LibreSSL versions 2.5 through 2.8 are supported. Both OpenSSL libraries and headers are required to build this crate. There are multiple options available to locate OpenSSL.
  • Jan 09, 2016  encode decode file using openssl and aes 256 cbc algorithm in linux. Here in this encryption decryption tutorial we will learn how to encrypt or encode a file using openssl and aes-256.
echo -n 'That's the text' openssl enc -e -aes-256-cbc -a
Encrypt with interactive password. Encrypted message is base64-encoded afterwards.
echo -n 'That's the text' openssl enc -e -aes-256-cbc -a -k 'MySuperPassword'
Encrypt with specified password. Encrypted message is base64-encoded afterwards.
echo 'GVkYiq1b4M/8ZansBC3Jwx/UtGZzlxJPpygyC' openssl base64 -d openssl enc -d -aes-256-cbc
Base-64 decode and decrypt message with interactive password.
echo 'GVkYiq1b4M/8ZansBC3Jwx/UtGZzlxJPpygyC' openssl base64 -d openssl enc -d -aes-256-cbc -k 'MySuperPassword'
Base-64 decode and decrypt message with specified password.

commented Mar 13, 2020
edited

Your decoding examples don't include -a so wouldn't Base64 decode the input string, right? Also worth noting that you should now include the password key function and iteration count as well, e.g. openssl enc -e -aes-256-cbc -pbkdf2 -iter 1234 -a -k <password>

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
glibberish-aes-256-cbc-decrypt.js
// Doing AES-256-CBC (salted) decryption with node.js.
// This code is based on http://php.net/manual/de/function.openssl-decrypt.php and works with PHP sqAES.
//
// Create your encrypted data with
// echo -n 'Hello world' openssl aes-256-cbc -a -e
varcrypto=require('crypto');
varpassword='password';
varedata='U2FsdGVkX18M7K+pELP06c4d5gz7kLM1CcqJBbubW/Q=';
vardata=newBuffer(edata,'base64');
console.log('Data (Base64): '+data);
varsalt=data.toString('binary',8,16);
console.log('Salt (Base64): '+newBuffer(salt,'binary').toString('base64'));
varct=data.toString('binary',16);
console.log('Content (Base64): '+newBuffer(ct,'binary').toString('base64'));
varrounds=3;
vardata00=password+salt;
console.log('Data00 (Base64): '+newBuffer(data00,'binary').toString('base64'));
md5_hash=newArray();
md5_hash[0]=crypto.createHash('md5').update(data00).digest('binary');
varresult=md5_hash[0];
console.log('MD5-Hash[0] (Base64): '+newBuffer(result,'binary').toString('base64'));
for(i=1;i<rounds;i++){
md5_hash[i]=crypto.createHash('md5').update(md5_hash[i-1]+data00).digest('binary');
result+=md5_hash[i];
console.log('Result (Base64): '+newBuffer(result,'binary').toString('base64'));
}
key=result.substring(0,32);
console.log('Key (Base64): '+newBuffer(key,'binary').toString('base64'));
variv=result.substring(32,(32+16));
console.log('IV (Base64): '+newBuffer(iv,'binary').toString('base64'));
vardecipher=crypto.createDecipheriv('aes-256-cbc',key,iv);
varcontent=decipher.update(ct,'binary','utf8');
content+=decipher.final('utf8');
console.log('Decrypted: '+content);

Openssl Enc Base64

glibberish-aes-256-cbc-decrypt.php
<?php
// Doing AES-256-CBC (Salted) decryption with PHP
// This code is based on http://php.net/manual/de/function.openssl-decrypt.php and adds only some comments
//
// Create your encrypted data with
// echo -n 'Hello world' openssl aes-256-cbc -a -e
$password = 'password';
$edata = 'U2FsdGVkX18M7K+pELP06c4d5gz7kLM1CcqJBbubW/Q=';
$data = base64_decode($edata);
print 'Data: ' . $data . 'n';
$salt = substr($data, 8, 8);
print 'Salt (Base64): ' . base64_encode($salt) . 'n';
$ct = substr($data, 16);
print 'Content (Base64): ' . base64_encode($ct) . 'n';
$rounds = 3;
$data00 = $password.$salt;
print 'Data00 (Base64): ' . base64_encode($data00) . 'n';
$md5_hash = array();
$md5_hash[0] = md5($data00, true);
$result = $md5_hash[0];
print 'MD5-Hash[0] (Base64): ' . base64_encode($result) . 'n';
for ($i = 1; $i < $rounds; $i++) {
$md5_hash[$i] = md5($md5_hash[$i - 1].$data00, true);
$result .= $md5_hash[$i];
print 'Result (Base64): ' . base64_encode($result) . 'n';
}
$key = substr($result, 0, 32);
print 'Key (Base64): ' . base64_encode($key) . 'n';
$iv = substr($result, 32, 16);
print 'IV (Base64): ' . base64_encode($iv) . 'n';
print 'Decrypted: ' . openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);

commented Sep 4, 2016

Openssl Generate Aes 256 Key Base64 Free

could you also provide the node.js encrypt function that the openssl can decrypt as well? Why we need to generate ssh key in ubuntu. Thanks!

i.e.
encrypt-from-nodejs.js openssl aes-256-cbc -base64 -a -d -k password

Aes 256 Encryption Software

commented Nov 17, 2016

You're a lifesaver, thanks dude.

commented Jun 20, 2018

The code need to update:
hash.update(data[, inputEncoding])

VersionChanges
v6.0.0The default inputEncoding changed from binary to utf8.
v0.1.92Added in: v0.1.92

After nodejs v6.0.0, the default encoding of Hash#update changed from 'binary' to 'utf8'.
When calculate the hash, encoding must be explicit as 'binary'.

md5_hash[0] = crypto.createHash('md5').update(data00).digest('binary');
>
md5_hash[0] = crypto.createHash('md5').update(data00, 'binary').digest('binary');

commented Jun 20, 2018

I also suggest the debug as hex not base64, so it's easier to debug with the openssl command line utils.

$ cat input.txt openssl aes-256-cbc -a -salt -k hello -p -out input.txt.enc
salt=C97734D83EDAFD8D
key=67B99E14801776F828D1614328653BDD02A706EC74B772F362BB5517D2BE1B37
iv =07EAEDD9CC7E4577957FE314C589E361

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment