zoukankan      html  css  js  c++  java
  • kkk

    #!/usr/bin/env python3
    # coding=utf-8
    # Author: yannanxiu
    """
    create_rsa_key() - 创建RSA密钥
    my_encrypt_and_decrypt() - 加密解密测试
    """
    from Cryptodome.PublicKey import RSA
    from Cryptodome.Cipher import PKCS1_OAEP, PKCS1_v1_5
    def create_rsa_key(password="123456"):
    """
    创建RSA密钥
    步骤说明:
    1、从 Crypto.PublicKey 包中导入 RSA,创建一个密码
    2、生成 1024/2048 位的 RSA 密钥
    3、调用 RSA 密钥实例的 exportKey 方法,传入密码、使用的 PKCS 标准以及加密方案这三个参数。
    4、将私钥写入磁盘的文件。
    5、使用方法链调用 publickey 和 exportKey 方法生成公钥,写入磁盘上的文件。
    """
    key = RSA.generate(1024)
    encrypted_key = key.exportKey(passphrase=password, pkcs=8,
    protection="scryptAndAES128-CBC")
    with open("my_private_rsa_key.bin", "wb") as f:
    f.write(encrypted_key)
    with open("my_rsa_public.pem", "wb") as f:
    f.write(key.publickey().exportKey())
    def encrypt_and_decrypt_test(password="123456"):
    # 加载公钥
    recipient_key = RSA.import_key(
    open("my_rsa_public.pem").read()
    )
    cipher_rsa = PKCS1_v1_5.new(recipient_key)
    en_data = cipher_rsa.encrypt(b"123456")
    print(len(en_data), en_data)
    # 读取密钥
    private_key = RSA.import_key(
    open("my_private_rsa_key.bin").read(),
    passphrase=password
    )
    cipher_rsa = PKCS1_v1_5.new(private_key)
    data = cipher_rsa.decrypt(en_data, None)
    print(data)
    if __name__ == '__main__':
    # create_rsa_key()
    encrypt_and_decrypt_test()

  • 相关阅读:
    Global Citizenship
    Eng Stu
    说说
    C#编程远程控制机械手臂
    切割系统
    C#编码 画图控件
    编程Sourceforge
    C#编程线程
    空间点的几何关系
    一台普通电脑通过设置连接到公司网络
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11490252.html
Copyright © 2011-2022 走看看