-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptDecryptPassword.py
More file actions
50 lines (39 loc) · 1.82 KB
/
encryptDecryptPassword.py
File metadata and controls
50 lines (39 loc) · 1.82 KB
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
#!/usr/bin/python
# written by Heiko 11.02.2019
import base64, subprocess
strPassword='8yaThaDCTGHxG8fu'
##########################################################
print ('Password: ' + strPassword)
print ('')
##########################################################
## Using base64
## Function to encrypt password
def encryptPw (strPassword):
return base64.b64encode(strPassword)
# Function to decrypt password
def decryprPw (strPassword):
return base64.b64decode(strPassword)
strEncrypted = encryptPw (strPassword)
print ('Base64 encrypted password: ' + strEncrypted)
strDecrypted = decryprPw (strEncrypted)
print ('Base64 decrypted password: ' + strDecrypted)
print ('')
##########################################################
## Using openssl
# Function to encrypt password
def encryptPwOpenSSL(inputString):
salt = subprocess.check_output(['/usr/bin/openssl', 'rand', '-hex', '8']).rstrip()
passphrase = subprocess.check_output(['/usr/bin/openssl', 'rand', '-hex', '12']).rstrip()
p = subprocess.Popen(['/usr/bin/openssl', 'enc', '-aes256', '-a', '-A', '-S', salt, '-k', passphrase], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
encrypted = p.communicate(inputString)[0]
return encrypted, salt, passphrase
print("Encrypted String: %s" % encrypted)
# Function to decrypt password
def decryptPwOpenSSL(inputString, salt, passphrase):
p = subprocess.Popen(['/usr/bin/openssl', 'enc', '-aes256', '-d', '-a', '-A', '-S', salt, '-k', passphrase], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
return p.communicate(inputString)[0]
arrEncrypt = encryptPwOpenSSL(strPassword)
print('OpenSSL encrypted password: ' + arrEncrypt[0])
print('Salt: %s | Passphrase: %s' % (arrEncrypt[1], arrEncrypt[2]))
strDecypted = decryptPwOpenSSL(arrEncrypt[0], arrEncrypt[1], arrEncrypt[2])
print('OpenSSL encrypted password: ' + strDecypted)