From c4450ea83746114c4e12b5f5dd891ba893c29dcb Mon Sep 17 00:00:00 2001 From: Votre Nom Date: Wed, 6 Nov 2019 14:29:01 +0100 Subject: [PATCH] retreived keys_manager_1.py --- crypto_v1.zip | Bin 1199 -> 0 bytes keys_manager_1.py | 137 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) delete mode 100644 crypto_v1.zip create mode 100644 keys_manager_1.py diff --git a/crypto_v1.zip b/crypto_v1.zip deleted file mode 100644 index fc2f0c85f70e6053a825ebc80b1b9b29bf721aa4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1199 zcmWIWW@Zs#U|`^2i0uvak3YVaVh7HaZ@ zrU@x_J(=XQC+Y5f|4nmm++4bcLqPtgocv3^TouN{=MV1M!O&OM&lDi?SKMSr<6O?( z8Q|8ViCr&?o zF3xJZ?6zh9^hf5~ue+KvYq)#s1+K2%^1@Fq=O9a4{DQ4*?4iCpJl!*`9<#7*wX%QO zes$HVlIES|EBiM6T(Ro9#J>7g_8*_aU-~X-K6&{&%T-bJ?S=*OCbsHvRIq2sUTKl$ zx%@YHUbjix&N-3M3!l$qv~24*x9etwZ<4q8oQ}f02`X${o1m{|WbcHI+MGO^msVZPb>3S3Wp(spkc&&Fe)s+-7@x znbq>W;*mT7o}M#1->0rUdSH1KRMQ2(0^Ub?;HU2!P?pyUmyQriz_ef4+a&U$yT?aqZX5 zJyYDWb8LR_8MEx^43znEY2ncjm)%a>ml`LXTRg4mWY9XX-0X{%lL87-r50K}wy|&i z7;dF1z2?fk2fnjhY7-AV)8)Of=i{r3H{yOg`Z8gi&f}!eciMfcdMbC%Ghi$6OX%Cx z8df3rcpgJir^<^)eXB`6@i!EoYd1))Rh!zx&eH8%uyt+4o3mN6f@~{i`+ti~e5anu zD0!82PC!<8Q^c>|I<7EsY=+X69#wWPY?% ofNUyeib6K^Gth9%^aV5(l)f-bWn}}of(Z!M0O_~D^vS>g0Do}s9{>OV diff --git a/keys_manager_1.py b/keys_manager_1.py new file mode 100644 index 0000000..912c9f9 --- /dev/null +++ b/keys_manager_1.py @@ -0,0 +1,137 @@ +#=========================================================================================== +# _____ _____ _ _ _ __ __ ___ ____ _____ _____ ____ _ _ +# |_ _| ____| \ | | / \ | \/ |/ _ \| _ \_ _| ____/ ___| | | | +# | | | _| | \| | / _ \ | |\/| | | | | |_) || | | _|| | | |_| | +# | | | |___| |\ |/ ___ \| | | | |_| | _ < | | | |__| |___| _ | +# |_| |_____|_| \_/_/ \_\_| |_|\___/|_| \_\|_| |_____\____|_| |_| +# +# RSA Keys Manager V1 (Python 3.6) +# +# Sources: +# https://nitratine.net/blog/post/asymmetric-encryption-and-decryption-in-python/ +# https://stackoverflow.com/questions/8933237/how-to-find-if-directory-exists-in-python +# https://stackoverflow.com/questions/273192/how-can-i-safely-create-a-nested-directory +# +#=========================================================================================== + +# Basic cryptography tools +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.asymmetric import rsa + +# To format created key obj into text to be used/exported +from cryptography.hazmat.primitives import serialization + +# To get user home directory +from pathlib import Path + + +def generate_private_key(): + # Generate Private key + private_key = rsa.generate_private_key( + public_exponent=65537, + key_size=2048, + backend=default_backend() + ) + save_key(private_key, 0) + return private_key + + +def generate_public_key(private_key): + # Get Public key from Private + public_key = private_key.public_key() + save_key(public_key, 1) + return public_key + + +def private_key_serializer(private_key): + # Key serialization + private_key_pem = private_key.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.PKCS8, + encryption_algorithm=serialization.NoEncryption() + ) + return private_key_pem + + +def public_key_serializer(public_key): + # Key serialization + public_key_pem = public_key.public_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PublicFormat.SubjectPublicKeyInfo + ) + return public_key_pem + + +def save_key(key, key_type): + keys_dir_path = Path.home() / '.auth_server_test' + # Path exists ? If not we will create it + keys_dir_path.mkdir(exist_ok=True) + if(key_type == 0): + key_pem = private_key_serializer(key) + key_path = keys_dir_path / 'id_rsa' + elif(key_type == 1): + key_pem = public_key_serializer(key) + key_path = keys_dir_path / 'id_rsa.pub' + else: + print("ERROR: Key type error") + exit() + + with open(str(key_path), 'wb') as f: + f.write(key_pem) + + +def generate_keys(): + private_key = generate_private_key() + public_key = generate_public_key(private_key) + return (private_key,public_key) + + +def check_for_existing_keys(): + keys_dir_path = Path.home() / '.auth_server_test' + private_key_path = keys_dir_path / 'id_rsa' + public_key_path = keys_dir_path / 'id_rsa.pub' + + # ========================================================= + # Private key file exists ? Load it + # --- Public key file exists ? Load it + # --- No Public key file ? Generate it from the Private + # No Private key file ? Generate both keys (save as well) + # ========================================================= + + if(private_key_path.exists()): + print('Find private key from file [ OK ]') + # Load private key from file + print('Load private key from file [ ... ]') + with open(str(private_key_path), "rb") as key_file: + private_key = serialization.load_pem_private_key( + key_file.read(), + password=None, + backend=default_backend() + ) + print('Load private key from file [ OK ]') + if(public_key_path.exists()): + print('Find public key from file [ OK ]') + # Load public key from file + print('Load public key from file [ ... ]') + with open(str(public_key_path), "rb") as key_file: + public_key = serialization.load_pem_public_key( + key_file.read(), + backend=default_backend() + ) + print('Load public key from file [ OK ]') + # Return keys + return (private_key,public_key) + else: + print('Find public key from file [ FAIL ]') + print('Generating public key from private key [ ... ]') + public_key = generate_public_key(private_key) + print('Generating public key from private key [ OK ]') + return (private_key,public_key) + else: + print('Find private key from file [ FAIL ]') + print('New keys will be generated') + return generate_keys() + + +check_for_existing_keys() +