53 lines
2.0 KiB
Python
Executable File
53 lines
2.0 KiB
Python
Executable File
import base64
|
|
|
|
from mceliece import McEliece
|
|
from ntruencrypt import NTRUEncrypt
|
|
from newhope import NewHope
|
|
|
|
class PostQuantumCiphers:
|
|
def __init__(self, password, salt, algorithm):
|
|
if algorithm == 'mceliece':
|
|
self.algorithm = McEliece
|
|
elif algorithm == 'ntruencrypt':
|
|
self.algorithm = NTRUEncrypt
|
|
elif algorithm == 'newhope':
|
|
self.algorithm = NewHope
|
|
else:
|
|
raise ValueError('Error: invalid algorithm')
|
|
# Derive the key from the password and salt using PBKDF2
|
|
self.key = self.algorithm.derive_key(password, salt)
|
|
|
|
def encrypt(self, data, salt):
|
|
# Encrypt the data using the chosen algorithm
|
|
try:
|
|
encrypted_data = self.algorithm.encrypt(data, self.key)
|
|
except Exception as e:
|
|
raise ValueError(f'Error encrypting data: {e}')
|
|
# Encode the encrypted data as a base64 string
|
|
try:
|
|
ciphertext = base64.b64encode(encrypted_data).decode('utf-8')
|
|
except Exception as e:
|
|
raise ValueError(f'Error encoding ciphertext: {e}')
|
|
return ciphertext
|
|
|
|
def decrypt(self, ciphertext, salt):
|
|
# Decode the base64 ciphertext
|
|
try:
|
|
encrypted_data = base64.b64decode(ciphertext)
|
|
except Exception as e:
|
|
raise ValueError(f'Error decoding ciphertext: {e}')
|
|
# Decrypt the data using the chosen algorithm
|
|
try:
|
|
data = self.algorithm.decrypt(encrypted_data, self.key)
|
|
except Exception as e:
|
|
raise ValueError(f'Error decrypting data: {e}')
|
|
return data
|
|
|
|
def verify(self, data, ciphertext, salt):
|
|
# Decrypt the ciphertext
|
|
try:
|
|
decrypted_data = self.decrypt(ciphertext, salt)
|
|
except Exception as e:
|
|
raise ValueError(f'Error verifying data: {e}')
|
|
# Check if the decrypted data is equal to the original data
|
|
return decrypted_data == data |