Question

Can i get Playfair Cipher for python 3 that encrypts a message and decrypts it, could...

Can i get Playfair Cipher for python 3 that encrypts a message and decrypts it, could you possibly make it as simple as you can without losing functionality.

please include comments, that would help me better understand

Example of PlayFair Cipher:

https://en.wikipedia.org/wiki/Playfair_cipher

The Playfair cipher uses a 5 by 5 table containing a key word or phrase. Memorization of the keyword and 4 simple rules was all that was required to create the 5 by 5 table and use the cipher.

To generate the key table, one would first fill in the spaces in the table (a modified Polybius square) with the letters of the keyword (dropping any duplicate letters), then fill the remaining spaces with the rest of the letters of the alphabet in order (usually omitting "J" or "Q" to reduce the alphabet to fit; other versions put both "I" and "J" in the same space). The key can be written in the top rows of the table, from left to right, or in some other pattern, such as a spiral beginning in the upper-left-hand corner and ending in the center. The keyword together with the conventions for filling in the 5 by 5 table constitute the cipher key.

To encrypt a message, one would break the message into digrams (groups of 2 letters) such that, for example, "HelloWorld" becomes "HE LL OW OR LD". These digrams will be substituted using the key table. Since encryption requires pairs of letters, messages with an odd number of characters usually append an uncommon letter, such as "X", to complete the final digram. The two letters of the digram are considered opposite corners of a rectangle in the key table. To perform the substitution, apply the following 4 rules, in order, to each pair of letters in the plaintext:

  1. If both letters are the same (or only one letter is left), add an "X" after the first letter. Encrypt the new pair and continue. Some variants of Playfair use "Q" instead of "X", but any letter, itself uncommon as a repeated pair, will do.
  2. If the letters appear on the same row of your table, replace them with the letters to their immediate right respectively (wrapping around to the left side of the row if a letter in the original pair was on the right side of the row).
  3. If the letters appear on the same column of your table, replace them with the letters immediately below respectively (wrapping around to the top side of the column if a letter in the original pair was on the bottom side of the column).
  4. If the letters are not on the same row or column, replace them with the letters on the same row respectively but at the other pair of corners of the rectangle defined by the original pair. The order is important – the first letter of the encrypted pair is the one that lies on the same row as the first letter of the plaintext pair.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

new=input("Enter new key")
new=new.replace(" ", "")
new=new.upper()
def matrix(a,b,initialvalue):
return [[initialvalue for i in range(a)] for j in range(b)]
  
result=list()
for c in new: #storing new key
if c not in result:
if c=='J':
result.append('I')
else:
result.append(c)
flag=0
for i in range(65,91): #storing other character
if chr(i) not in result:
if i==73 and chr(74) not in result:
result.append("I")
flag=1
elif flag==0 and i==73 or i==74:
pass
else:
result.append(chr(i))
k=0
new_matrix=matrix(5,5,0) #initialize matrix
for i in range(0,5): #making matrix
for j in range(0,5):
new_matrix[i][j]=result[k]
k+=1

def locindex(c): #get location of each character
loc=list()
if c=='J':
c='I'
for i ,j in enumerate(new_matrix):
for k,l in enumerate(j):
if c==l:
loc.append(i)
loc.append(k)
return loc
  
def encrypt(): #Encrypt
message=str(input("ENTER MSG:"))
message=message.upper()
message=message.replace(" ", "")   
i=0
for s in range(0,len(message)+1,2):
if s<len(message)-1:
if message[s]==message[s+1]:
message=message[:s+1]+'X'+message[s+1:]
if len(message)%2!=0:
message=message[:]+'X'
print("CIPHER TEXT:",end=' ')
while i<len(message):
loc=list()
loc=locindex(message[i])
loc1=list()
loc1=locindex(message[i+1])
if loc[1]==loc1[1]:
print("{}{}".format(new_matrix[(loc[0]+1)%5][loc[1]],new_matrix[(loc1[0]+1)%5][loc1[1]]),end=' ')
elif loc[0]==loc1[0]:
print("{}{}".format(new_matrix[loc[0]][(loc[1]+1)%5],new_matrix[loc1[0]][(loc1[1]+1)%5]),end=' ')
else:
print("{}{}".format(new_matrix[loc[0]][loc1[1]],new_matrix[loc1[0]][loc[1]]),end=' ')
i=i+2

def decrypt(): #decrypt
message=str(input("ENTER CIPHER TEXT:"))
message=message.upper()
message=message.replace(" ", "")
print("PLAIN TEXT:",end=' ')
i=0
while i<len(message):
loc=list()
loc=locindex(message[i])
loc1=list()
loc1=locindex(message[i+1])
if loc[1]==loc1[1]:
print("{}{}".format(new_matrix[(loc[0]-1)%5][loc[1]],new_matrix[(loc1[0]-1)%5][loc1[1]]),end=' ')
elif loc[0]==loc1[0]:
print("{}{}".format(new_matrix[loc[0]][(loc[1]-1)%5],new_matrix[loc1[0]][(loc1[1]-1)%5]),end=' ')
else:
print("{}{}".format(new_matrix[loc[0]][loc1[1]],new_matrix[loc1[0]][loc[1]]),end=' ')
i=i+2

while(1):
choice=int(input("\n 1.Encrypt \n 2.Decrypt: \n 3.close"))
if choice==1:
encrypt()
elif choice==2:
decrypt()
elif choice==3:
exit()
else:
print("Choose your option")

Add a comment
Know the answer?
Add Answer to:
Can i get Playfair Cipher for python 3 that encrypts a message and decrypts it, could...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1: Encrypt the message “attackhmsfleet” using Playfair Cipher with key =”security”. Use filler “x” and...

    Question 1: Encrypt the message “attackhmsfleet” using Playfair Cipher with key =”security”. Use filler “x” and use “I” for the pair “I/J” in the table. Question 2: Using Vigenere cipher, encrypt the message “sendmoremoney” using the key “cipher”.

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

  • Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers...

    Part 3: Transposition Ciphers #can't use ord or chr functions You must implement three transposition ciphers (the "backwards" cipher, the Rail Fence cipher, and the Column Transposition cipher) where the ciphertext is created via an altered presentation of the plaintext. The algorithm for each is detailed in the function descriptions in this section. (13 points) def backwards_cipher(plaintext, key): • Parameter(s): plaintext ----- a string; the message to be encrypted key ----- an integer; the number to control this cipher •...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher fo...

    using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The cipher illustrated here uses a left shift of three, so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext. For example...

  • Python code. NO importing modules. you may not use: contains(), index(), sum(),join(), split(). def column_cipher(plaintext): •...

    Python code. NO importing modules. you may not use: contains(), index(), sum(),join(), split(). def column_cipher(plaintext): • Parameter(s): plaintext ----- a string; the message to be encrypted • Return value: A string; the ciphertext after applying the column cipher algorithm • Assumptions: o The standard English alphabet is used: "abcdefghijklmnopqrstuvwxyz" o All strings will be non-empty, entirely lowercase, and only contain valid letters in the alphabet. • How it works: o First, transpose the text into 5 different columns by writing...

  • ****************************************************************************************************************8 I want it same as the output and with details please and comments "For two...

    ****************************************************************************************************************8 I want it same as the output and with details please and comments "For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them." - taken from Code Book, The Evolution of Secrecy from Mary, Queen of Scots to Quantum Cryptography by Simon Singh. The idea for this machine problem came from this book.You will encrypt and decrypt some messages using a simplified version of a code in the book. The...

  • C++ Code

    "For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them." - taken from Code Book, The Evolution of Secrecy from Mary, Queen of Scots to Quantum Cryptography by Simon Singh.The idea for this machine problem came from this book.You will encrypt and decrypt some messages using a simplified version of a code in the book. The convention in cryptography is to write the plain text in lower case letters and the encrypted text in upper case...

  • Need help with number 3 the last one Need help with number 3 I have already...

    Need help with number 3 the last one Need help with number 3 I have already given the whole question MATH 1030 – Application Assignment 3 Cryptography Due: Thursday, June 4, 2020 at 11:59pm Atlantic time (submit through Brightspace) You must show your work for full marks. The goal of this assignment is to use our knowledge of linear algebra to do cryptography. We will encrypt a plaintext using a cipher where the resulting ciphertext should not be legible unless...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT