Practical 4 & 5 CNS
Practical 4 & 5 CNS
Practical 4 & 5 CNS
for i in range(len(plain_text)):
if plain_text[i].isalpha():
shift = ord(key_repeated[i].upper()) - ord('A')
if plain_text[i].isupper():
encrypted_text += chr((ord(plain_text[i]) + shift - ord('A')) % 26 +
ord('A'))
else:
encrypted_text += chr((ord(plain_text[i]) + shift - ord('a')) % 26 +
ord('a'))
else:
encrypted_text += plain_text[i]
return encrypted_text
Rules
1. Divide Plain text into pairs of letters. Example: BA|LL|OO|N
2. If a pair contains repeated letters, we can use a filler letter such as “X” and rearrange the letters.
Example: BA|LX|LO|ON
3. When a letter is left alone, we can add “x” in the end. Example: BUT -> BU|TX 4. If two letters are
in the same row, replace them with the immediate right.
5. If two letters are in the same column, replace them with immediate below.
6. If two letters are not in the same row or column, we draw a rectangle enclosed with those letters. In
this case direction will be matter to choose the letter as cipher letter.
7. If two letters are in the same row, but there is no letter to the right, we return to the first letter from
the left in circular way.
8. If two letters are in the same column, but there is no letter immediate below, we return to the first
letter from the column in circular way.
EXAMPLE:
OUTPUT:
PLAY-FAIR KEYWORD MATRIX:
C E I T D
P A R M N
B F G H K
L O Q S U
V W X Y Z
ENCRYPTION:
PLAIN-TEXT ARRANGMENT: BA LX LO ON SX
CIPHER-TEXT: FPQVOQUAQY
DECRYPTION:
CIPHER-TEXT: FPQVOQUAQY
PLAIN-TEXT: BALXLOONSX --> Remove ‘X’ So, Message is: BALLOONS