Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Transposition Cipher (Encrypting)



Instead of replacing characters with other characters, the transposition cipher jumbles up themessage’s symbols into an order that makes the original message unreadable. Before we startwriting code, let’s encrypt the Message “Common sense is not so common.” with pencil and paper. Including the spaces and punctuation, this message has 30 characters. We will use the number 8 for the key.The first step is to draw out a number of Boxes equal to the key. We will draw 8 boxes since our key for this example is 8


The second step is to start writing the message you want to encrypt into the boxes, with one
character for each box. Remember that spaces are a character (this book marks the boxes with (s)
to indicate a space so it doesn’t look like an empty box).

We only have 8 boxes but there are 30 characters in the message. When you run out of boxes,
draw another row of 8 boxes under the first row. Keep creating new rows until you have written
out the full message.

The steps for encrypting are:
1. Count the number of characters in the message and the key.
2. Draw a number of boxes equal to the key in a single row. (For example, 12 boxes for a
key of 12.)
3. Start filling in the boxes from left to right, with one character per box.
4. When you run out of boxes and still have characters left, add another row of boxes


Source Code :-

-------------------------------------------------------------

# Transposition Cipher Encryption
import pyperclip

def main():
myMessage = input("Enter your text to encode" " ")
myKey = int(input("Enter the key"))

ciphertext = encryptMessage(myKey, myMessage)

# Print the encrypted string in ciphertext to the screen, with
# a | (called "pipe" character) after it in case there are spaces at    

# the end of the encrypted message.   
    print(ciphertext + '|')

# Copy the encrypted string in ciphertext to the clipboard.
    pyperclip.copy(ciphertext)


def encryptMessage(key, message):
# Each string in ciphertext represents a column in the grid.
    ciphertext = [''] * key

# Loop through each column in ciphertext.
    for col in range(key):
pointer = col

# Keep looping until pointer goes past the length of the message.
        while pointer len(message):
# Place the character at pointer in message at the end of the            
# current column in the ciphertext list.            
           ciphertext[col] += message[pointer]

# move pointer over
           pointer += key

#Convert the ciphertext list into a single string value and return it.    
       return ''.join(ciphertext)

# If transpositionEncrypt.py is run
#(instead of imported as a module) call
# the main() function.
if __name__ == '__main__':
main()

Result :-



If you have any problem let me know in the comments below or mail me. Do subscribe for more like this . :)




This post first appeared on CODE IT TO RULE IT, please read the originial post: here

Share the post

Transposition Cipher (Encrypting)

×

Subscribe to Code It To Rule It

Get updates delivered right to your inbox!

Thank you for your subscription

×