Uncategorized

algorithm – Python unicode encryption algorithim


I am building an encryption algorithm. It firstly takes the message you want to encrypt and an encryption key which can be more than 8 digits. It takes the message, using ‘for’ in Python it strips apart each char and puts them in a variable called ‘char’. Now, it checks whether the ord(char) + encryption key is odd or even. If it’s even then it subtracts the ord(char) by the key. If it’s odd then it adds. it converts these digits to char by chr() and after every character being passed by this process, it displays the output to the user

Now, you might have noticed if let’s say the result of ord(char) + key was odd, and the number was more than 1114111 (that’s the number of usable unicode points), In that case it uses this algo-

            checkpoint = key - (1114111 - ord(char))
            if checkpoint < 1114111:
               mes += chr(checkpoint)
            else:
                mes += chr(checkpoint % 1114111)

and in if ord(char) + key was even and if key was more than ord(char), then it will use this-

            checkpoint = key - (1114111 - ord(char))
            if checkpoint < 1114111:
               mes += chr(checkpoint)
            else:
                mes += chr(checkpoint % 1114111)

The problem with my program is that when I use decryption, and the ord(char) + key is odd and more than 1114111, it simply gives me the wrong decryption. My tests indicate this and I can’t figure how do I correct this problem. I will feel extremely grateful if you can correct my code or if you find out that there is a different twist to this problem or a new approach. I have posted the full code of both the programs (encryption and decryption)-

# encryption
code = str(input('Write your message, \t'))
key = int(input('Write your encryption key, use only numbers, \t'))
mes=""
for char in code:
    if (ord(char) + key) % 2 != 0:
        if (ord(char) + key) <= 1114111:
            mes += chr(ord(char) + key)
        else:
            checkpoint = key - (1114111 - ord(char))
            if checkpoint < 1114111:
               mes += chr(checkpoint)
            else:
                mes += chr(checkpoint % 1114111)
    else:
        if ord(char) > key:
           mes += chr(ord(char) - key)
        else:
c
print(f'this is your encrypted messsage-   {mes}')
# decryption
code = str(input('Write your encrypted message, \t'))
key = int(input('Write your encryption key, \t'))
mes=""
for char in code:
    if (ord(char) % 2) == (key % 2):
       if (ord(char) + key) % 2 != 0:
          if (ord(char) + key) <= 1114111:
             mes += chr(ord(char) + key)
          else:
            checkpoint = key - (1114111 - ord(char))
            if checkpoint < 1114111:
               mes += chr(checkpoint)
            else:
                mes += chr(checkpoint % 1114111)
       else:
           if ord(char) > key:
              mes += chr(ord(char) - key)
           else:
               checkpost = (key - ord(char))
               if checkpost < 1114111:
                  mes += chr(checkpost)
               else:
                   mes += chr(key % 1114111)
    else:
       if (ord(char) + key) % 2 == 0:
          if (ord(char) + key) <= 1114111:
             mes += chr(ord(char) + key)
          else:
              checkpoint = key - (1114111 - ord(char))
              if checkpoint < 1114111:
                 mes += chr(checkpoint)
              else:
                  mes += chr(checkpoint % 1114111)
       else:
           if ord(char) > key:
              mes += chr(ord(char) - key)
           else:
               checkpost = (key - ord(char))
               if checkpost < 1114111:
                  mes += chr(checkpost)
               else:
                   mes += chr(key % 1114111)
print(f'this is your decrypted messsage "{mes}"')



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *