-
Notifications
You must be signed in to change notification settings - Fork 2
/
mycrypto.py
84 lines (64 loc) · 2.44 KB
/
mycrypto.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import hashlib
from Crypto.PublicKey import RSA
from base64 import b64encode, b64decode
from Crypto.Cipher import PKCS1_OAEP, AES
#: Generate a pair of encrypted privateKey and a publicKey
def generateKeys(passphrase):
privateKey = RSA.generate(1024)
publicKey = privateKey.publickey().exportKey().decode()
#! Encrypting privateKey
aes = AESCipher(passphrase)
encryptedPrivateKey = aes.encrypt(privateKey.exportKey().decode())
return encryptedPrivateKey, publicKey
#: Encryption with RSA using publicKey
def encrypt(text, publicKey):
publicKey = RSA.importKey(publicKey)
cipher = PKCS1_OAEP.new(key=publicKey)
text = text.encode()
#! Encrypt 85 characters at once and separate the cipher text with commas
chunkSize = 85
chunkedTexts = [ text[i:i+chunkSize] for i in range(0, len(text), chunkSize) ]
cipherText = ''
for i in chunkedTexts:
cipherText += ',' if cipherText else ''
cipherText += cipher.encrypt(i).hex()
return cipherText
#: Decryption with RSA using privateKey
def decrypt(cipherText, privateKey, passphrase):
#! Decrypting privateKey
aes = AESCipher(passphrase)
decryptedPrivateKey = aes.decrypt(privateKey)
privateKey = RSA.importKey(decryptedPrivateKey)
decrypt = PKCS1_OAEP.new(key=privateKey)
#! Split the cipherText with comma to deprypt them individually and concatenate them together
text = ''
for i in cipherText.split(','):
cT = bytes.fromhex(i)
text+= decrypt.decrypt(cT).decode()
return text
#: Encryption and Decryption with AES-128-CBC
class AESCipher():
def __init__(self, key):
self.key = key.encode()
self.bs = AES.block_size
def encrypt(self, raw):
raw = self._pad(raw)
#! zero based byte[16]
iv = b'\0'*16
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return b64encode(cipher.encrypt(raw.encode())).decode('UTF-8')
def decrypt(self, enc):
enc = b64decode(enc)
#! zero based byte[16]
iv = b'\0'*16
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return self._unpad(cipher.decrypt(enc)).decode('utf-8')
def _pad(self, s):
return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
#: Generate SHA Hash of a string
def genHash(string):
result = hashlib.sha512(str(string).encode())
return result.hexdigest()