6

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?

10
  • One bug I noticed - if the cleartext is a multiple of the block size, when you finish the last block, it goes to the "incomplete last block" code instead of the code for a complete block. Commented Dec 3, 2013 at 2:57
  • I recommend factoring out parts of this code into helper functions. For example, pull out the code for encrypting and decrypting individual blocks. Commented Dec 3, 2013 at 3:01
  • In the incomplete last block I'm doing the same stuff I did inside my loop, it's just in case I get some "lonely" bits that doesn't have the size I defined for blocks. Commented Dec 3, 2013 at 3:02
  • Thank you I'll separate the code. :) I've already tried this methods in so many ways to make it work that I haven't organize my code better. Commented Dec 3, 2013 at 3:03
  • 1
    Can you use some existing tool and compare your encrypted/decrypted text with a "known-good" result? This should let you drill down and find out if the error in in the encryption or decryption (or both). Commented Dec 16, 2013 at 9:37

1 Answer 1

1

I already found 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.