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=35int("a", 36)

Out[ ]:

10

In [ ]:

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

Out[ ]:

35

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:

Dec | Chr | Dec | Chr | Dec | Chr |
---|---|---|---|---|---|

97 | a | 106 | j | 115 | s |

98 | b | 107 | k | 116 | t |

99 | c | 108 | l | 117 | u |

100 | d | 109 | m | 118 | v |

101 | e | 110 | n | 119 | w |

102 | f | 111 | o | 120 | x |

103 | g | 112 | p | 121 | y |

104 | h | 113 | q | 122 | z |

105 | i | 114 | r | – | – |

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

chr(97)

Out[ ]:

'a'

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

Out[ ]:

'a'

**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 nk=4defencrypt2(input, k): output=""# write your program below:forcininput: value=int(c, 36) value=value-10ifvalue+k>25: value=value-26 value=value+97+k c=chr(value) output+=creturnoutput

In [31]:

# test your functionencrypt2("abcdefghijklmnopqrstuvwxyz", 1)

Out[31]:

'bcdefghijklmnopqrstuvwxyza'

In [32]:

# test your functionencrypt2("abcdefghijklmnopqrstuvwxyz", 4)

Out[32]:

'efghijklmnopqrstuvwxyzabcd'

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

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!

Thanks, will try my level best to overcome the issues in upcoming posts as well as already on site