DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on

Simple way to encrypt informations

Simple ways to encrypt information

A simple way of encrypting a message is to rearrange its characters. One way to rearrange the characters is to pick out the characters at even indices, put them first in the encrypted string, and follow them by the odd characters. For example, the string message would be encrypted as msaeesg because the even characters are m, s, a, e (at indices 0, 2, 4, and 6) and the odd
characters are e, s, g (at indices 1, 3, and 5).
Now let's write a program that asks the user for a string and uses this method to encrypt the string.
our encrypt code :

def encrypt():#to put all our code in function
    strings=input('what is your message: ')
    even=strings[::2]#extract the even part 
    odd=strings[1::2]#extract the odd part
    print(even+odd)#print the result
encrypt() 
Enter fullscreen mode Exit fullscreen mode

You can try to input anything and it will be hard for anyone to read.
Here are some different input and there encrypt message

>>>what is your message: kill the readers
>>>kl h edrilteraes

>>>what is your message: I am the one who encrypt the information. so don't bother to try you can't read it
>>>Ia h n h nrp h nomto.s o' ohrt r o a' edi mteoewoecytteifrain odntbte otyyucntra t
Enter fullscreen mode Exit fullscreen mode

But what if we need to get the real message back. what do we do? Yeah, we do something called decryption and our decryption code will be

def decrypt():
    string=input('what is your message: ')
    length=len(string)#to get the length of the input
    half_length=(length+1)//2 #half of the input
    even=string[:half_length]#even part
    odd=string[half_length:]#odd part
#here we start inserting each of the part to form original
    msg=''
    if length%2==0:
        for i in range (half_length):
            join=even[i]+odd[i]
            msg=msg+join
    else:
        for i in range (1,half_length+1):
            start=i-1
            join=even[start:i]+odd[start:i]
            msg=msg+join
    print(msg)
decrypt()
Enter fullscreen mode Exit fullscreen mode

Now you can copy the result you get in encrypt here and you will get the real message.
Here are some

>>>what is your message:kl h edrilteraes 
>>>kill the readers
>>>what is your message: Ia h n h nrp h nomto.s o' ohrt r o a' edi mteoewoecytteifrain odntbte otyyucntra t
>>> I am the one who encrypt the information. so don't bother to try you can't read it
Enter fullscreen mode Exit fullscreen mode

find it interesting? then consider to subscribe and like.

Top comments (0)