(16)uniGUI for C++ builder下如何实现https加密浏览

(同一个世界,同一个梦想,交流学习C++Builder XE10,传承c++builder的魅力!欢迎各地朋友加入我的QQ群484979943,进群密码“BCB”,同时也请将该群号广为宣传,希望能够广集各方高手,共同进步。如需下载开发工具及源代码请加入我的QQ群)

【阅读倡议】

1、有问题请留言;

2、没问题请点赞;

3、看连载请加群;

4、下源码请加群;

【开发工具】

1、C++Builder10.2.2tokyo

2、FMSoft_uniGUI_Complete_Professional_1.10.0build1462(正版)

本文将介绍在UniGUI程序下如何实现https加密访问,该部分内容在FMSOFT的帮助文档里有详细解释,帮助文文档在unigui的安装目录下C:\Program Files (x86)\FMSoft\Framework\uniGUI\Docs,网站安全证书可以外购权威认证中心的证书,也可以自己创建证书用于研究.本文将使用OPENSSL自己创建证书。.

一、创建证书

访问OPENSSL官网https://slproweb.com/products/Win32OpenSSL.html,下载一个安装程序,本人下载的是第四个64位的完整安装包。下载后默认安装。

(16)uniGUI for C++ builder下如何实现https加密浏览

然后到dos目录C:\OpenSSL-Win64\bin下按照如下提示创建证书。

 1、创建一个根证书

    openssl genrsa -out root.key 1024

也可以创建一个带密码的根证书。

     openssl genrsa -des3 -out root.key 1024

 2、然后对根证书签名:

openssl req -x509 -days 365 -new -nodes -key root.key -out root.pem

 如果你的根证书带有密码则用如下命令

    openssl req -x509 -days 365 -new -key root.key -out root.pem

 Now you will be prompted to provide several details needed to sign your certificate. You will also be prompted for a password if your root.key file was created with a password in the first step.

 Enter pass phrase for root.key:


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) [AU]:TR
State or Province Name (full name) [Some-State]:Ankara
Locality Name (eg, city) []:Cankaya
Organization Name (eg, company) [Internet Widgits Pty Ltd]:FMSoft
Organizational Unit Name (eg, section) []:R&D
Common Name (eg, YOUR name) []:Farshad Mohajeri
Email Address []:[email protected]

 

Note: 365 is the number days the certificate will remain valid.

 This will place a new root.pem file in the current folder. This file will be used in your uniGUI server.

 3、创建一个自签名key 

The next step is to generate a self-signed key. This step will produce the key.pem and cert.pem files.

 At command prompt issue the following command:

 openssl req -x509 -days 365 -nodes -newkey rsa:1024 -keyout key.pem -out cert.pem

 

Again, you'll be asked several questions.

 

Loading 'screen' into random state - done
Generating a 1024 bit RSA private key
........++++++
.....++++++
writing new private key to 'key.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
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) [AU]:TR
State or Province Name (full name) [Some-State]:Ankara
Locality Name (eg, city) []:Cankaya
Organization Name (eg, company) [Internet Widgits Pty Ltd]:FMSoft
Organizational Unit Name (eg, section) []:R&D
Common Name (e.g. server FQDN or YOUR name) []:Farshad Mohajeri
Email Address []:[email protected]

 

To create the same key with a password use this command:

 

openssl req -x509 -days 365 -newkey rsa:1024 -keyout key.pem -out cert.pem

 

This time you'll have to enter a password:

 

Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

 

This password should be assigned to  SSL->SSLPassword parameter of UniServerModule. (See SSL Configuration )

 

When all above procedures are completed, you will end up with three files named root.pem, key.pem and cert.pem which are required to setup and run your project in SSL mode. These files must be placed in the same folder as your server executable binary.

搜集上述创建的root.pem,key.pem,cert.pem三个文件,拷贝到后续创建的项目的执行文件统计目录D:\testSSL\Win32\Debug下。

同时将uniGUI安装目录C:\Program Files (x86)\FMSoft\Framework\uniGUI\SSL\dll\x86下的libeay32.dll、ssleay32.dll两个文件也复制到D:\testSSL\Win32\Debug下。

 二、创建一个WEB项目

1、创建一个项目,界面布局如下,保存在d:\testSSL下

(16)uniGUI for C++ builder下如何实现https加密浏览

(16)uniGUI for C++ builder下如何实现https加密浏览

在主窗口MainForm下随意放置一些uni控件用于演示WEB。

(16)uniGUI for C++ builder下如何实现https加密浏览

2、设置ServerModule的SSL属性

(16)uniGUI for C++ builder下如何实现https加密浏览

SSL->Enabled = True
SSL->SSLOptions->CertFile = cert.pem
SSL->SSLOptions->KeyFile = key.pem

SSL->SSLOptions->RootCertFile = root.pem

SSL->Method=sslvSSLv3

 

如果证书设置密码了,则设置一下密码属性SSL->SSLPassword = [Password] 

1)如果 ServerModule->SSL->SSLPort = 0,则系统默认使用ServerModule->Port = 8077作为https的端口,则访问地址为:

https://127.0.0.1:8077

此时可将8077修改为443实现不带端口的https访问:

https://127.0.0.1

2)如果想实现http和https两种访问都支持,则可以同时设定 ServerModule->SSL->SSLPort和ServerModule->Port端口

ServerModule->SSL->SSLPort = 443(SSL默认端口)

ServerModule->Port = 80(非SSL默认端口)

此时访问地址为:

http://127.0.0.1

https://127.0.0.1

 三、运行结果

(经测试,谷歌浏览器不支持该种加密交互方式,本人未进一步更换其它加密算法类型测试,360的可以。)

1、https访问

(16)uniGUI for C++ builder下如何实现https加密浏览

2、http访问

(16)uniGUI for C++ builder下如何实现https加密浏览


至此,一个既支持http访问也支持https访问的WEB站点就大功告成了。唉,杭州实在是太热了,谨以此文解解暑气吧。

                                                                  2018.7.13于杭州西湖