1. Overview of Asymmetric encryption algorithms

Asymmetric encryption algorithm is also called Public key algorithm, which solves the problem of symmetric encryption algorithm key distribution, the basic features of asymmetric encryption algorithm are as follows:

1. Encryption key and decryption key are different

2. A key in the key pair can be exposed

3, according to public key difficult to calculate the private key

According to the characteristics of asymmetric encryption algorithm, user digital signature, key exchange and data encryption can be used. However, because the asymmetric encryption algorithm is much slower than the symmetric encryption algorithm, the most common use is digital signature and key exchange.

At present, there are three kinds of asymmetric encryption algorithms, such as RSA, DH and DSA, but not all of them can be used for key exchange and digital signature. Instead, RSA can be used for digital signatures and key exchange, and the DH algorithm can be used for key exchange, while the DSA algorithm specializes in user digital signatures.

OpenSSL supports the above three algorithms, and provides a rich set of instructions for three algorithms, this chapter mainly introduces the RSA algorithm and related instructions

2. RSA algorithm related instructions and usage

RSA, although it can be digitally signed, key exchange and data encryption, but RSA encryption data slow, usually do not use RSA encryption data. So the most common function is digital signature and key exchange, and the concept of digital signature and key exchange is put aside, which is essentially the difference between using public key encryption or using private key encryption. So we just have to remember the word: "Public key encryption, private key signature."

Public Key cryptography: Purpose is key exchange, user A uses the public key of User B to send a small amount of data encryption to B,b to decrypt the data with its own private key

Private key Signature: The purpose is digital signature, user A uses their own private key to encrypt the digest information of the data and send it to b,b to decrypt the digest information with a's public key and verify

There are three main RSA algorithm directives in Opessl, although other directives are involved, but they are not detailed here.

Instructions |
Function |

Genrsa |
Generate and enter an RSA private key |

Rsa |
Handling issues such as format conversion for RSA keys |

Rsautl |
Use RSA keys for encryption, decryption, signing, and validation operations |

2.1 Genrsa Instruction Description

The GENRSA is used to generate a key pair, which uses the following

[email protected]:~$ OpenSSL Genrsa-usage:genrsa [args] [numbits] Number of key bits, recommended 1024 and above-des encrypt the generated key with DES in CBC mode//generated keys are encrypted using DES mode- Des3 encrypt the generated key with DES in Ede CBC mode (168 bit key)//generated key is encrypted using DES3 mode-seed Encrypt PEM output with CBC seed//generated key or seed mode for-aes128,-aes192,-aes256 Encrypt PEM output with CBC AES//generated keys are encrypted using AES mode-camellia128,-CAMELLIA192,–CA mellia256 encrypt PEM output with CBC Camellia//generated keys are encrypted using Camellia mode-out File output the key to ' files//generated keys from which you can extract the public key-passout arg output file Pass phrase source//Specify the encryption password of the key file, from the file, environment variable, terminal, etc. input-F4 use F4 (0x10001) for The E value Select the value of index E, which is specified by default, and E is 65537-3 use 3 for the E value Select the value of index E, the default value is 65537, the index is specified as 3-engine e use engine E, possibly a hardware device. Specify a three-party cryptographic library or hardware-rand file:file: ... load the file (or the files in the directory) into//generate with The number of machines in the seed file of the random numbers generator

You can see that the GENRSA instruction is relatively simple to use, the commonly used also has the specified encryption algorithm, output key file, encrypted password. Let's just cite one example to illustrate

/* * Specify Key file Rsa.pem * Specify cryptographic algorithm aes128 * Specify encryption Key 123456 * Specify key length 1024x768 **/[email protected]:~$ OpenSSL genrsa-out rsa.pem-ae S128-passout pass:123456 1024Generating RSA private key, 1024x768 bit long modulus ..... ..... ..... ..... ..... ..... ..... ..... ++++++.................................++++++e. .....?????????????????. is 65537 (0x10001)///default mode 65537/* encrypted key file has encryption algorithm and other information */[email protected]:~$ cat RSA.PEM-----BEGIN RSA PRIVATE Key----- proc-type:4,encrypteddek-info:aes-128-cbc,4c23682b0d34d339ed7e44819a70b4f9c9uhqqwbkcw3hjdq/6fgujcofchd4+ kfvzojnnisnjbahv3celfakskb2rka5goc4eq6sykccsh8obopopbjd1zdasdl1pio0vijfaoq4nmarj61+6onj/ Hax2nftdjnyrmsgowejb6a3mt4kixrvicnkkmsuy1qp6ln2qoevynmxewawivznjfm0okscl1krgsul32vecn5b1s8fztyjts3pqxjmyaw65zlx +8muobanl9whsltz2eo/6xtzrbdiogmolfp/3obqias3007qv48ctwwrlaa+ Rpbmviiesn7bforoanbh0s5nvuunxyshx90izj2m1l4i5sp8jkbunxpk6chqtuqxpmh06nhomnyzptrqegfgzlwvopofos5khgajjpnexi7ah8ocnyo21jv6s Lmfxk1lues3xcvm8cd/zvbszd7jg+axbjr+lporhpmefkstxhtfo3ok3boyqhizyeyh4s59xwo+dfrb2zuvkksqkkv+tfmszpr7b7uieguck3nrbcwdapftymf/edubljbv816to+ hyqlhxkfuzp5imjmjnubhrxra6s477xn6nil9dgwzuempnh6brc8mj7jwftxdpwdn2py+vcj04o98fo08c+4ess3u0y9ftyxyy1c9niwxf+ t2dulq94n4aq2uytxovnhrmdyrj9bucugg6zx6xtu24asgfvtnikgau8jcx0gkcwu60ttlsxpnawhnxjsj5n7bxav6qq1goiikqljacrv2pmxnqvgkpovq742 +AWSICHRWQE5VIFW9ADSMYIT7W06IOGYURS+0+FMFS6QPTT3ZBFZAKZKD-----END RSA PRIVATE KEY-----[email protected]:~$

2.2 RSA Instruction Description

The RSA instruction user manages the generated key using the following usage

[email protected]:~$ OpenSSL Rsa-unknown option-rsa [options] <infile >outfile where options Are-inform Arg input format-one of Der NET PEM//input file format, default PEM format-outform arg output Format-one of Der NET PEM//input file format, default PEM format-in arg input file//input text Piece-sgckey use IIS SGC key format//Specify SGC encoded format, compatible with older versions and should not be used-passin arg input fil E Pass Phrase source//Specifies the encrypted password of the input file, which can be from file, terminal, environment variable, etc.-out arg output file Output files-passout arg output file pass phrase source//Specify an encrypted password for the output files, from the file, Terminal, environment variables, etc.-des encrypt PEM output with CBC des//File output with DES encryption-des3 encrypt PEM Output with Ede CBC des using 168 bit key//file with Des3 encrypted output-seed encrypt PEM output with CBC seed Using seed to encrypt output files -aes128,-aes192,-aes256 encrypt PEM output with CBC AES//output file with AES encryption-camellia -camellia192,-camellia256 encrypt PEM output with CBC Camellia//file with Camellia encryption output -text print the key in text//output each parameter value in clear text-noout the "T print key ou T//Do not output key to any file-modulus print the RSA key modulus/ /output modulus refers to-check verify key consistency//check the correctness and consistency of the input key-pubin expect a PU Blic key in input file//Specifies that the input files are public key-pubout output a publicly key Specifies that the output file is the public key-engine e use engine E, possibly a hardware device. Specify a three-party cryptographic library or hardware [email protected]:~$

RSA Instruction Operation example below

1. RSA Add and remove key protection password

/* Generate an unencrypted RSA key */[email protected]:~/test$ OpenSSL genrsa-out rsa.pemgenerating RSA private key, a bit long modulus ... ... ++++++++++++.....++++++++++++e is 65537 (0x10001)/* Add password protection for RSA keys */[email protected]:~/test$ OpenSSL rsa-in Rsa.pem-des3-passout pass:123456-out e_rsa.pemwriting RSA key/* for RSA key removal password protection */[email protected]:~/test$ OpenSSL rsa-in E_rsa.pem-passin pass:123456-out p_rsa.pemwriting RSA key/* Compare the original RSA key and the RSA key after removing the password, is the same as */[email protected]:~/test$ Diff Rsa.pem P_rsa.pem

2, modify the key protection password and algorithm

/* Generate RSA key */[email protected]:~/test$ OpenSSL genrsa-des3-passout pass:123456-out rsa.pemgenerating RSA private key, 512 Bit long modulus..................++++++++++++......................++++++++++++e is 65537 (0x10001)/* Modify the encryption algorithm to aes128 , the password is 123456*/[email protected]:~/test$ OpenSSL rsa-in rsa.pem-passin pass:123456-aes128-passout pass:123456-out E_RSA. Pemwriting RSA Key

3. View each parameter in the key pair

[Email protected]:~/test$ OpenSSL rsa-in rsa.pem-des-passin pass:123456-text-noout

4. Extract the public key from the key and print the modulus value

/* Extract the public key, specify the output as the public key with the Pubout parameter */[email protected]:~/test$ OpenSSL rsa-in rsa.pem-passin pass:123456-pubout-out Pub.pemwriting RSA key/* Print public key modulus value */[email protected]:~/test$ OpenSSL rsa-in pub.pem-pubin-modulus-nooutmodulus= c35e0b54041d78466eae7de67c1da4d26575bc1608ce6a199012e11d10ed36e2f7c651d4d8b40d93691d901e2cf4e21687e912b77dcce069373a7f658 5e946ef

5. Format of the conversion key

/* Convert PEM format to der Format, use Outform to specify der format */[email protected]:~/test$ OpenSSL rsa-in rsa.pem-passin pass:123456-des-passout Pass:123456-outform der-out rsa.derwriting RSA key/* convert der format to PEM format, use inform to specify DER format */[email protected]:~/test$ OpenSSL rsa-in rsa.der-inform der-passin pass:123456-out Rsa.pem

2.3 Rsautl Instruction Description

The above two instructions are the generation and management of the key, Rsautl is really used for key exchange and digital signature. It is essentially encrypted with the RSA public key or private key.

Whether using public key encryption or private key encryption, RSA can encrypt the length of the data each time cannot exceed the RSA key length, and depending on the specific way to enter the maximum length of the encrypted data is different, and the output length is always equal to the RSA key length. RSA the input input length corresponding to the different methods of completion is the following table

Data completion methods |
Input data length |
Output data length |
Parameter string |

Pkcs#1 v1.5 |
Less than (key length-11) bytes |
Same Key length |
-pkcs |

Pkcs#1 OAEP |
Less than (key length-11) bytes |
Same Key length |
-oaep |

Pkcs#1 for SSLv23 |
Less than (key length-11) bytes |
Same Key length |
-ssl |

Do not use the padded |
Same Key length |
Same Key length |
-raw |

RSAUTL instruction usage is as follows

[email protected]:~$ OpenSSL rsautl-usage:rsautl [options]-in file input file Input files-out file output file//output Files-inkey fil e input key//input keys-keyform arg private key Format-default PEM Specifies the key format-pubin input is a RSA public//Specifies the RSA key-certin INP UT is a certificate carrying a RSA public key//specifies that the input is a certificate file-SSL Use SSL v2 padding Fill with SSLv23-raw use no padding//Do not fill-PKCS using PK Cs#1 v1.5 padding (default)//using V1.5 fill mode-OAEP use pkcs#1 OAEP Use OAEP fill mode-sign sign with private key//Use private key for signature-verify Verify W ITH public Key Using a public key authentication signature-encrypt encrypt with public key//using an encryption-decrypt Decrypt with private key//decrypt-hexdump hex dump output with private key Output-engine e using engine E, possibly a hardware device with a 16-input dump. Specify a three-party library or hardware Device-passin Arg pass phrase source//Specify the password entered

The Rsautl operation example is as follows:

1. Use Rsautl for encryption and decryption operations

/* Generate RSA key */[email protected]:~/test$ OpenSSL genrsa-des3-passout pass:123456-out Rsa.pem generating RSA private K EY, modulus............++++++++++++...++++++++++++e bit Long is 65537 (0x10001)/* Extract public key */[email protected]:~/ test$ OpenSSL rsa-in rsa.pem-passin pass:123456-pubout-out PUB.PEM writing RSA key/* uses RSA as the key for encryption, and actually encrypts it with the public key */[ema il protected]:~/test$ OpenSSL rsautl-encrypt-in plain.txt-inkey rsa.pem-passin pass:123456-out enc.txt/* use RSA as The key is decrypted and is actually decrypted with the private key */[email protected]:~/test$ OpenSSL rsautl-decrypt-in Enc.txt-inkey rsa.pem-passin Pass : 123456-out replain.txt/* Compare original file with decrypted file */[email protected]:~/test$ diff plain.txt Replain.txt/* Use public key for encryption */[email protected]:~/test$ OpenSSL rsautl-encrypt-in plain.txt-inkey pub.pem-pubin-out enc1.txt/* uses RSA as the key to decrypt, The private key is actually used to decrypt */[email protected]:~/test$ OpenSSL rsautl-decrypt-in enc1.txt-inkey rsa.pem-passin pass:123456- Out replain1.txt/* compare raw files and decrypted files */[email protected]:~/test$ diff Plain.txt Replain1.txt

In this experiment there is a doubt, why the same plaintext, the use of key encryption and public key encryption after the ciphertext results are different? In the online query, because the RSA public key encryption when the random number is populated according to the fill pattern, resulting in different encryption results each time.

2. Use Rsautl for signing and verifying operations

/* Extract the private key in PCKS8 format */[email protected]:~/test$ OpenSSL pkcs8-topk8-in rsa.pem-passin pass:123456-out pri.pem-nocrypt/* using R The SA key is signed and is actually encrypted with the private key */[email protected]:~/test$ OpenSSL rsautl-sign-in plain.txt-inkey rsa.pem-passin Pass:123456-ou T sign.txt/* uses the RSA key for authentication, actually decrypts with the public key */[email protected]:~/test$ OpenSSL rsautl-verify-in sign.txt-inkey Rsa.pem- Passin pass:123456-out replain.txt/* Compare original file and signature decrypted file */[email protected]:~/test$ diff plain.txt Replain.txt/* Sign with private key * /[email protected]:~/test$ OpenSSL rsautl-sign-in plain.txt-inkey pri.pem-out sign1.txt/* use public key for authentication */[email protected]:~/test$ OpenSSL rsautl-verify-in sign1.txt-inkey pub.pem-pubin-out replain1.txt/* vs. original file and signature decrypted file */[email protected]:~/test$ Cat Plain Replain1.txt

It is important to note that the signature and verification process here is essentially a decryption operation, not a standard sense of signature and validation. Signature and verification in the standard sense is required to increase the summary operation, the following articles are elaborated in detail.

3. Summary

We can see that the parameters of the above instructions are related to the contents of the certificate, and we wait until later when we introduce the relevant contents of the CA.

OpenSSL Asymmetric Encryption algorithm RSA command details