Advertisement
chrisCNG

Vcipher1

Nov 9th, 2021
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  1. # A polyalphabetic (Vigenere) cipher encryption and decryption program
  2. # Based on a single keyword and a modified alphabet
  3. # messages to be encrypted must be lower case letters only and not contain spaces,
  4. # numbers, punctuation, or symbols
  5.  
  6. # variables
  7. alphabet0 = "kryptosabcdefghijlmnquvwxz"# The alphabet variant
  8. keyWord = ""
  9. plainText = ""
  10. alphaLength = len(alphabet0)
  11.  
  12. # functions
  13. # Set up the alphabet grid
  14. def matAlpha(m, n):
  15.     global gridAlpha
  16.     gridAlpha = []  # define empty matrix
  17.     # n rows
  18.     # m cols
  19.     for i in range(n):  # total row is n
  20.         row = []
  21.         for j in range(m):  # total column is m
  22.             row.append("")  # adding "" null string for each column for this row
  23.         gridAlpha.append(row)  # add fully defined column into the row
  24.  
  25. # Fill alphabet grid with 26 shifted alphabets
  26. def FillAlpha(aL):
  27.     global gridAlpha
  28.     stoLine = 0
  29.     for alphab in range(aL):
  30.         for letter in range(aL):
  31.             gridAlpha[alphab][letter] = alphabet0[((letter + alphab)%aL)]
  32.  
  33. def Encrypt():
  34.     global gridAlpha, plainText, keyWord, alphabet0, cryptOut
  35.     plainText = input('Enter the message: ')
  36.     keyWord = input('Enter the keyword: ')
  37.     # variables for Encyption
  38.     cryptOut = ""
  39.     crypText = []
  40.     textLength = len(plainText)
  41.     keyLength = len(keyWord)
  42.     # loop through the plain text and encrypt
  43.     for x in range(textLength):
  44.         letterX = plainText[x]
  45.         keyNum = x%keyLength
  46.         keyLetter = keyWord[keyNum]
  47.         alphaNum = alphabet0.find(keyLetter)
  48.         letterNum = alphabet0.find(letterX)
  49.         cryptLetter = gridAlpha[alphaNum][letterNum]
  50.         crypText.append(cryptLetter)
  51.  
  52.     cryptLength = len(crypText)
  53.     for y in range(cryptLength):
  54.         cryptOut += crypText[y]
  55.  
  56.     print(cryptOut) #print the ciphertext
  57.     # save as a text file
  58.     text_file = open("cryptOut.txt", "w")
  59.     n = text_file.write(cryptOut)
  60.     text_file.close()
  61.  
  62. def Decrypt():
  63.     global gridAlpha, keyWord, alphabet0, cryptIn, plainOut
  64.     cryptIn = input('Enter the ciphertext: ')
  65.     keyWord = input('Enter the keyword: ')
  66.     # variables for decryption
  67.     plainOut = ""
  68.     plainText = []
  69.     cryptLength = len(cryptIn)
  70.     keyLength = len(keyWord)
  71.     # loop through the plain text and encrypt
  72.     for x in range(cryptLength):
  73.         letterX = cryptIn[x]
  74.         keyNum = x%keyLength
  75.         keyLetter = keyWord[keyNum]
  76.         alphaNum = alphabet0.find(keyLetter)
  77.         letterNum = gridAlpha[alphaNum].index(letterX)
  78.         plainLetter = alphabet0[letterNum]
  79.         plainText.append(plainLetter)
  80.     plainLength = len(plainText)
  81.     for y in range(plainLength):
  82.         plainOut += plainText[y]
  83.  
  84.     print(plainOut) #print the plaintext
  85.  
  86. def DecryptFile():
  87.     global gridAlpha, keyWord, alphabet0, cryptIn, plainOut
  88.     getCryptext()
  89.     keyWord = input('Enter the keyword: ')
  90.     # variables for decryption
  91.     plainOut = ""
  92.     plainText = []
  93.     cryptLength = len(cryptIn)
  94.     keyLength = len(keyWord)
  95.     # loop through the plain text and encrypt
  96.     for x in range(cryptLength):
  97.         letterX = cryptIn[x]
  98.         keyNum = x%keyLength
  99.         keyLetter = keyWord[keyNum]
  100.         alphaNum = alphabet0.find(keyLetter)
  101.         letterNum = gridAlpha[alphaNum].index(letterX)
  102.         plainLetter = alphabet0[letterNum]
  103.         plainText.append(plainLetter)
  104.     plainLength = len(plainText)
  105.     for y in range(plainLength):
  106.         plainOut += plainText[y]
  107.  
  108.     print(plainOut) #print the plaintext
  109.  
  110. def getCryptext():
  111.     global cryptIn
  112.     with open('cryptOut.txt', 'r') as file:
  113.         crypt = file.read().rstrip()
  114.         cryptIn = crypt.casefold()
  115.  
  116.  
  117. #example using alphabet0 = kryptosabcdefghijlmnquvwxz and keyword = palimpsest
  118. # message
  119. #betweensubtleshadingandtheabsenceoflightliesthenuanceofiqlusion
  120. # resulting ciphertext
  121. #emufphzlrfaxyusdjkzldkrnshgnfivjyqtquxqbqvyuvlltrevjyqtmkyrdmfd
  122.  
  123. #main section
  124. matAlpha(alphaLength, alphaLength)
  125. FillAlpha(alphaLength)
  126.  
  127. #inputs
  128. action = input("Enter e to encrypt a message, d to decode a ciphertext, f to decode a .txt file: ")
  129. if action == "e":
  130.     Encrypt()
  131. elif action == "d":
  132.     Decrypt()
  133. elif action == "f":
  134.     DecryptFile()
  135. else:
  136.     print("Action invalid")
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement