栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python实现rsa加密解密_rsa加密算法应用?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python实现rsa加密解密_rsa加密算法应用?

pip install rsa

rsaUtil.py

import base64
import random

import rsa


# https://stuvel.eu/python-rsa-doc/usage.html#generating-keys
class Key(object):
    def __init__(self, pubKey, priKey):
        self.pubKey = pubKey
        self.priKey = priKey


class RsaKeyData(object):
    def __init__(self, n, e, d, p, q):
        self.n = n
        self.e = e
        self.d = d
        self.p = p
        self.q = q


def get_key():
    (pubkey, priKey) = rsa.newkeys(512)
    key1 = Key(pubkey, priKey)
    return key1


def get_key_data():
    return get_key_data_by_size(512)


def get_key_data_by_size(keySize):
    (pubkey, priKey) = rsa.newkeys(keySize)
    return RsaKeyData(priKey.n, priKey.e, priKey.d, priKey.p, priKey.q)


def encrypt(pubkey, data):
    encrypt_res = rsa.encrypt(data, pubkey)
    return encrypt_res


def decrypt(privkey, data):
    content = rsa.decrypt(data, privkey)
    return content


def test(pub_key, pri_key):
    data = '你好世界 Hello world'.encode('utf-8')
    encrypt_data = encrypt(pub_key, data)
    # print(encrypt_data.hex())
    print(base64.encodebytes(encrypt_data).decode().replace('n', ''))
    decrypt_data = decrypt(pri_key, encrypt_data)
    print(decrypt_data.decode('utf-8'))


def rsa_test1():
    key1 = get_key()
    print(key1.pubKey)
    print(key1.priKey)
    test(key1.pubKey, key1.priKey)


def rsa_test_key():
    (pubkeyO, priKeyO) = rsa.newkeys(512)

    pubkeydata = pubkeyO.save_pkcs1()
    print(pubkeydata.hex())
    pubkey = rsa.PublicKey.load_pkcs1(pubkeydata)

    prikeydata = priKeyO.save_pkcs1()
    print(prikeydata.hex())
    privkey = rsa.PrivateKey.load_pkcs1(prikeydata)

    test(pubkey, privkey)


def rsa_test2():
    key_data = get_key_data_by_size(1024)
    pub_key = rsa.key.PublicKey(key_data.n, key_data.e)

    # print(key_data.p, key_data.q)
    p, q = getpq(key_data.n, key_data.e, key_data.d)
    # print(p, q)

    pri_key = rsa.key.PrivateKey(key_data.n, key_data.e, key_data.d, p, q)
    print(pub_key)
    print(pri_key)
    test(pub_key, pri_key)


def getpq(n, e, d):
    p = 1
    q = 1
    while p == 1 and q == 1:
        k = d * e - 1
        g = random.randint(0, n)
        while p == 1 and q == 1 and k % 2 == 0:
            k //= 2
            y = pow(g, k, n)
            if y != 1 and gcd(y - 1, n) > 1:
                p = gcd(y - 1, n)
                q = n // p
    return p, q


def gcd(a, b):
    if a < b:
        a, b = b, a
    while b != 0:
        temp = a % b
        a = b
        b = temp
    return a

main.py

from rsaUtil import *


def main():
    print('Hello world')
    rsa_test2()


if __name__ == '__main__':
    main()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号