linux下OpenSSL的RSA密钥生成

时间:2021-05-05 03:46:48

工具的安装:

一.源码安装

OpenSSL Version:openssl-1.0.0e.tar.gz

------------------------安装:

1、将下载的压缩包放在根目录,

2、在文件夹下解压缩,

命令:tar -xzf  openssl-openssl-1.0.0e.tar.gz

得到openssl-openssl-1.0.0e文件夹

3、进入解压的目录:cd openssl-1.0.0e

4、设定Openssl 安装,( --prefix )参数为欲安装之目录,也就是安装后的档案会出现在该目录下:

执行命令:./config  --prefix=/usr/local/openssl

5、执行命令./config  -t

6.执行make,编译Openssl

----------------------完成安装。

二,yum安装

#yum install openssl* -y

1.生成秘钥:(生成1024位的私钥,不指定的话默认2048位)

输入“genrsa -out rsa_private_key.pem 1024”命令,回车后,在当前 bin 文件目 录中会新增一个 rsa_private_key.pem 文件,其文件为原始的商户私钥(请妥善保 存该文件,PHP 开发语言中需要使用该文件)

[root@wentao12 ~]# openssl genrsa -out rsa_private_key.pem 

2.生成公钥:(根据这个私钥生成公钥)

输入“rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem”命令回车 后,在当前 bin 文件目录中会新增一个 rsa_public_key.pem 文件,其文件为原始 的商户公钥(请妥善保存该文件,PHP 开发语言中需要使用该文件)

[root@wentao12 ~]# openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

3.生成PKCS8 编码的私钥

输入命令“pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt”并回车,当前界面中会直接显示出生成结果

[root@wentao12 ~]# openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt

linux下OpenSSL的RSA密钥生成

,把复制的内容粘 贴进一个新的记事本中,可随意命名,只要知道这个是 PKCS8 格式的私钥即可(请 妥善保存该文件)

经过我测试就用屏幕打印的这个作为私钥,

公钥查看:#cat rsa_public_key.pem

注意文件格式前后标记符不能去掉,可以正常加密解密。

例子:

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/4/1
* Time: 1:50
*/ //注意格式一定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/JT3UzgzD6X
ybxwxyupVw8aK1QZDYwn9LVqru2WjpG99JXxEcSP+vY=
-----END RSA PRIVATE KEY-----"; $str = "这个是要加密的字符4567串888"; //这个例子是演示RSA加密
//非对称加密 分为 公钥和私钥
//通常 公钥加密 私钥解密,私钥加密,公钥解密 /*
* 用到的加密函数
openssl_private_decrypt — Decrypts data with private key
openssl_private_encrypt — Encrypts data with private key
openssl_public_decrypt — Decrypts data with public key
openssl_public_encrypt — Encrypts data with public key
*
*/ //echo $private_key;
$pi_key = openssl_pkey_get_private($pri_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
$pu_key = openssl_pkey_get_public($pub_key);//这个函数可用来判断公钥是否是可用的
//print_r($pi_key);echo "\n";
//print_r($pu_key);echo "\n"; //公钥加密过程
$enStr = '';
openssl_public_encrypt($str,$enStr,$pu_key);
//加密后的字符 有些无法显示 需要 base64_encode ( string $data ) ,解密的时候不需要要 base64_decode ( string $data )
echo base64_encode ($enStr);
echo "<br/>"; //私钥解密过程
$deStr = '';
//$enStr = base64_decode ( $enStr);
openssl_private_decrypt($enStr,$deStr,$pi_key);
echo $deStr;
echo "<br/>"; //下面是签名验证例子,验证签名 一定是用公钥去验证 对方私钥加密的结果
$binary_signature = "";
// At least with PHP 5.2.2 / OpenSSL 0.9.8b (Fedora 7)
// there seems to be no need to call openssl_get_privatekey or similar.
// Just pass the key as defined above
openssl_sign($str, $binary_signature, $pi_key, OPENSSL_ALGO_SHA1); // Check signature
$ok = openssl_verify($str, $binary_signature, $pu_key, OPENSSL_ALGO_SHA1); echo "check #1: ";
if ($ok == 1) {
echo "signature ok (as it should be)\n";
} elseif ($ok == 0) {
echo "bad (there's something wrong)\n";
} else {
echo "ugly, error checking signature\n";
} //var_dump(openssl_get_cipher_methods() );

生成秘钥:http://php.net/manual/en/function.openssl-private-encrypt.php

$res = openssl_pkey_new();

// Get private key
openssl_pkey_export($res, $privkey); // Get public key
$pubkey = openssl_pkey_get_details($res);
$pubkey = $pubkey["key"];
var_dump($privkey);
var_dump($pubkey);

参考:

http://stephen830.iteye.com/blog/2087281

https://www.cnblogs.com/jaryoung/p/7644481.html