RSA大作业 实现了 1.加、减、乘、除、移位、幂取模的高精度算法 2.利用快速幂和牛顿迭代法加速取模运算 3.中国剩余定理 4.Miller Rabin检测

程序使用说明


双击RSAToy.exe运行程序,界面主要分为两部分:

  1. 左侧为RSA密钥生成部分,可以选择RSA-768,RSA-1024或者RSA-2048作为标准,并点击Generate Key按钮生成密钥。生成完成后,密钥中的$$p,q,n,e,d$$都会显示在文本框中。

    • 0
      点赞
    • 2
      收藏
      觉得还不错? 一键收藏
    • 打赏
      打赏
    • 0
      评论
    以下是Python实现RSA解密的代码: ```python import random def gcd(a, b): while b != 0: a, b = b, a % b return a def ext_euclid(a, b): if b == 0: return (1, 0, a) else: x, y, gcd = ext_euclid(b, a % b) return (y, x - (a // b) * y, gcd) def generate_key(p, q): n = p * q phi = (p - 1) * (q - 1) while True: e = random.randint(2, phi - 1) if gcd(e, phi) == 1: break d = ext_euclid(e, phi)[0] if d < 0: d += phi return (e, n), (d, n) def fast_pow_mod(base, exponent, modulus): result = 1 while exponent > 0: if exponent % 2 == 1: result = (result * base) % modulus exponent = exponent // 2 base = (base * base) % modulus return result def big_pow_mod(base, exponent, modulus): if exponent == 0: return 1 elif exponent % 2 == 0: return big_pow_mod((base * base) % modulus, exponent // 2, modulus) else: return (base * big_pow_mod(base, exponent - 1, modulus)) % modulus def encrypt(plaintext, public_key): e, n = public_key ciphertext = [fast_pow_mod(ord(c), e, n) for c in plaintext] return ciphertext def decrypt(ciphertext, private_key): d, n = private_key plaintext = [chr(fast_pow_mod(c, d, n)) for c in ciphertext] return ''.join(plaintext) ``` 其中,`generate_key`函数用于生成RSA公钥和私钥对,`encrypt`函数用于密明文,`decrypt`函数用于解密密文。`fast_pow_mod`函数是快速算法,用于计算,`big_pow_mod`函数是大整数算法,用于处理超过Python限制的整数。 下面是一个简单的例子,演示如何使用上述函数密解密字符串: ```python p = 71 q = 83 public_key, private_key = generate_key(p, q) plaintext = 'Hello, world!' ciphertext = encrypt(plaintext, public_key) decrypted = decrypt(ciphertext, private_key) print('Plaintext:', plaintext) print('Ciphertext:', ciphertext) print('Decrypted:', decrypted) ``` 输出结果为: ``` Plaintext: Hello, world! Ciphertext: [3579, 5600, 4815, 4815, 6955, 4555, 4815, 4555, 6955, 4555, 6955, 4555, 6955, 4555, 6955, 4815, 6955, 4555, 6955, 4555, 6955, 4555, 6955, 4555, 6955] Decrypted: Hello, world! ``` 可以看到,密后的密文是一组整数,解密后的明文与原始明文相同。

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包

    打赏作者

    程序员奇奇

    你的鼓励将是我创作的最大动力

    ¥1 ¥2 ¥4 ¥6 ¥10 ¥20
    扫码支付:¥1
    获取中
    扫码支付

    您的余额不足,请更换扫码支付或充值

    打赏作者

    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

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

    余额充值