简体   繁体   English

在python中使用rsa加密时处理零

[英]Dealing with zeros when encrypting with rsa in python

I have been given a word "NETLAB" and have been told to encrypt it the following way by giving each letter a two-digit positional code, so this then becomes 14, 05, 20, 12, 01, 02. The next step is to deal with these in 3 blocks on 4 digits so they become 1405, 2012 and 0102, I can encrypt and decrypt the first two fine, however, I have run into a problem trying to encrypt and decrypt with '0102', I am pretty certain this is due to the number starting with 0 and that with it being an int the 0 is disregarded, which does not help when encrypting/decrypting data. 我得到一个单词“ NETLAB”,并被告知要通过以下方式对其进行加密,即给每个字母一个两位数字的位置代码,这样它就变成14、05、20、12、01、02。下一步是以4位数字在3个区块中处理它们,使它们成为1405、2012和0102,我可以对前两个罚款进行加密和解密,但是,尝试用'0102'进行加密和解密时遇到了问题,我很漂亮可以肯定的是,这是由于以0开头的数字,而由于它是一个int,所以忽略了0,这在加密/解密数据时无济于事。 So my question is how do I either stop the 0 from being disregarded? 因此,我的问题是如何停止忽略0? I have tried putting all the number into an array then concatenating them but I run into problems when using lists and powers of calculations. 我曾尝试将所有数字放入数组中,然后将它们连接起来,但是在使用列表和计算能力时遇到了问题。 My code is below: 我的代码如下:

from __future__ import division
import fractions
import itertools
import urllib2

ml = []
p = 47
q = 59
x = 2
d = 0
e = 0


z = (p-1)*(q-1)
print z
n = p*q
print n

for x in range(z):
    if (fractions.gcd(x, z) == 1):
        ml.append(x)
    ##print ml

s = 2
for s,x in itertools.product(range(65),ml):
    t = s * x 
    if t == z + 1:
        ##print s
        ##print x
        break
d = x
e = s
print d
print e

med = []
message = "NETLAB"
for c in message:
    m = ord(c) - 64
    m = ("%02d" % m)
    med.append(m)
    print m

print "m^e(mod n)"

A = med.join(0, 1)
##B = 
##C = 


Ea = A ** e % n
##Eb = B ** e % n
##Ec = C ** e % n 

print Ea
##print Eb
##print Ec


print "Ea^d(mod n)"
Da = Ea ** d % n
##Db = Eb ** d % n
##Dc = Ec ** d % n

print Da
##print Db
##print Dc

I think this all depends on what you mean by the leading 0 being "disregarded". 我认为这一切都取决于您“无视”前导0的含义。 When you apply the following expression, you're implying that A is an integer: 应用以下表达式时,意味着A是整数:

Ea = A ** e % n

In positional notation leading zeros have no value, but this does not mean they are being disregarded. 在位置表示法中,前导零没有价值,但这并不意味着它们会被忽略。 The integer 0102 will always have the value of 102, whereas for any other x, x102 will not have the value 102. 整数0102将始终具有值102,而对于任何其他x,x102将不会具有值102。

As for the output (let's say you were writing this to a file, or some other encoding where the leading 0 is important), then it's simply a matter of formatting. 至于输出(假设您正在将其写入文件或其他重要的前0编码),那么这仅仅是格式化问题。 For example, you could try the following function: 例如,您可以尝试以下功能:

def encrypt_decrypt(m,d,e,n):
    print "m           %4s" % m

    Ea = (int(m) ** e) % n
    print "Ea          %d" % Ea

    Da = (Ea ** d) % n
    print "Ea^d(mod n) %04d" % Da

Calling this function with 调用此函数

encrypt_decrypt(''.join(med[0:2]),d,e,n)
encrypt_decrypt(''.join(med[2:4]),d,e,n)
encrypt_decrypt(''.join(med[4:6]),d,e,n)

Gives the output 给出输出

m           1405
Ea          2641
Ea^d(mod n) 1405
m           2012
Ea          1453
Ea^d(mod n) 2012
m           0102
Ea          1395
Ea^d(mod n) 0102

(Note that I used the following... (请注意,我使用了以下内容...

p = 47
q = 59
x = 2
d = 5
e = 0

...) ...)

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

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