Python Encryption Cipher
Python Encryption Cipher
Python Encryption Cipher
Usage python python_vigenere_cipher.py textfile.txt key123
Usage python python_vigenere_cipher.py textfile.txt key123
- Code:
#!/usr/bin/env python
#
# Usage python python_vigenere_cipher.py e (or d) textfile.txt key123
#
#
# How it works: Let the two users be denoted sender (S) and receiver (R).
# S and R send each other messages in the form of plaintext (.txt) files.
# S passes R a .txt file encrypted using this program, and R must decrypt
# it using this program. Therefore, only those in possession of this program
# will be able to read the encrypted messages.
# The program uses the Vigenere cipher for encyrption and decryption, a cipher
# which requires a keyword. Thus, S and R must not only share this program,
# but they must also have common knowledge of the keyword.
import os
import sys
if len(sys.argv) < 2:
os.system('clear')
print "Usage Example: python python_vigenere_cipher.py e (or d) textfile.txt key123"
exit()
def encrypt(textfile, key):
'''Encrypts a textfile using the Vigenere cipher with given keyword.
For example, encrypt('goo.txt', 'key') creates an encrypted file named
'goo_encoded.txt' using the Vignere cipher with keyword 'key'. After
encryption is complete, deletes original file'''
plaintext = open(textfile, 'r')
ciphertext = open(textfile.strip('.txt') + '_encrypted.txt', 'w')
msg = plaintext.read()
listChars = []
listKey = []
EncListChars = []
EncMsg = ''
keylist = list(key + '^*&')
for char in msg:
listChars.append(ord(char)-32)
for i in range(len(msg)):
listKey.append(ord(keylist[i % len(keylist)])-32)
for i in range(len(msg)):
EncListChars.append((listChars[i] + listKey[i]) % 96)
for i in range(len(msg)):
EncMsg += chr(EncListChars[i] + 32)
ciphertext.write(EncMsg)
plaintext.close()
ciphertext.close()
os.remove(textfile)
def decrypt(textfile, key):
'''Decrypts an encoded textfile using the Vigenere cipher
with given keyword.'''
ciphertext = open(textfile, 'r')
plaintext = open(textfile.replace('_encrypted', ''), 'w')
EncMsg = ciphertext.read()
EncListChars = []
listKey = []
listChars = []
msg = ''
keylist = list(key + '^*&')
for char in EncMsg:
EncListChars.append(ord(char)-32)
for i in range(len(EncMsg)):
listKey.append(ord(keylist[i % len(keylist)])-32)
for i in range(len(EncMsg)):
listChars.append((EncListChars[i] - listKey[i]) % 96)
for i in range(len(EncMsg)):
msg += chr(listChars[i] + 32)
plaintext.write(msg)
plaintext.close()
ciphertext.close()
if __name__ == '__main__':
if sys.argv[1] == 'e':
encrypt(sys.argv[2], sys.argv[3])
if sys.argv[1] == 'd':
decrypt(sys.argv[2], sys.argv[3])
if sys.argv[1] == 'help':
print 'Usage:'
print 'Encryption: python encryptor.py e plaintext.txt keyword'
print 'Decryption: python encryptor.py d ciphertext.txt keyword'
Similar topics
» CAESAR'S CIPHER For Python
» Securing a Python Menu With Python Encrypted Password File
» Python Bootstrap
» ROT13 In Bash On Linux Code Cipher Example
» This command adds Python 3 as an alternative for the python command
» Securing a Python Menu With Python Encrypted Password File
» Python Bootstrap
» ROT13 In Bash On Linux Code Cipher Example
» This command adds Python 3 as an alternative for the python command
Permissions in this forum:
You cannot reply to topics in this forum
|
|