Home Backend Development PHP Tutorial 解析php php_openssl.dll的作用_php技巧

解析php php_openssl.dll的作用_php技巧

May 17, 2016 am 08:57 AM
php

一.openssl简介
数据加密是信息信息传输中的一个重要组成部分.任何信息都以明文方式传输,确实是个很不安全的做法.所以,
需要对数据进行加密.将明文数据转换为密文数据,再进行传输.

OpenSSL是一套用于SSL/TLS协议的加密工具,其作用有:
1.生成私有密钥.
2.生成证书,即数字签名证书,它包含一个公有密钥,可以用来单向的加密和解密数据.即,使用公钥加密的数据,只能使用
私有密钥解密.使用私钥加密的数据,可以使用公钥来解密.
3.计算信息摘要.
4.SSL/TLS客户端和服务器端测试.
5.处理S/MIME标记和邮件的加密.

二.加密技术简介.
加密,即将明文数据转换为密文数据的过程.以起到对明文保密的作用.
加密算法: 数据加密过程所采用的一种运算算法.用于将明文转换为密文.
密钥: 加密算法通过与密钥进行某种运算,将明文数据进行加密,生成加密数据.解密时,加密算法通过密钥,
将加密数据再转换为明文数据.
现今的加密技术,加密算法是公开的,即所有人都知道加密所采用的运算方式.但只有使用密钥才能进行
对密文的解密.所以,密钥的保护,是数据安全的核心.

三.openssl工具使用简介
1.私有密钥生成方法

生成私有密钥,可以使用不同的数字签名算法.下面分别介绍;

#采用DSA算法
$ openssl dsaparam -noout -out dsakey0.pem -genkey 1024
#采用RSA算法
$ openssl genrsa -out rsakey0.pem 1024
#采用RSA算法,并使用密码保护.在生成私钥时,需要输入一个密码,用于保护私钥.
#在使用这个私钥进行加/解密操作时,也需要输入这个密码.
$ openssl genrsa -des3 -out rsakey1.pem 10242.公用密钥的生成方法
根据私钥来生成公钥
#生成dsa算法的公钥
$ openssl dsa -in dsakey0.pem -pubout -out dsakey0-pub.pem
#生成rsa算法的公钥
$ openssl rsa -in rsakey0.pem -pubout -out rsakey0-pub.pem3.自签名证书的生成方法
#产生DSA算法的证书
$ openssl req -x509 -key dsakey0.pem -days 365 -out mycert-dsa.pem -new
#产生RSA算法的证书
$ openssl req -x509 -key rsakey0.pem -days 365 -out mycert-rsa.pem -new

4.使用证书进行邮件加密
我们的明文信件内容:

$ cat test.txt
111111
222222
333333
444444
aaaaaa
使用证书对明文信件进行加密,输出到etest.txt文件:
$ openssl smime   -encrypt -in test.txt -out etest.txt mycert-rsa.pem查看加密后的密文内容:

$ cat etest.txt
MIME-Version: 1.0
Content-Disposition: attachment; filename="smime.p7m"
Content-Type: application/x-pkcs7-mime; smime-type=enveloped-data; name="smime.p7m"
Content-Transfer-Encoding: base64

MIIBYAYJKoZIhvcNAQcDoIIBUTCCAU0CAQAxgewwgekCAQAwUjBFMQswCQYDVQQG
EwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lk
Z2l0cyBQdHkgTHRkAgkAr71mh4NRX/UwDQYJKoZIhvcNAQEBBQAEgYCbrA1WBwQK
Zj7TcNtrxiDzqMBc/Lu063SLKvBK6mQMqT+npFOOFtzIKdFVkldH0YyQhMZDSCyq
YUtGwOaDw6Jn0AHRt64UwPOKoaeL9RVqp9vgtiFC/uXis5UeyZCWS1z7Jsih8Aa+
Da/DQN0sOCX85OdK/TDewNx8mTaYBbVf4jBZBgkqhkiG9w0BBwEwGgYIKoZIhvcN
AwIwDgICAKAECNxsgiJ2s+1ugDC6JknPL+rDYBCddcyPH+bMYjqrUP0hE/GQ5WSj
sv8CDkOUdvY5XG440yiAL3Z3ysI=使用私钥进行解密,输出到dtest.txt文件:

$ openssl smime  -decrypt -in etest.txt -inkey rsakey0.pem  -out dtest.txt查看解密后的信件内容,与原明文信件内容完全一致.

$ cat dtest.txt
111111
222222
333333
444444
aaaaaa

5.简单的文件加密
明文文件内容:

$ cat test.txt
1
22
333
4444
55555
Hello加密明文文件,输出为test.enc文件,输入”123123”作为加密密码:

$ openssl enc -aes-256-cbc -salt -in test.txt -out test.enc
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:对密文进行解密,输入”123123”作为解密密码:

$ openssl enc -d -aes-256-cbc -in test.enc
enter aes-256-cbc decryption password:
1
22
333
4444
55555
Hello

6.简单的字符串加密
采用base64方式进行加密:

$ echo "encode me" | openssl enc -base64
ZW5jb2RlIG1lCg==解密时,需要知道加密算法,才可解密:

$ echo "ZW5jb2RlIG1lCg==" | openssl enc -base64 -d
encode me

7.SSL客户端和服务器的测试
使用私钥和证书启动SSL服务器,"-www”的作用是,当客户端连接服务器时,发送一个状态信息网页到客户端.

openssl s_server -key mykey.pem -cert mycert.pem -www连接SSL服务器到,客户端会获得服务器的证书:

openssl s_client -connect localhost:4433

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles