-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathChacha20Poly1305StringEncryption.py
68 lines (57 loc) · 2.47 KB
/
Chacha20Poly1305StringEncryption.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
from Crypto.Cipher import ChaCha20_Poly1305
from Crypto.Random import get_random_bytes
import base64
def base64Encoding(input):
dataBase64 = base64.b64encode(input)
dataBase64P = dataBase64.decode("UTF-8")
return dataBase64P
def base64Decoding(input):
return base64.decodebytes(input.encode("ascii"))
def generateRandomAesKey():
return get_random_bytes(32)
def generateRandomNonce():
return get_random_bytes(12)
def chacha20Poly1305EncryptToBase64(encryptionKey, plaintext):
nonce = generateRandomNonce()
aad = b""
cipher = ChaCha20_Poly1305.new(key=encryptionKey, nonce=nonce)
cipher.update(aad)
ciphertext, tag = cipher.encrypt_and_digest(plaintext.encode("ascii"))
nonceBase64 = base64Encoding(nonce)
ciphertextBase64 = base64Encoding(ciphertext)
poly1305TagBase64 = base64Encoding(tag)
return nonceBase64 + ":" + ciphertextBase64 + ":" + poly1305TagBase64
def chacha20Poly1305DecryptFromBase64(decryptionKey, ciphertextDecryptionBase64):
data = ciphertextDecryptionBase64.split(":")
nonce = base64Decoding(data[0])
ciphertext = base64Decoding(data[1])
poly1305Tag = base64Decoding(data[2])
aad = b""
decryptedtext = ""
try:
cipher = ChaCha20_Poly1305.new(key=decryptionKey, nonce=nonce)
cipher.update(aad)
decryptedtext = cipher.decrypt_and_verify(ciphertext, poly1305Tag)
return decryptedtext.decode("UTF-8")
except ValueError:
return ""
return ""
print("ChaCha20-Poly1305 String encryption with random key full")
plaintext = "The quick brown fox jumps over the lazy dog"
print("plaintext: " + plaintext)
encryptionKey = generateRandomAesKey()
encryptionKeyBase64 = base64Encoding(encryptionKey)
print("encryptionKey (Base64): " + encryptionKeyBase64)
print("\n* * * Encryption * * *")
ciphertextBase64 = chacha2020Poly1305EncryptToBase64(encryptionKey, plaintext)
print("ciphertext: " + ciphertextBase64)
print("output is (Base64) nonce : (Base64) ciphertext : (Base64) poly1305Tag")
print("\n* * * Decryption * * *")
ciphertextDecryptionBase64 = ciphertextBase64
decryptionKeyBase64 = encryptionKeyBase64
decryptionKey = base64Decoding(decryptionKeyBase64)
print("decryptionKey (Base64): " + decryptionKeyBase64)
print("ciphertext (Base64): " + ciphertextDecryptionBase64)
print("input is (Base64) nonce : (Base64) ciphertext : (Base64) poly1305Tag")
decryptedtext = chacha2020Poly1305DecryptFromBase64(decryptionKey, ciphertextBase64)
print("plaintext: " + decryptedtext)