python 利用pycrypto进行rsa生成公钥、私钥,加密、解密、签名、解签

1、安装 pycrypto

pip install pycrypto

2、利用pycrypto进行rsa生成公钥、私钥,加密、解密、签名、解签

# -*- 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




3、google billing rsa验证支付 python版本

# 验证商品
# param base64PublicKey base64加密的公钥
# param signedData 数据
# param signature 签名
def verifyPurchase(base64PublicKey, signedData, signature):
    if base64PublicKey == "" or signedData == "" or signature == "":
        return False;
    publicKey = generatePublicKey(base64PublicKey)
    return verify(publicKey, signedData, signature)


# 生成公钥
def generatePublicKey(base64PublicKey):
    try:
        return base64.b64decode(base64PublicKey)
    except Exception, e:
        return ""


# 验证签名
# param publicKey 公钥
# param signedData 需要验证的数据
# param signature 签名
def verify(publicKey, signedData, signature):
    try:
        decodeSig = base64.b64decode(signature)
    except Exception, e:

        return False

    try:
        rsakey = RSA.importKey(publicKey)

        verifier = Signature_pkcs1_v1_5.new(rsakey)

        digest = SHA.new()

        digest.update(signedData)

        is_verify = verifier.verify(digest, decodeSig)
    except Exception, e:
        is_verify = False

    return is_verify

  • 4
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值