openssl usage

本文详细介绍了使用 OpenSSL 创建证书的过程,包括创建 CA 密钥和证书,验证 CA 证书,以及为 Web 服务器创建证书并进行配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.  openssl genrsa -out key.pem 1024

2.  openssl req -new -key key.pem -config /etc/ssl/openssl.cnf -out request.pem

Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:BeiJing               
Locality Name (eg, city) []:BeiJing 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:www.test.com
Organizational Unit Name (eg, section) []:test.cn
Common Name (eg, YOUR name) []:zhangsan
Email Address []:zhangsan@test.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:test2011
An optional company name []:test_company

3.  openssl x509 -req -days 9999 -in request.pem -signkey key.pem -out certificate.pem

4.  openssl pkcs8 -topk8 -outform DER -in key.pem -inform PEM -out key.pk8 -nocrypt

 

 ======================================================================

mutual ca authentication steps (main certification and key creating procedure)

1.   Creating the CA Key and Certificate

The general process for creating a certificate includes:

       1.1  Creating a private key

            openssl genrsa -out CA.key 1024

       1.2  Creating a certificate request

               openssl req -new -key CA.key -out CA.csr -config ..\openssl.cnf

Enter pass phrase for Server.key:
Loading 'screen' into random state - done
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]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:yuvad
Organizational Unit Name (eg, section) []:yuvadbj
Common Name (eg, YOUR name) []:yuv
Email Address []:bj@yuvad.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:hello
An optional company name []:yuvad.cn


        1.3 Creating and signing a certificate from the certificate request
               openssl x509 -req -days 3650 -in CA.csr -out CA.crt -signkey CA.key

2.  Verifying the CA certificate contents (option)

     At this point we have our self-signed CA certificate and our CA key, which will be

    used to sign the web server and client certificates that we create. To verify the

    certificate contents, use the following command:

    openssl x509 -in CA.crt -text

3.   Creating a Web Server Certificate
3.1   The procedure for creating a web server certificate is
similar to that for creating the CA certificate except
that the web server certificate will be signed using
the CA key rather than self-signing with a web
server-specific key.

Command:

openssl genrsa -aes128 -out server.key 1024

and input pass phrase when prompt

3.2     Next, create the web server certificate request for the private key.
When prompted for the pass phrase for the keys, enter the pass
phrase that you used for the private key.

Command:

openssl req -new -key Server.key -out Server.csr -config ..\openssl.cnf

Enter pass phrase for Server.key:
Loading 'screen' into random state - done
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]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:yuvad
Organizational Unit Name (eg, section) []:yuvadbj
Common Name (eg, YOUR name) []:yuv
Email Address []:bj@yuvad.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:hello
An optional company name []:yuvad.cn

3.3    Then, sign the web server certificate with the CA key

Command:

openssl ca -days 3650 -in Server.csr -cert CA.crt -keyfile CA.key -out Server.crt -config ..\openssl.cnf

notes:

at first, modify conf/openssl.cnf file, set dir  = ../DemoCA, because above command is run in conf/ssl

additionally, DemoCA directory should be created in conf directory

and certs, crl, newcerts directory need be created in DemoCA

file index.txt and serial should be create in DemoCA and 01 is writed into serial file.

 

Using configuration from ..\openssl.cnf
Loading 'screen' into random state - done
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Dec 30 07:26:14 2011 GMT
            Not After : Dec 27 07:26:14 2021 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Beijing
            organizationName          = yuvad
            organizationalUnitName    = yuvadbj
            commonName                = yuv
            emailAddress              = bj@yuvad.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                F8:7E:C4:9B:2E:8E:B4:DB:48:97:00:97:66:9A:D9:10:93:2A:B8:2B
            X509v3 Authority Key Identifier:
                DirName:/C=CN/ST=Beijing/L=Beijing/O=yuvad/OU=yuvadbj/CN=yuv/ema
ilAddress=bj@yuvad.com
                serial:B4:EE:50:3B:C9:D1:7A:9C

Certificate is to be certified until Dec 27 07:26:14 2021 GMT (3650 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

 3.4   To verify the web server certificate contents, use the following command (optional)

openssl x509 -in Server.crt -text

### 如何在 Qt 中集成和使用 OpenSSL #### 安装与配置 OpenSSL 为了使 OpenSSL 在 Qt 项目中可用,需先安装并配置 OpenSSL 库。对于 Windows 用户,在 MinGW 或 MSVC 编译环境下,可以从官方网站获取预编译二进制文件[^1]。 完成安装后,设置环境变量 `PATH` 包含 OpenSSL 的 bin 文件夹路径,以便应用程序能够找到所需的 DLLs。接着修改 `.pro` 工程文件来链接静态或共享版本的 OpenSSL 库: ```qmake win32 { LIBS += -LC:/path/to/OpenSSL/lib \ -llibssl -llibcrypto INCLUDEPATH += C:/path/to/OpenSSL/include } ``` #### 测试 OpenSSL 是否正常工作 确认 OpenSSL 正确加载的方法之一是利用 `QSslSocket::supportsSsl()` 函数返回布尔值判断当前平台是否支持 SSL 协议;另一个方式则是借助 `QNetworkAccessManager` 对象尝试发起 HTTPS 请求,并观察响应状态码以及 URL 方案字段是否存在 "https"。 #### 实现基本加密操作 以 RSA 加密为例,可以采用命令行工具 `openssl rsautl` 来执行具体的加解密动作。具体而言,通过 `-encrypt`, `-decrypt` 参数配合公钥私钥文件对消息体实施编码转换过程[^2]: ```bash # 假设已经准备好待处理的数据文件hello.txt 和 私钥test.key openssl rsautl -encrypt -in hello.txt -pubin -inkey public.pem -out hello.enc openssl rsautl -decrypt -in hello.enc -inkey private.pem -out hello.dec ``` 当然也可以直接编程调用相应的 API 接口完成相同的功能逻辑。下面给出一段简单的 Python 伪代码展示如何基于 PyCryptoDome 扩展包封装类似的业务流程(注意实际生产环境中应选用更安全的方式管理秘钥材料): ```python from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP def rsa_encrypt(message, pub_key_path): with open(pub_key_path,'rb') as f: key = RSA.import_key(f.read()) cipher_rsa = PKCS1_OAEP.new(key) encrypted_data = cipher_rsa.encrypt(message.encode('utf-8')) return encrypted_data def rsa_decrypt(ciphertext,private_key_path): with open(private_key_path,'rb') as f: key=RSA.import_key(f.read()) decipher_rsa=PKCS1_OAEP.new(key) decrypted_message=decipher_rsa.decrypt(ciphertext).decode('utf-8') return decrypted_message ``` 请注意上述例子仅作为教学用途,真实世界里应当遵循最佳实践指南妥善保存敏感信息并且考虑性能因素选择合适的算法强度。 #### Android 平台上简化 OpenSSL 集成 考虑到移动设备上的特殊需求,特别是针对 Android 系统下的 Qt 开发者们面临的挑战——即繁琐的手动编译步骤可能耗费大量时间精力。为此有开发者分享了一份名为 `android_openssl-master.zip` 的压缩包资源,里面包含了预先构建好的适合 ARM 架构处理器运行的 OpenSSL 版本,极大地方便了相关人员快速上手开展工作[^3]。 #### DES ECB 模式的实现案例 除了非对称密码体系之外,有时也会遇到需要运用到对称密钥机制的情况,比如 DES 算法中的 ECB 操作模式。这里有一个 GitCode 上托管着开源项目的实例可供参考学习,该项目不仅打包好了适用于特定编译器链路状况下的 OpenSSL 动态连接库,还附带了一套完整的演示样例用来指导初学者掌握基础概念和技术要点[^4]. ```cpp #include <openssl/des.h> // ... other includes ... void des_ecb_example(const unsigned char *input, size_t input_len, const unsigned char *key, unsigned char **output, size_t *output_len) { DES_cblock key_schedule; DES_set_odd_parity((DES_cblock *)key); DES_set_key_checked((const_DES_cblock *)key, &key_schedule); // Allocate enough space for output (same length as input plus padding). (*output_len) = ((input_len + DES_BLOCK_SIZE - 1)/DES_BLOCK_SIZE)*DES_BLOCK_SIZE; (*output) = new unsigned char[*output_len]; memset(*output,0,*output_len); /* Zero out the buffer */ DES_ncbc_encrypt(input, *output, input_len, &key_schedule, nullptr, DES_ENCRYPT); } int main(){ // Example usage of function defined above... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值