This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto
executable file
·56 lines (46 loc) · 2.11 KB
/
crypto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3.8
import os, sys
from getpass import getpass
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto import Random
secureRandom = Random.new()
def Main():
if len(sys.argv) != 4: # commands: ./crypto -enc file2encrypt.txt cipher | ./crypto -dec ./cipher plaintext
raise Exception("There is a missing argument(s). Try again")
mode = sys.argv[1] # options: -erc | -dec
input = sys.argv[2] # -enc: it is the file will be encrypted | -dec: it is the generated cipher file
out = sys.argv[3] # -enc: it is the generated cipher file name | -dec: it is the generated plaintext from cipher
password = getpass("Password:") #
if mode == '-enc':
hmac = calcDigest(password)
encryptFile(hmac, input, out)
elif mode == '-dec':
hmac = calcDigest(password)
decryptFile(hmac, input, out)
else:
raise Exception('There is no such mode for this program. You can only encrypt (-enc) or decrypt(-dec) a file')
def encryptFile(key, fileName, outputFile):
with open(fileName , 'rb') as fileWillBeEncrypted:
IV = secureRandom.read(16)
AESBlockCipher = AES.new(key, AES.MODE_CBC, IV)
fileSize = str(os.stat(fileName).st_size).zfill(16)
with open(outputFile, 'wb') as cipher:
cipher.write(fileSize.encode('utf-8'))
cipher.write(IV)
fileInStringFormat = fileWillBeEncrypted.read()
if len(fileInStringFormat) % 16 != 0:
fileInStringFormat += b' ' * (16 - (len(fileInStringFormat) % 16))
cipher.write(AESBlockCipher.encrypt(fileInStringFormat))
def decryptFile(key, filename, outputFile):
with open(filename, 'rb') as cipher:
fileSize = int(cipher.read(16))
IV = cipher.read(16)
AESBlockCipher = AES.new(key, AES.MODE_CBC, IV)
with open(outputFile, 'wb') as plainText:
plainText.write(AESBlockCipher.decrypt(cipher.read()))
def calcDigest(password):
hash = SHA256.new(password.encode('utf-8'))
password = secureRandom.read(16) # deleting the password after generate the digest
return hash.digest()
if __name__ == '__main__': Main()