implementation of encryption using python

implementation of encryption using python

implementation of encryption using python – You realized that your encryption method is too naive, now you want to change your and make it more sophisticated. You came up an idea that to move each plaintext character k steps forward to become another character. For example, if k = 4, 4 steps forward of the character ‘a’ is ‘e’, and 4 steps forward of the character ‘b’ is ‘f’.


In [ ]:

# Hints
# you can convert "a" to number 1 by thinking a 36-decimal system 
# (we borrow the idea of hexdecimal system). i.e. a=10, b=11, ... z=35

int("a", 36)

Out[ ]:


In [ ]:

# test for "z"
int("z", 36)

Out[ ]:


Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as ‘a’ or ‘@’ or an action of some sort.

Given the following ASCII table:


You can find out more about ASCII code here: ASCIIIn [ ]:


Out[ ]:


In [ ]:

# you can do a conversion from number to character by the following sample code
# In your 36-decimal system, 10 represent and "a", and you know that 97 represent "a" in ASCII system, 
# so you can simply add 87 (97-10) to get your character in your 36-decimal system.

value = int("a", 36)  ## it give your value of 10
value = value - 10    ## it is better to shift your character in a range of 1-25, instead of 10-35
chr(value+97)   ## it should give you back character "a", We only use the ASCII table in the range of 97 to 122.

Out[ ]:


Implement the encryption algorithm and named your function as encrpt2. Take one argument as the input and one argument contains the value k. You only require to handle lowercase character. You NO need to handle special character such as space bar.In [30]:

# implementation of encryption using python

# k = 4 is for testing, your program should work for any value of n

k = 4

def encrypt2(input, k):
    output = ""    
# write your program below:
    for c in input:
        value = int(c, 36)
        value = value - 10
        if value+k > 25:
            value = value-26
        value = value+97+k
        c = chr(value)
        output += c
    return output

In [31]:

# test your function
encrypt2("abcdefghijklmnopqrstuvwxyz", 1)



In [32]:

# test your function
encrypt2("abcdefghijklmnopqrstuvwxyz", 4)



If you have another efficient way to do this, please share in the comment section.

If you have also done the same(implementation of encryption using python) in python or any other language works. Share it in the comments section to help others

keep visiting : shaadcoding



  1. Wonderful post however I was wanting to know if you could write a litte more on this subject?
    I’d be very grateful if you could elaborate a little bit
    further. Appreciate it!

Leave a Reply

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