RSA encrypts / decrypts failure in my own python implementation

I am doing work in a school in which I need to implement rsa generating public / private keys and encrypting / decrypting a binary message. I already generate public / private keys, but my encryption / decryption functions do not work. I do not get any runtime errors, but the encryption of the message is not the same when I decrypt.

My code for block encryption:

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 the following:

 0000110010111101000001100010110000010000110110111110001010110011100010111010111001100011110101100 

Does anyone know why this is not working?

+6
source share
1 answer

I already found an error. I have problems with my generation. But in this code I still have a problem.

In encryption, I check if I have a block for encryption after the loop ends. But in decryption, I did not perform this check, and my last block was never decrypted. I could put

 if(i == '1' or i == '0'): tmp_text += i 

at the beginning of mine or I just put an if if at the end so that I don't lose my last block.

Anyway, thanks for your suggestions :)

+1
source

Source: https://habr.com/ru/post/959207/


All Articles