encryption functions added (may require adaptation)
This commit is contained in:
parent
89de230367
commit
5d826f409b
|
@ -0,0 +1,26 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from cryptography.hazmat.primitives import serialization, hashes
|
||||||
|
from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
||||||
|
|
||||||
|
|
||||||
|
def decryptFile(private_key, cipher_text):
|
||||||
|
plain_text = private_key.decrypt(
|
||||||
|
cipher_text,
|
||||||
|
padding.OAEP(
|
||||||
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
||||||
|
algorithm=hashes.SHA256(),
|
||||||
|
label=None
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return plain_text
|
||||||
|
|
||||||
|
|
||||||
|
def load_private_key(file, paswd=None):
|
||||||
|
with open(file, 'rb') as key_file:
|
||||||
|
private_key = serialization.load_pem_private_key(
|
||||||
|
key_file.read(),
|
||||||
|
password=paswd,
|
||||||
|
)
|
||||||
|
|
||||||
|
return private_key
|
|
@ -0,0 +1,23 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from cryptography.hazmat.primitives import serialization, hashes
|
||||||
|
from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
||||||
|
|
||||||
|
def encryptFile(public_key, text):
|
||||||
|
ciphertext = public_key.encrypt(
|
||||||
|
text,
|
||||||
|
padding.OAEP(
|
||||||
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
||||||
|
algorithm=hashes.SHA256(),
|
||||||
|
label=None
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return ciphertext
|
||||||
|
|
||||||
|
def load_public_key(file):
|
||||||
|
with open(file, 'rb') as key_file:
|
||||||
|
public_key = serialization.load_pem_public_key(
|
||||||
|
key_file.read(),
|
||||||
|
)
|
||||||
|
|
||||||
|
return public_key
|
|
@ -0,0 +1,38 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from cryptography.hazmat.primitives import serialization, hashes
|
||||||
|
from cryptography.hazmat.primitives.asymmetric import rsa, padding
|
||||||
|
|
||||||
|
|
||||||
|
def generate_key_pair(passwd=None):
|
||||||
|
pub_name, priv_name, key_size = sys.argv[1:]
|
||||||
|
|
||||||
|
private_key = rsa.generate_private_key(
|
||||||
|
public_exponent=65537,
|
||||||
|
key_size=int(key_size)
|
||||||
|
)
|
||||||
|
public_key = private_key.public_key()
|
||||||
|
|
||||||
|
with open(pub_name, 'wb') as f:
|
||||||
|
f.write(public_key.public_bytes(
|
||||||
|
encoding=serialization.Encoding.PEM,
|
||||||
|
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||||
|
))
|
||||||
|
|
||||||
|
if not passwd:
|
||||||
|
with open(priv_name, 'wb') as f:
|
||||||
|
f.write(private_key.private_bytes(
|
||||||
|
encoding=serialization.Encoding.PEM,
|
||||||
|
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||||
|
encryption_algorithm=serialization.NoEncryption()
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
with open(priv_name, 'wb') as f:
|
||||||
|
f.write(private_key.private_bytes(
|
||||||
|
encoding=serialization.Encoding.PEM,
|
||||||
|
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||||
|
encryption_algorithm=serialization.BestAvailableEncryption(passwd.encode())
|
||||||
|
))
|
||||||
|
|
||||||
|
return pub_name, priv_name
|
||||||
|
|
Loading…
Reference in New Issue