Last active
          May 20, 2021 16:05 
        
      - 
      
 - 
        
Save jatinsharrma/f85c37531851883244d55618b7f453ef to your computer and use it in GitHub Desktop.  
    Caesar Cipher : Python
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #--------------------------------------- | |
| #-----------Caesar Cipher--------------- | |
| #--------------------------------------- | |
| # Ceaser Cipher class | |
| class CaesarCipher: | |
| # this method encrypt the plain text | |
| @staticmethod | |
| def encrypt(string,key): | |
| result = "" | |
| for char in string: | |
| if char == " ": | |
| result += " " | |
| continue | |
| if ord(char) > 96 : | |
| result += chr((ord(char) + key - 97) % 26 + 97) | |
| else: | |
| result += chr((ord(char)+ key - 65) % 26 +65) | |
| # acsii_value = ord(char) | |
| # temp = acsii_value - 97 if acsii_value > 96 else acsii_value - 65 | |
| # new_ascii = (temp + key) % 26 | |
| # new_char = chr(new_ascii+97) if acsii_value > 96 else chr(new_ascii+65) | |
| # result += new_char | |
| return result | |
| # this method decrypt cipher text | |
| @staticmethod | |
| def decrypt(string,key): | |
| result = "" | |
| for char in string: | |
| if char == " ": | |
| result += " " | |
| continue | |
| if ord(char) > 96 : | |
| result += chr((ord(char) - key - 97) % 26 + 97) | |
| else: | |
| result += chr((ord(char) - key -65) % 65 +65) | |
| # acsii_value = ord(char) | |
| # temp = acsii_value - 97 if acsii_value > 96 else acsii_value - 65 | |
| # new_ascii = (temp - key) % 26 | |
| # new_char = chr(new_ascii+97) if acsii_value > 96 else chr(new_ascii+65) | |
| # result += new_char | |
| return result | |
| #----Testing------- | |
| if __name__ == "__main__": | |
| String = "Hey i am a plain text" | |
| key = 3 | |
| encrypted = CaesarCipher.encrypt(String,key) | |
| decrypted = CaesarCipher.decrypt(encrypted,key) | |
| print("Plain Text : ", String) | |
| print("Encrypted : ", encrypted) | |
| print("Decrypted : ", decrypted) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
____ / \ / \ /| |_/ || || || || /|_/||-/ | || _/____________________________ / \ || |_____________________________________/