![Implement Monoalphabetic Cipher Encryption And Decryption In Python Implement Monoalphabetic Cipher Encryption And Decryption In Python](https://image.slidesharecdn.com/1344364890000basic-encryption-and-decryption-160217193917/95/basic-encryption-and-decryption-22-638.jpg)
- #IMPLEMENT MONOALPHABETIC CIPHER ENCRYPTION AND DECRYPTION IN PYTHON CODE#
- #IMPLEMENT MONOALPHABETIC CIPHER ENCRYPTION AND DECRYPTION IN PYTHON ZIP#
Return ''.join(caesar_shift(tc, ord(kc)-96) for tc, kc in zip(text, key)) Let's also apply the advice from the previous part and write it as a function. Print(chr(ord(text_char) + ord(key_char) - 96)) for text_char, key_char in zip(text, key):
#IMPLEMENT MONOALPHABETIC CIPHER ENCRYPTION AND DECRYPTION IN PYTHON ZIP#
Print(chr(ord(text) + ord(key) - 96))Īn even better, more idiomatic way to loop over multiple sequences (strings, lists etc.) is with the built-in zip function, which creates a tuple of elements for every loop iteration. for i in range(min(len(text), len(key))): It could be written as follows to avoid that particular issue (using min in case the lengths are different). Your loop here is somewhat confusing, in that the actual loop variable i goes unused while an additional variable j is used as the index. With the character substitution logic now being too long to fit comfortably in one line, I chose to separate it into a nested function. Return ''.join(substitute(char) for char in text) from string import ascii_lowercaseĬhar = chr((char_num + places) % 26 + 97) To avoid unintended results on different inputs, one option is to leave other characters unmodified.Īlso, "wrap around" instead of substituting for characters outside of the alphabet, by using the modulus operator %. The technique appears to be meant to only work on (English) letters. A value of -5 then decrypts the resulting string. The number of places to shift is a parameter instead of hardcoded, with a default of 5 to work the same as your example when the argument is omitted.
![Implement Monoalphabetic Cipher Encryption And Decryption In Python Implement Monoalphabetic Cipher Encryption And Decryption In Python](https://img.yumpu.com/19717536/1/500x640/coding-and-cryptography-school-of-mathematical-sciences-.jpg)
Return ''.join(chr(ord(char) + places) for char in text) It also lets us use the similarity between encryption and decryption to reduce repetition in the code. Rather than forcing printing text to screen, a function returning a string can be used in any other context as well. Separate the encryption/decryption into a function The variable name i is associated with integer indexes and is misleading to use for a character. ''.join(chr(ord(char) + 5) for char in text) Instead of printing one character at a time (inefficient), create a string using the str.join method. Print(chr(ord(encrypt)+ ord(codeword)-96)) codeword = input('Enter codeword : ')Įncrypt = input('Enter text to encrypt : ') The output of having my codeword as 'abc' and the word to encrypt and 'bcd' would be 'ceg'. Then: (a + b = 1 + 2) and this is repeated for each letter in the two words. If I had 'abc' for the codeword and 'bcd' for the text to encrypt.
#IMPLEMENT MONOALPHABETIC CIPHER ENCRYPTION AND DECRYPTION IN PYTHON CODE#
To decrypt, I do the reverse, I move each letter 5 letters down the alphabet.īelow is my second piece of code that encrypts text against a code word. The output of this code if I enter 'hello' would be 'mjqqt'. encrypt = input('Enter text to encrypt : ')Įncrypt = encrypt.lower().replace(" ", "")ĭecrypt = input('Enter text to decrypt : ')ĭecrypt = decrypt.lower().replace(" ", "") My first piece of code encrypts text by moving each letter in the string 5 letters across in the alphabet.