使用openssl生成公钥和私钥

使用openssl生成公钥和私钥
 
C:\Users\Administrator\Desktop>openssl genrsa -out rsa_private_key.pem 1024
Generating RSA private key, 1024 bit long modulus
........++++++
........++++++
e is 65537 (0x010001)
 
C:\Users\Administrator\Desktop>openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
writing RSA key
 
 
C:\Users\Administrator\Desktop>openssl rsa -pubin -text -in rsa_public_key.pem
Public-Key: (1024 bit)
Modulus:
    00:d4:f9:91:b8:96:e0:c3:40:4c:6c:f0:90:33:ac:
    9d:1c:00:03:3f:55:71:86:84:56:51:1e:23:c4:7a:
    60:b4:06:ac:2c:f8:aa:07:a9:f6:b6:84:1b:65:76:
    0c:c4:cc:89:e1:f0:36:e7:7e:17:eb:c6:28:41:2b:
    b8:48:98:ec:56:6a:33:d4:9f:c3:e1:c8:07:66:5d:
    92:b3:d4:4f:da:4c:e4:70:38:c0:3f:95:4a:5f:1f:
    b7:d8:c9:e4:24:82:6e:65:0d:ae:e3:21:01:d4:46:
    91:a8:d9:c7:aa:d9:b7:c0:63:34:8d:e9:e3:00:a0:
    94:a3:3c:ba:7a:cd:4c:13:49
Exponent: 65537 (0x10001)
writing RSA key
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDU+ZG4luDDQExs8JAzrJ0cAAM/
VXGGhFZRHiPEemC0Bqws+KoHqfa2hBtldgzEzInh8DbnfhfrxihBK7hImOxWajPU
n8PhyAdmXZKz1E/aTORwOMA/lUpfH7fYyeQkgm5lDa7jIQHURpGo2ceq2bfAYzSN
6eMAoJSjPLp6zUwTSQIDAQAB    
-----END PUBLIC KEY-----
 
 
C:\Users\Administrator\Desktop>openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -out pkcs8_rsa_private_key.pem -nocrypt
 
C:\Users\Administrator\Desktop>echo  -n "Welcome to PYG" | openssl rsautl -sign -inkey rsa_private_key.pem |openssl enc -base64
htwYE3WpkfZaF18VEWXE/kaIhuJeIwccbQik5ibT2xSEgrQQQwV50j3htB3mUqda
SVeCjCK9tDwolwXJSJ81e0XoCbmWewD2b3QVi3g99gTzMvgstZbVb/xCXY/DfySX
OmmniDk1zQiLn6XILP4wPe7pNGEkwmaXEgQX8DssYGo=
 
 
 
在线加解密
 
 
 
 
Python实现RSA加解密
 
pip install pycrypt
# -*- coding: utf-8 -*-
 
 
from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
import base64
 
 
# 加密解密:公钥加密,私钥解密
#
# 签名验签:私钥签名,公钥验签
#
# 生成 private key and pulic key
print("1、生成 private key and pulic key")
 
 
# 伪随机数生成器
random_generator = Random.new().read
# rsa算法生成实例
rsa = RSA.generate(1024, random_generator)
 
 
# master的秘钥对的生成
private_pem = rsa.exportKey()
 
 
with open('master-private.pem', 'w') as f:
    f.write(private_pem)
 
 
public_pem = rsa.publickey().exportKey()
with open('master-public.pem', 'w') as f:
    f.write(public_pem)
 
 
# ghost的秘钥对的生成
private_pem = rsa.exportKey()
with open('ghost-private.pem', 'w') as f:
    f.write(private_pem)
 
 
public_pem = rsa.publickey().exportKey()
with open('ghost-public.pem', 'w') as f:
    f.write(public_pem)
 
 
# 加密和解密
print("2、加密和解密")
# Master使用Ghost的公钥对内容进行rsa 加密
 
 
message = 'hello ghost, this is a plian text'
print("message: " + message)
with open('ghost-public.pem') as f:
    key = f.read()
    rsakey = RSA.importKey(key)
    cipher = Cipher_pkcs1_v1_5.new(rsakey)
    cipher_text = base64.b64encode(cipher.encrypt(message))
    print("加密(encrypt)")
    print(cipher_text)
 
 
# Ghost使用自己的私钥对内容进行rsa 解密
 
 
with open('ghost-private.pem') as f:
    key = f.read()
    rsakey = RSA.importKey(key)
    cipher = Cipher_pkcs1_v1_5.new(rsakey)
    text = cipher.decrypt(base64.b64decode(cipher_text), random_generator)
 
 
    print("解密(decrypt)")
    print("message:" + text)
 
 
    assert text == message, 'decrypt falied'
 
 
# 签名与验签
print("3、 签名与验签")
 
 
# Master 使用自己的私钥对内容进行签名
print("签名")
with open('master-private.pem') as f:
    key = f.read()
    rsakey = RSA.importKey(key)
    signer = Signature_pkcs1_v1_5.new(rsakey)
    digest = SHA.new()
    digest.update(message)
    sign = signer.sign(digest)
    signature = base64.b64encode(sign)
 
 
print(signature)
 
 
print("验签")
with open('master-public.pem') as f:
    key = f.read()
    rsakey = RSA.importKey(key)
    verifier = Signature_pkcs1_v1_5.new(rsakey)
    digest = SHA.new()
    # Assumes the data is base64 encoded to begin with
    digest.update(message)
    is_verify = verifier.verify(digest, base64.b64decode(signature))
 
 
print(is_verify)
 
# -*- coding: utf-8 -*-
 
 
from Crypto import Random
from Crypto.PublicKey import RSA
print("generate private key and pulic key")
# 参考 C:\Python27\Lib\site-packages\Crypto\PublicKey\RSA.py
######################################################
# 利用伪随机数来生成私钥和公钥
random_generator = Random.new().read
rsa = RSA.generate(2048, random_generator)
private_pem = rsa.exportKey()
f = open('MY_KEY1_pri.pem', 'wb')
f.write(private_pem)
f.close()
public_pem = rsa.publickey().exportKey()
f = open('MY_KEY1_pub.pem', 'wb')
f.write(public_pem)
f.close()
######################################################
# 利用默认的generate来生成私钥和公钥
rsa = RSA.generate(2048)
private_pem = rsa.exportKey('PEM')
f = open('MY_KEY2_pri.pem', 'wb')
f.write(private_pem)
f.close()
public_pem = rsa.publickey().exportKey()
f = open('MY_KEY2_pub.pem', 'wb')
f.write(public_pem)
f.close()
######################################################
# 根据已有的RSA私钥来生成公钥
f = open('MY_KEY2_pri.pem', 'rb')
rsa = RSA.importKey(f.read())
f.close()
public_pem = rsa.publickey().exportKey()
f = open('MY_KEY3_pub.pem', 'wb')
f.write(public_pem)
f.close()
######################################################
# 根据已有的RSA PEM格式的私钥来转换成DER格式的私钥
f = open('MY_KEY2_pri.pem', 'rb')
rsa = RSA.importKey(f.read())
f.close()
private_der = rsa.exportKey('DER')
f = open('MY_KEY3_pri.der', 'wb')
f.write(private_der)
f.close()
'''
————————————————
版权声明:本文为CSDN博主「cfl927096306」的原创文章,遵循
CC
4.0
BY - SA
版权协议,转载请附上原文出处链接及本声明。
原文链接:https: // blog.csdn.net / cfl927096306 / article / details / 79888428
'''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2020-03-23 22:28  heycomputer  阅读(739)  评论(0编辑  收藏  举报