<jupyter_start><jupyter_text>Name: <NAME>

Roll No. C022

Program/Semester Btech EXTC SEM 8

Course: DENSE

Expt Title-  To implement Cryptanalysis on traditional cipher<jupyter_code>#Library intsallation
!pip install pyenchant 
# Library installation
!apt install -qq enchant
import string
all_letters= string.ascii_letters 
print(all_letters)
print(len(all_letters))
class Substitution_Cypher:
  #Constructor for intializing the encryption and decryption key which is same here
  def __init__(self,key=3):
    self.key=key

  #This instance  function will read the file line by line and return it in string format 
  def read_plain_txt(self,path):
    plain_txt=''
    file_en = open(path,"r") 
    for char in file_en.readlines():
      plain_txt=plain_txt+char
      return plain_txt
  
  """This will encrypt the file when the user will pass the key
  unlike the original approach where first the corresponding cyphers of each alphabet was stored in the 
  dictionary  and then asigned to plain text in diff loop.Now it is done in a single step
  by extracting the index of every character of plaintext in (all letters) using .index()
  and directly replacing that index by  the new index """
  def encrypt(self,plain_txt,key=3):
    cipher = ''
    for i in plain_txt:
      if i in all_letters:
        cipher+=all_letters[(all_letters.index(i)+key)%len(all_letters)]
      else:
        cipher+=i
    return cipher

  def decrypt(self,cipher,key=3):
    decipher = ''
    for i in cipher:
      if i in all_letters:
        decipher+=all_letters[(all_letters.index(i)-key)%len(all_letters)]
      else:
        decipher+=i
    return decipher
  
  def brute_force_attack(self,cipher):
    from enchant.checker import SpellChecker
   
    for key in range(len(all_letters)):
      decipher=self.decrypt(cipher,key)
      #print(decipher)
      #cracked=decipher.split(' ')
      #out = map(lambda x:x.lower(),cracked)
      #out=list(out)
      d = SpellChecker("en_US")
      d.set_text(decipher)
      errors = [err.word for err in d]
      if len(errors) is 0:
        print('Key found after attack',key,all_letters[key])
        break

if __name__ == "__main__":
    s=Substitution_Cypher(3) #by default the key is going to be three if user does not pass another value
    plain_txt=s.read_plain_txt('/content/SecretMission.txt')
    print('Plaintext\n',plain_txt)
    print("\n")
    cipher=s.encrypt(plain_txt)
    print('Cipher text:\n',cipher)
    s.brute_force_attack(cipher)
    #print(key
class Transposition_Cypher:
  def encrypt(self,plain_txt,key):
    cipher=''
    for i in range(len(plain_txt)):
      cipher += plain_txt[(i-key)%len(plain_txt)]
    return cipher
    

  def decrypt(self,plain_txt,cipher,key):
    decipher=''
    for i in range(len(plain_txt)):
      decipher += cipher[(i+key)%len(plain_txt)]
    return decipher
  
  def brute_force_attack(self,cipher):
    from enchant.checker import SpellChecker
   
    for key in range(len(all_letters)):
      decipher=self.decrypt(plain_txt,cipher,key)
      d = SpellChecker("en_US")
      d.set_text(decipher)
      errors = [err.word for err in d]
      if len(errors) is 0:
        print('Key found after attack',key,all_letters[key])
        break
 
if __name__ =="__main__":
  t=Transposition_Cypher()
  plain_txt = input('Enter plain text: ')
  print('Plain_txt:\n',plain_txt)
  cipher=t.encrypt(plain_txt,2)
  print('Cipher\n',cipher)
  decipher=t.decrypt(plain_txt,cipher,2)
  print('Decipher\n',decipher)
  t.brute_force_attack(cipher)<jupyter_output>Enter plain text: Sunday
Plain_txt:
 Sunday
Cipher
 aySund
Decipher
 Sunday
Key found after attack 2 c
{'fraction_non_alphanumeric': 0.09746328437917223, 'fraction_numerical': 0.003471295060080107, 'mean_word_length': 3.2713797035347776, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '391262', 'license': 'MIT', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1213, 'n_words': 326, 'path': 'EXPT2.ipynb', 'repo_name': 'kapilmayank/Data-encryption-and-network-Security', 'size': '3751'}