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

  • 相关阅读:
    Qt的网络通信(以一对一聊天室为例)
    C/C++中的const ,static
    Qt的认识与相关问题的解决
    Qt的简单介绍,发展和由来
    STL模板的基础与了解
    malloc与new,C++中的指针与引用,C++struct与class的区别
    Qt的常用控件及控件的使用
    Qt的环境与工具、信号与槽
    学习Qt前你需要知道的
    模板与STL小结--vector、list、map等各类容器
  • 原文地址:https://www.cnblogs.com/lged/p/9548204.html
Copyright © 2011-2022 走看看