Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the picture is an example my professor told us to use in a program we are mak

ID: 3677165 • Letter: I

Question

In the picture is an example my professor told us to use in a program we are making but gave us no solution to it and I have no clue how to this. I looked up examples online but most are over my head in terms of complexity.


mput text according to this cipher. Exercise P9.5. Playfair cipher. Another way of thwarting a simple letter frequency analysis of an encrypted text is to encrypt pairs of letters together. A simple scheme to do this is the Playfair cipher. You pick a keyword and remove duplicate letters from it. Then you fill the keyword, and the remaining letters of the alphabet, into a 5 x 5 square. (Since there are only 25 squares, I and J are considered the same letter.) Here is such an arrangement with the keyword PLAYFAIR. PLAYF IRBCD EGHK M NO Q ST To encrypt a letter pair, say AM, look at the rectangle with corners A and M: PLAY F IR B CD E GHKM The encoding of this pair is formed by looking at the other two corners of the rect- angle, in this case, FH. If both letters happen to be in the same row or column, such as co, simply swap the two letters. Decryption is done in the same way. Write a program that encrypts or decrypts an input text according to this cipher.

Explanation / Answer

Copy the code in playfair.py file.

To encrypt a string: Store the string in a file named as "infile". And use the following command.

# python playfair.py < infile

to decrypt:

#python playfair -d < infile

import re, sys, random

# use alphabet with no j
alph    = "abcdefghiklmnopqrstuvwxyz"
filler = "qxz"

def randFill():
return filler[random.randint(0,len(filler)-1)]

# lowercase alphabetic
def lowercasenoj(ln):
txt = ""
for ch in ln:
    if ch.isalpha():
      txt += ch.lower()
txt = re.sub('j','i',txt) # j--> i
return txt

# use filler to separate doublet digrams, e.g. oo --> oqo
# use filler if last digram is incomplete, e.g. th en d --> th en dx
def preprocess(ln):
txt = ""
state = 0
for ch in ln:
    if state == 0:
      prevCh = ch
      state = 1-state # change state
    else: # state == 1
      if ch==prevCh:
        txt += prevCh + randFill()
      else: # ch!=prevCh
        txt += prevCh + ch
   state = 1-state
if state == 1: #ended with an incomplete digram
    txt += prevCh + randFill()
return txt + ' '

def shifthorizontal(rowcol,j):
return rowcol[0], (rowcol[1]+j)%5

def shiftvertical(rowcol, j):
return (rowcol[0]-j)%5, rowcol[1]

def psn(rowcol):
return 5*rowcol[0] + rowcol[1]

def encipher(cipAlph,dgrm,encrypt):
idx = divmod(cipAlph.find(dgrm[0]),5), divmod(cipAlph.find(dgrm[1]),5),
txt = ""
for j in range(2):
    if idx[0][0] == idx[1][0]: # same row
      if encrypt:
        txt += cipAlph[ psn(shifthorizontal(idx[j], 1)) ]
      else:
        txt += cipAlph[ psn(shifthorizontal(idx[j], -1)) ]
    elif idx[0][1] == idx[1][1]: # same col
      if encrypt:
        txt += cipAlph[ psn(shiftvertical(idx[j], -1)) ]
      else:
        txt += cipAlph[ psn(shiftvertical(idx[j], 1)) ]
    else: # switch corners of rectangle
      new = idx[j][0], idx[1-j][1]
      txt += cipAlph[psn(new)] # same row, other col
return txt

# main block
cipherAlphabet = sys.stdin.readline()
print cipherAlphabet,
if len(sys.argv)==1: # python playfair.py < infile
for line in sys.stdin:
    txt = preprocess(lowercasenoj(line))
    ctxt = ""
    for j in range(len(txt)/2):
      ctxt += encipher(cipherAlphabet,txt[2*j]+txt[2*j+1], True)
    ctxt += ' '
    print ctxt,
elif sys.argv[1]=="-d": # decrypt
for line in sys.stdin:
    ptxt = ""
    for j in range(len(line)/2):
      ptxt += encipher(cipherAlphabet,line[2*j]+line[2*j+1], False)
    ptxt += ' '
    print ptxt,
else:
print(" usage: python playfair.py [-d] < infile")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote