我正在为学校做一项工作,我必须实现rsa生成公钥/私钥和加密/解密二进制消息。我已经生成了公钥/私钥,但我的加密/解密功能不起作用。我没有得到任何执行错误,但是当我解密时,我加密的消息不一样。
我加密块的代码:
def encrypt_block(block,block_size,e,n):
number = int(block,2) #convert to decimal number
cipher_number = pow(number,e,n) #method for fastest exponentiation number^e mod n
cipher_size = len(bin(cipher_number)) - 2
tmp_text = '{0:b}'.format(cipher_number)
while(len(tmp_text)<block_size): #add zeros to left to fill until block_size
tmp_text = "0" + tmp_text
return tmp_text
我的加密代码:
block_size = len(bin(n-1)) - 2 #size of encrypted blocks
text_size = block_size - 5 #size of clear text blocks
tmp_text = "" #variable for holding current block
encrypted_message = ""
for i in data:
if(len(tmp_text)==text_size): #when a block is complete
tmp_text = encrypt_block('1'+tmp_text,block_size,e,n) #add '1' so I don t loose left zeros
encrypted_message += tmp_text
tmp_text = ""
if(i == '0' or i == '1'): #just precaution so I won t add other characters
tmp_text += i
if(tmp_text != ""): # in case last block isnt the clear text size
tmp_text = encrypt_block('1'+tmp_text,block_size,e,n) #add '1' so I don t loose left zeros
encrypted_message += tmp_text
print encrypted_message
我的解密方法:
block_size = len(bin(n-1)) - 2
tmp_text = ""
decrypted_message = ""
for i in data:
if(len(tmp_text) == block_size):
number = int(tmp_text,2)
plain_number = pow(number,d,n)
decrypted_message += '{0:b}'.format(plain_number)[1::] #remove the '1' that I added in all blocks to prevent loosing zeros
if(i == '1' or i == '0'):
tmp_text += i
print decrypted_message
例如,如果我的消息是:
11001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011
我收到此加密消息(密钥大小为64位或更多位):
0101110010110010100110111010111011111001001101010110010100000110011011101111101111100000011110101101010000000001010110000111010101001000111100000011110110110011111001111000111000101011000000101111000100110100100100010100000000011101111110111101100011110011010001111000000101010010100111010010001000110010100111111000101001010101100010101001010000110010001101000111001111110010110111001000100101001000100100110011010101000111100101100111010110010000101111100111001001100110111110000100100001010100100110110100100011100010010100101000111011101101111110001000010111101111110000100001011100110010101111010010001011101000111100110101110111011100100001000010100011010001010111010000011100111100001110100100011100000101011011000001010001011101011111010110111001111001011001100001010010110000
当我解密时,我得到了这个:
0000110010111101000001100010110000010000110110111110001010110011100010111010111001100011110101100
有谁知道为什么这不起作用?
答案 0 :(得分:1)
我已经找到了错误。我和我的黄金一代有问题。但是在这段代码中,无论如何我都遇到了问题。
在加密中,我验证在循环完成后是否有一些块加密。但在解密时我没有进行验证,我的最后一个块从未被解密过。
我可以if(i == '1' or i == '0'):
tmp_text += i
在我的开头或者我只是把一个if放在最后所以我不会松开我的最后一个块。
无论如何,谢谢你的建议:)