Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Openssl

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

In the modern Internet, security is a problem that can not be ignored. When it comes to security, it has to be related to encryption and decryption. Now, as long as our data is spread on the Internet, we can't avoid snooping by people with malicious intentions, so we should first consider several factors when spreading data in the network, such as the identity of the other party, the integrity of the data, and the privacy of the data.

Commonly used cryptographic algorithms: symmetric encryption, public key encryption, one-way encryption.

Symmetric encryption uses the same secret key for encryption and decryption to encrypt the original data in blocks. The advantage is that the encryption speed is fast, and it is often used when the encrypted data is relatively large. The disadvantage is that the secret key cannot be safely sent to the other party. If the password is missing, the data security no longer exists. Commonly used encryption algorithm: DES,3DES,AES,Blowfish,Twofish,IDEA,RC6,CAST5.

Use tools: openssl

For example: encrypt qq.txt files.

Openssl enc-a-salt-des3-in / tmp/qq.txt-out / tmp/qqs.txt then enter the password you defined twice.

-a: indicates that it has been encrypted by base64.

-des3: encryption algorithm.

Check the encrypted qqs.txt at this time

Decryption: openssl enc-d-salt-des3-a-in / tmp/qqs.txt-out / tmp/qqj.txt

-d: decrypt.

As can be seen in the example, symmetric encryption and decryption are used in the same way, and it is not secure to transmit passwords or password leaked data during network transmission.

One-way encryption:

One-way encryption means that the data after encryption operation is irreversible and the original data can not be calculated in reverse. Generally, the hash function is calculated, and the main task of the hash function is to calculate the integrity of data. Common algorithms are: MD5,SHA-1,SHA256,SHA384,SHA512. It is characterized by the same input and the same output. If there is any change in the input, the result will change dramatically. The original data cannot be restored through the output, which is the same size no matter how large the input.

Example: use the MD5 algorithm to calculate the result of qq.txt. Openssl dgst-md5 qq.txt

[root@localhost tmp] # openssl dgst-md5 qq.txt

MD5 (qq.txt) = cd7257236da80701dd11ce383644e213

[root@localhost tmp] # vim qq.txt

151515252:13523452345

455667678:45767687877

567787881:13456566767

667788931:15667676833

234457890:15899865663

567787881:13778798888

667788931:15678789904

234457890:15467689909

567791246:18755653256

234457890:15467685229

567791246:18758944576

D

"qq.txt" 12L, 244C written

[root@localhost tmp] # openssl dgst-md5 qq.txt

MD5 (qq.txt) = c8fcbdb073a0381a8d2d9542dfc64800

[root@localhost tmp] #

Thus it can be seen that the result of adding a character d to the end of the text is quite different from that of the previous one. Because of its irreversible nature, it is often used to verify that the file has changed. The result of the output is usually called the fingerprint of the data, or signature.

Public key encryption:

Public key encryption, also known as asymmetric encryption, is different from symmetric encryption in that it has two keys, namely, public key and private key, which appear in pairs if you use public key to encrypt data and decrypt data with private key. Data encrypted with a private key is decrypted with its public key. It is called asymmetric encryption because it uses different keys for encryption and decryption. Often used in combination with other encryption methods.

Generate your own key pair:

Generate a 2048-bit key in the current directory and save it to mykey. Note that the default generation permission belongs to the external account with read permission. To prohibit other users from reading it, you need to modify the permission or use it directly: (umask 077 OpenSSL genrsa 2048 > mykey)

[root@localhost tmp] # openssl genrsa 2048 > mykey

Generating RSA private key, 2048 bit long modulus

. +

.. + +

E is 65537 (0x10001)

[root@localhost tmp] # ll

-rw-r--r-- 1 root root 1679 Sep 26 03:51 mykey

-rw-r--r-- 1 root root 242 Sep 26 02:54 qqj.txt

-rw-r--r-- 1 root root 358 Sep 26 02:49 qqs.txt

-rw-r--r-- 1 root root 244 Sep 26 03:31 qq.txt

Extract the public key:

[root@localhost tmp] # openssl rsa-in / tmp/mykey-pubout

Writing RSA key

-BEGIN PUBLIC KEY-

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1CZOG8YAJ81strW7T9Sx

CdsdD9tc0wNk5Qv7qE2PyNAthCuQdernyWYbf0on6uOGVfiHu0djLc9mfbDlXV2q

Bu5WDS8Kh5RCFbTjeA35h+YGni16foQxa/U1QjijBK9Ju0BNQt/fblRsbjHR3W3g

0eUd5gJBgFf+p3W9FjR5S063ACIprVZF0u1Eu5lrOkKdtcHAXqSGQje70DCxJA3y

IMtcTxSwBl3YRTuu37GMYot5KVBKzxAfcs7xjB34qxremBZlrLB3Wv8mG1bFf3GH

SiQjPtQ3gSig5spovPVccc5w4LglYE94OOa++hjf0btbBAqB9hET928cWZSGOuH+

9wIDAQAB

-END PUBLIC KEY-

[root@localhost tmp] #

The above steps are complete. The public and private keys have been created. And the private key must ensure its security.

The above three encryption methods have their own characteristics, but also have their own defects. If used alone for network data encryption, it is still unreliable. But it's safe to use them together.

First, the sender:

The main contents are as follows: 1. The original data to be sent is extracted by one-way encryption to ensure the integrity of the data.

2. The sender uses asymmetric encryption, that is, encrypts the signature of the original text with its own private key and adds it to the back of the original text. In this way, the identity is guaranteed. After receiving the data with the sender's public key, the receiver can decrypt the data normally with the other party's public key to get the signature, which proves the identity of the data sender.

3. The sender selects another password and encrypts the original text and the encrypted signature together with symmetrical encryption. Because symmetrical encryption is faster, it doesn't matter if the data is too large.

4. The sender encrypts the selected password with the receiver's public key and sends the data to the receiver.

Recipient:

1. After receiving the data, the receiver first uses its own private key to get a password, which is the original data and signature packaged by the sender.

2. Use the obtained password to type the original data and signature that the sender packages and encrypts. At this point, the original data is obtained, and only the original data is not sure that the data has not been modified or replaced. Verify the integrity of the data at this point

3. The receiver decrypts the encrypted signature with the sender's public key, and then obtains the signature of the original data.

4. The receiver calculates the signature of the original data in a general way and compares the obtained signature with the decrypted signature if it shows that the data has not been modified. If inconsistent, this information is unreliable.

Summarize the whole process:

In the process, a combination of three encryption methods is used, and the two sides verify each other's identity through asymmetric encryption, because the sender's data password is encrypted using the receiver's public key, and the data can only be decrypted by the receiver's private key. It is useless for others to get the data. Use symmetric encryption to encrypt the original data so that the original text is encrypted. The reliability of the information is verified by comparing the signature by means of one-way encryption.

The process is shown in a diagram below:

In the above process, both the receiver and the sender have each other's public key beforehand and trust each other, but in the Internet, how can we ensure the credibility of each other's public key? if the other party is an impostor, we will not know. So we need a trusted institution to guarantee, just like a person in our life who says that he is Zhang San living at No. 1 Jia Street. How can I know if what he said is true or false? I went to the police station to check. The police station said that there was indeed a man named Zhang San who lived at No. 1 A Street. Then I think he is Zhang San, otherwise he is not. In the Internet, this "police station" is CA, which is responsible for issuing certificates to hosts on the Internet. If you want to get "trust" on the Internet, you have to register with CA. CA verifies your identity information and then extracts the signature of the information with CA's own private key and sends it to you. This is your digital signature.

After we have the signature, when we need to communicate with a host, the other party will send me its certificate. At this time, I first use the public key of CA to decrypt the certificate. If it can be decrypted, it shows that the certificate was issued by CA. CA has verified his identity and information. We think the other party is reliable, but we need to further prove whether the certificate is within its validity period and whether it is a revoked certificate. For this query, you need to go to CA. Communication really begins when there is no problem with the whole confirmation process. The whole encryption and decryption process and the verification mechanism of CA lead to a new technical specification: PKI (Public.Key.Infrastructure).

This mechanism of CA certificates does not have to be applied to large-scale Internet and can also be used within its own private scope. The tool used is openssl.

Establish a private CA:

1. Generate the secret key:

(umask077; openssl genrsa-out / etc/pki/CA/private/cakey.pem 2048)

[root@localhost private] # (umask 077; openssl genrsa-out / etc/pki/CA/private/cakey.pem 2048)

Generating RSA private key, 2048 bit long modulus

. +

. +

E is 65537 (0x10001)

[root@localhost private] #

2. Request for signature:

[root@localhost private] # openssl req-new-x509-key / etc/pki/CA/private/cakey.pem-out / etc/pki/CA/cacert.pem-days 365

You are about to be asked to enter information that will be incorporated

Into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value

If you enter'., the field will be left blank.

-

Country Name (2 letter code) [XX]: CN

State or Province Name (full name) []: PEK

Locality Name (eg, city) [Default City]: PEK

Organization Name (eg, company) [Default Company Ltd]: FF

Organizational Unit Name (eg, section) []: FF

Common Name (eg, your name or your server's hostname) []: ops

Email Address []: admin@admin.com

[root@localhost private] # ls / etc/pki/CA/cacert.pem

/ etc/pki/CA/cacert.pem

[root@localhost private] # ls / etc/pki/CA/

Cacert.pem certs crl newcerts private

[root@localhost private] #

Use the command: openssl req-new-x509-key / etc/pki/CA/private/cakey.pem-out / etc/pki/CA/cacert.pem-days365, initiate a certificate signing request, and then prompt you to fill in some information.

Req: generate certificate signing request;-news: new request;-key / path/to/keyfile: specify private key file;-x509: generate self-signed certificate;-days: valid days, customizable.

Then create two new files, index.txt and serial, under the CA directory

Then: ~] # echo 00 > seria

3. Sign the certificate:

The method of signing request generation is mentioned in this article. Instead of repeating the instructions, sign the certificate command:

Openssl ca-in / tmp/mykey.csr-out / tmp/ff.com.crt-days 365

-in: indicates the location of the request file

-out: indicates the output file name and location.

-days: specifies the validity period of the signature.

[root@localhost CA] # openssl ca-in / tmp/mykey.csr-out / tmp/ff.com.crt-days 365

Using configuration from / etc/pki/tls/openssl.cnf

Check that the request matches the signature

Signature ok

Certificate Details:

Serial Number: 0 (0x0)

Validity

Not Before: Sep 26 12:22:03 2015 GMT

Not After: Sep 25 12:22:03 2016 GMT

Subject:

CountryName = CN

StateOrProvinceName = PEK

OrganizationName = FF

OrganizationalUnitName = OPS

CommonName = OPS

EmailAddress = admin@ff.com

X509v3 extensions:

X509v3 Basic Constraints:

CA:FALSE

Netscape Comment:

OpenSSL Generated Certificate

X509v3 Subject Key Identifier:

12:2C:F0:DF:C1:99:08:88:73:BA:04:8E:C4:2F:26:20:52:F5:A2:13

X509v3 Authority Key Identifier:

Keyid:84:C4:2E:4A:ED:A6:9D:26:A1:80:BE:D2:47:7F:CC:D9:85:97:4D:19

Certificate is to be certified until Sep 25 12:22:03 2016 GMT (days)

Sign the certificate? [y/n]: y

1 out of 1 certificate requests certified, commit? [y/n] y

Write out database with 1 new entries

Data Base Updated

[root@localhost CA] #

[root@localhost CA] # cat serial

01

[root@localhost CA] # cat index.txt

V 160925122203Z 00 unknown / C=CN/ST=PEK/O=FF/OU=OPS/CN=OPS/emailAddress=admin@ff.com

[root@localhost CA]

You can see it in index.txt in the picture above. A signature record is generated. Other hosts sign in the same way, except that the request needs to be sent to CA in some way.

Certificate revocation: when the host key is lost or leaked, you need to apply to CA for certificate revocation in order to ensure security.

1. Get the serial number and information of the certificate to be revoked.

[root@localhost CA] # openssl x509-in / tmp/ff.com.crt-noout-serial-subject

Serial=00

Subject= / C=CN/ST=PEK/O=FF/OU=OPS/CN=OPS/emailAddress=admin@ff.com

[root@localhost CA] #

2. Verify whether the certificate to be revoked is consistent with the information provided by the host for revocation.

3. Revoke the certificate: openssl ca-revoke / etc/pki/CA/newcerts/00.pem

If this is the first revocation, you need to generate the revocation certificate number: echo 01 > / etc/pki/CA/crlnumber

4. Update the list of revoked certificates: openssl ca-gencrl-out ff.com.crl

View the crl file: openssl crl-in / path/to/crl_FILE.crl-noout-text

[root@localhost crl] # openssl crl-in ff.com.crl-noout-text

Certificate Revocation List (CRL):

Version 2 (0x1)

Signature Algorithm: sha1WithRSAEncryption

Issuer: / C=CN/ST=PEK/L=PEK/O=FF/OU=FF/CN=ops/emailAddress=admin@admin.com

Last Update: Sep 26 12:45:50 2015 GMT

Next Update: Oct 26 12:45:50 2015 GMT

CRL extensions:

X509v3 CRL Number:

one

Revoked Certificates:

Serial Number: 00

Revocation Date: Sep 26 12:40:47 2015 GMT

Signature Algorithm: sha1WithRSAEncryption

B5:f4:9c:ec:3d:4a:e4:d1:1b:48:d4:a0:5e:06:4f:a0:ab:e6:

76:de:62:f6:88:8e:cc:ec:b9:de:39:db:8c:a0:00:3e:57:41:

73:09:90:e9:64:4c:0a:01:70:0b:ac:43:f2:28:0a:1a:77:c9:

B2:20:ef:30:d6:3d:5b:7b:a0:5a:5d:dc:1a:95:63:4b:e8:11:

E9:f6:53:8b:42:83:cb:34:cc:cc:25:94:de:f9:54:77:a4:1f:

6a:12:27:77:e2:fc:48:3b:56:58:08:f2:47:0c:f2:4d:52:ed:

0e:ba:e6:76:47:d5:d5:6f:de:44:5d:73:3d:ff:14:13:b1:d0:

Aa:da:ee:6e:4d:84:d7:34:e9:4f:0f:fe:aa:9f:da:6e:a9:bd:

2b:aa:3e:82:2b:91:f4:37:bd:38:08:99:94:95:0b:98:3b:93:

81:bf:cc:6a:80:31:f5:73:4f:45:e3:5f:53:25:a3:d9:95:03:

C7:27:e8:44:c1:97:9d:cb:8d:26:9d:69:d3:0d:ba:6d:a8:1b:

6f:47:e9:fb:9e:ad:9c:f5:e9:9c:b0:50:be:e2:35:44:2b:c5:

6b:c6:36:3d:52:e5:d1:5a:6c:56:13:57:6f:67:e5:5b:ba:1d:

5c:d4:5b:81:9b:e7:c2:9e:99:c7:7b:ea:48:ff:3c:70:5d:96:

50:20:18:1a

[root@localhost crl] #

Crt certificate files can be configured in the http server, so that you can use https to access the site to achieve the full exchange of data.

At this point, the private CA construction and certificate signing have been completed. This article is only a summary of personal knowledge for future reference.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report