Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyDes, sys
- # For Python3, you'll need to use bytes, i.e.:
- # data = b"Please encrypt my data"
- # k = pyDes.des(b"DESCRYPT", pyDes.CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
- def plaintext_from_file():
- f = open("input-des.txt", "r")
- content = f.read()
- f.close()
- print("Plaintext:", content)
- return content
- def key_from_file():
- f = open("key-des.txt", "r")
- content = f.read()
- f.close()
- if len(content) != 8:
- print("Default key: 12345678 (has to be a multiple of 8 bytes)")
- return "12345678"
- print("Key:",content)
- return content
- def ciphertext_to_file(content):
- f = open("cipher-des.txt", "w")
- f.write(str(content))
- f.close()
- def ciphertext_from_file():
- f = open("cipher-des.txt", "r")
- content = f.read()
- f.close()
- return content
- def plaintext_to_file(content):
- f = open("plain-des.txt", "w")
- f.write(str(content))
- f.close()
- if len(sys.argv) != 2:
- print("Usage: python CipherDES.py <e> or <d>")
- exit()
- else:
- data = str.encode(plaintext_from_file())
- key = str.encode(key_from_file())
- k = pyDes.des(key, pyDes.CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
- d = k.encrypt(data)
- if sys.argv[1] == "e":
- d = k.encrypt(data)
- ciphertext_to_file(d)
- print ("Encrypted:", d)
- elif sys.argv[1] == "d":
- plaintext_to_file(k.decrypt(d))
- print ("Decrypted:", k.decrypt(d))
- else:
- print("Usage: <e> for encryption or <d> for decryption")
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement