My goal is to write a code in Python that do the following: 1.Generate a public key. 2. Hash the public key. 3. Generate a random string. 4. Encrypt & decrypt the random string with the hashed public key.
This is the code I written:
from Crypto.PublicKey import RSA
import random
import string
import hashlib
import base64
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
def encrypt(plaintext, password):
f = Fernet(base64.urlsafe_b64encode(
PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=b'abcd', iterations=1000,
backend=default_backend()).derive(password.encode())))
return f.encrypt(plaintext.encode()).decode()
def decrypt(ciphertext, password):
f = Fernet(base64.urlsafe_b64encode(
PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=b'abcd', iterations=1000,
backend=default_backend()).derive(password.encode())))
return f.decrypt(ciphertext.encode()).decode()
def randomString(strlength = 16): #Random code
letters = string.ascii_letters
return ''.join(random.choice(letters) for i in range(strlength))
key = RSA.generate(2048) # Private key creation
code = 'nooneknows'
privatekey = key.exportKey(passphrase=code, pkcs=8)
publickey = key.publickey().exportKey()
result = hashlib.md5(publickey) #Hashing the Publickey
publickey = result.digest()
Nonce=randomString() # Creating Nonce
encrypt((str(Nonce)), password=(str(publickey))) # Encrypting nonce with hashed pub.key
decrypt((str(encrypt)), password=(str(publickey)))
print("This is the decrption", encrypt)
print("This is the decrption", decrypt)
When I run it, I get the error:
D:\Anaconda3\python.exe C:/Users/AVIV/.PyCharmCE2019.1/config/scratches/test.py
Traceback (most recent call last):
File "D:\Anaconda3\lib\site-packages\cryptography\fernet.py", line 87, in _get_unverified_token_data
data = base64.urlsafe_b64decode(token)
File "D:\Anaconda3\lib\base64.py", line 133, in urlsafe_b64decode
return b64decode(s)
File "D:\Anaconda3\lib\base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/AVIV/.PyCharmCE2019.1/config/scratches/test.py", line 39, in <module>
decrypt((str(encrypt)), password=(str(publickey)))
File "C:/Users/AVIV/.PyCharmCE2019.1/config/scratches/test.py", line 21, in decrypt
return f.decrypt(ciphertext.encode()).decode()
File "D:\Anaconda3\lib\site-packages\cryptography\fernet.py", line 74, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "D:\Anaconda3\lib\site-packages\cryptography\fernet.py", line 89, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
Is there a way to solve this error? My guess, is that the issue is with the decoding and encoding to/from bytes. I tried to decode/encode it many times, but always ended up with an error. Also, I guess that it has an issue with the padding, but I couldn't figured how to solve it. I was thinking, maybe the Fernet encryption isn't the right fit for the goals of my project, maybe I should use other encryption/library?