简体   繁体   English

实施RSA加密/解密

[英]Implementing RSA encryption/decryption

I'm trying to implement RSA encryption/decryption (a simple implementation of course, so there's no need for nitpicking) and it seems like the numbers (ie the keys) are fine, yet the final result is wrong. 我正在尝试实现RSA加密/解密(当然,这是一种简单的实现,因此不需要进行挑剔),而且看起来数字(即密钥)还可以,但是最终结果是错误的。

Here's the problematic function: 这是有问题的功能:

def square_and_mult(base, k, mod):
    b = 1
    for ki in reversed(k):
        if ki == '0':
            b = (b**2) % mod
        else:
            b = ((b**2) * base) % mod
    return b

I suspect something is wrong with your modular exponentiation algorithm. 我怀疑您的模幂运算算法有问题。 As you're using python you can use the builtin pow(base, exp, mod) to do this, no need to implement it yourself. 当您使用python时,可以使用内置的pow(base, exp, mod)来执行此操作,而无需自己实现。

In [1]: n = 48961353722289327881

In [2]: e = 7

In [3]: d = 6994479101184233143

In [4]: x = 12345678

In [5]: c = pow(x, e, n)

In [6]: c
Out[6]: 32225547235202030473

In [7]: m = pow(c, d, n)

In [8]: m
Out[8]: 12345678

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM