OpenSSL provides SSL, TLS and general purpose cryptography.It wraps the OpenSSL library. Examples ¶ ↑. All examples assume you have loaded OpenSSL with. Require 'openssl'. These examples build atop each other.
Hi, This is rather a request for clarification. In my application I'm using a key derived from a passphrase when exporting the RSA key of some user. Something like: require 'openssl' rsakey = OpenSSL::PKey::RSA.new 3072 digest = OpenSSL.
While Encrypting a File with a Password from the Command Line using OpenSSL is very useful in its own right, the real power of the OpenSSL library is its ability to support the use of public key cryptograph for encrypting or validating data in an unattended manner (where the password is not required to encrypt) is done with public keys.
Provides symmetric algorithms for encryption and decryption. The algorithmsthat are available depend on the particular version of OpenSSL that is installed.
Listing all supported algorithms
A list of supported algorithms can be obtained by
Instantiating a Cipher
There are several ways to create a Cipherinstance. Generally, a Cipher algorithm iscategorized by its name, the key length in bits and the cipher mode to beused. The most generic way to create a Cipher is the following
That is, a string consisting of the hyphenated concatenation of theindividual components name, key length and mode. Either all uppercase orall lowercase strings may be used, for example:
For each algorithm supported, there is a class defined under the Cipher class that goes by the name of thecipher, e.g. to obtain an instance of AES, you could also use
Finally, due to its wide-spread use, there are also extra classes definedfor the different key sizes of AES
Choosing either encryption or decryption mode
Encryption and decryption are often very similar operations for symmetricalgorithms, this is reflected by not having to choose different classes foreither operation, both can be done using the same class. Still, afterobtaining a Cipher instance, we need totell the instance what it is that we intend to do with it, so we need tocall either
or
on the Cipher instance. This should be thefirst call after creating the instance, otherwise configuration that hasalready been set could get lost in the process.
Openssl Public Key
Choosing a key
Symmetric encryption requires a key that is the same for the encrypting andfor the decrypting party and after initial key establishment should be keptas private information. There are a lot of ways to create insecure keys,the most notable is to simply take a password as the key without processingthe password further. A simple and secure way to create a key for aparticular Cipher is
If you absolutely need to use passwords as encryption keys, you should usePassword-Based Key Derivation Function 2 (PBKDF2) by generating the keywith the help of the functionality provided by OpenSSL::PKCS5#pbkdf2_hmac_sha1or OpenSSL::PKCS5#pbkdf2_hmac.
Although there is #pkcs5_keyivgen, its use isdeprecated and it should only be used in legacy applications because itdoes not use the newer PKCS#5 v2 algorithms.
Choosing an IV
The cipher modes CBC, CFB, OFB and CTR all need an 'initialization vector',or short, IV. ECB mode is the only mode that does not require an IV, butthere is almost no legitimate use case for this mode because of the factthat it does not sufficiently hide plaintext patterns. Therefore
You should never use ECB mode unless you are absolutely sure thatyou absolutely need it
Because of this, you will end up with a mode that explicitly requires an IVin any case. Although the IV can be seen as public information, i.e. it maybe transmitted in public once generated, it should still stay unpredictableto prevent certain kinds of attacks. Therefore, ideally
Always create a secure random IV for every encryption of your Cipher
A new, random IV should be created for every encryption of data. Think ofthe IV as a nonce (number used once) - it's public but random andunpredictable. A secure random IV can be created as follows
Although the key is generally a random value, too, it is a bad choice as anIV. There are elaborate ways how an attacker can take advantage of such anIV. As a general rule of thumb, exposing the key directly or indirectlyshould be avoided at all cost and exceptions only be made with good reason.
Calling #final
ECB (which should not be used) and CBC are both block-based modes. Thismeans that unlike for the other streaming-based modes, they operate onfixed-size blocks of data, and therefore they require a 'finalization' stepto produce or correctly decrypt the last block of data by appropriatelyhandling some form of padding. Therefore it is essential to add the outputof #final to yourencryption/decryption buffer or you will end up with decryption errors ortruncated data.
Although this is not really necessary for streaming-mode ciphers, it isstill recommended to apply the same pattern of adding the output of #final there as well - it alsoenables you to switch between modes more easily in the future.
Encrypting and decrypting some data
Authenticated Encryption and Associated Data (AEAD)
If the OpenSSL version used supports it, anAuthenticated Encryption mode (such as GCM or CCM) should always bepreferred over any unauthenticated mode. Currently, OpenSSL supports AE only in combination withAssociated Data (AEAD) where additional associated data is included in theencryption process to compute a tag at the end of the encryption. This tagwill also be used in the decryption process and by verifying its validity,the authenticity of a given ciphertext is established.
This is superior to unauthenticated modes in that it allows to detect ifsomebody effectively changed the ciphertext after it had been encrypted.This prevents malicious modifications of the ciphertext that couldotherwise be exploited to modify ciphertexts in ways beneficial topotential attackers.
An associated data is used where there is additional information, such asheaders or some metadata, that must be also authenticated but notnecessarily need to be encrypted. If no associated data is needed forencryption and later decryption, the OpenSSLlibrary still requires a value to be set - ' may be used in case none isavailable.
An example using the GCM (Galois/Counter Mode). You have 16 byteskey, 12 bytes (96 bits) nonce and the associated dataauth_data. Be sure not to reuse the key andnonce pair. Reusing an nonce ruins the security guarantees of GCMmode.
Now you are the receiver. You know the key and have receivednonce, auth_data, encrypted and tagthrough an untrusted network. Note that GCM accepts an arbitrary length tagbetween 1 and 16 bytes. You may additionally need to check that thereceived tag has the correct length, or you allow attackers to forge avalid single byte tag for the tampered ciphertext with a probability of1/256.