I'm doing a work for school in which I've got to implement rsa generating of public/private keys and encryption/decryption of a binary message. I already generate public/private keys but my encrypt/decrypt functions aren't working. I don't get any execution errors but the message I encrypt isn't the same when I decrypt.
My code for encrypting a block:
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
My encryption code:
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
And my decryption method:
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
So for example if my message is:
11001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011110011001111001100111100110011
I get this encrypted message (with 64 or plus bits for key size):
0101110010110010100110111010111011111001001101010110010100000110011011101111101111100000011110101101010000000001010110000111010101001000111100000011110110110011111001111000111000101011000000101111000100110100100100010100000000011101111110111101100011110011010001111000000101010010100111010010001000110010100111111000101001010101100010101001010000110010001101000111001111110010110111001000100101001000100100110011010101000111100101100111010110010000101111100111001001100110111110000100100001010100100110110100100011100010010100101000111011101101111110001000010111101111110000100001011100110010101111010010001011101000111100110101110111011100100001000010100011010001010111010000011100111100001110100100011100000101011011000001010001011101011111010110111001111001011001100001010010110000
And when I decrypt I get this:
0000110010111101000001100010110000010000110110111110001010110011100010111010111001100011110101100
Does anyone have any idea why this isn't working?
I already find the error. I got a problem with my prime generation. But in this code I've got a problem anyway.
In encryption, I verify if I've got some block to encrypt after my loop finishes. But in decryption I wasn't doing that validation and my last block was never decrypted. I could put the
if(i == '1' or i == '0'):
tmp_text += i
In the beginning of my for or I'll just put an if in the end so I won't lose my last block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With