In: Computer Science
python and mathematical solution
Assume that you design a new affine cipher, where you encrypt three letters at a time, where your alphabet is
{'A':0, 'B':1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7, 'I':8, 'J':9, 'K':10, 'L':11, 'M':12, 'N':13, 'O':14, 'P':15, 'Q':16, 'R':17, 'S':18, 'T':19, 'U':20, 'V':21, 'W':22, 'X':23, 'Y':24, 'Z':25, ' ':26, '.':27, ',': 28, '!': 29, '?':30}.
In other words, you group your plaintext message in trigrams (i.e., three-character words) and encrypt each trigram of the plaintext separately using this affine cipher. For example, if the first three letters of a plaintext is “THE” then it will be encoded as follows
THE -> 19×31×31 + 7×31 + 4 = 18480.
If the number of letters in the plaintext is not a multiple of three, you pad it with the letter “X” at the end. Determine the modulus and the size of the key space.
Source Code:
affine={'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9,'K':10,'L':11,'M':12,'N':13,'O':14,'P':15,
'Q':16,'R':17,'S':18,'T':19,'U':20,'V':21,'W':22,'X':23,'Y':24,'Z':25,'
':26,'.':27,',': 28,'!': 29,'?':30}
plaintext=input("Enter the plaintext(In UPPER CASE):
")
if(len(plaintext)%3!=0):
mul=len(plaintext)%3
plaintext=plaintext+('X'*(3-mul))
lst=[]
for i in range(0,len(plaintext),3):
lst.append(plaintext[i:i+3])
cipher=[]
for trigram in lst:
val=affine[trigram[0]]*31*31 + affine[trigram[1]]*31 +
affine[trigram[2]]
cipher.append(val)
print(cipher)
SAmple input and output: