zoukankan      html  css  js  c++  java
  • Python 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

    【转】https://blog.csdn.net/ts__cf/article/details/47862911

  • 相关阅读:
    CruiseControl.NET与TFS结合的配置文件
    环信Restfull API dotnetSDK
    NAnt0.92版本首次在windows 8.1的机子上运行报错的问题解决
    asp.net接收ajax请求参数时为空的现象
    对接微信红包时:CA证书出错,请登录微信支付商户平台下载证书
    在打开vs解决方案时,怎样让所以打开的项目自动折叠
    使用Chrome或Fiddler抓取WebSocket包
    SVN使用教程
    禁用Resharper长代码自动换行的解决办法
    SQLServer日期格式化
  • 原文地址:https://www.cnblogs.com/lged/p/9548204.html
Copyright © 2011-2022 走看看