2020-08-16 17:50:17 +00:00
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
from cryptography.hazmat.primitives import hashes
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import padding
|
2020-08-20 01:39:26 +00:00
|
|
|
from cryptography.exceptions import InvalidSignature
|
2020-08-16 17:50:17 +00:00
|
|
|
import os
|
2020-08-20 09:51:57 +00:00
|
|
|
try:
|
|
|
|
from .syfr import * #import syfr
|
|
|
|
except ImportError:
|
|
|
|
from syfr import *
|
2020-08-16 17:50:17 +00:00
|
|
|
|
|
|
|
key_dir = os.path.join(os.path.expanduser('~'),'.keys','komrade')
|
|
|
|
if not os.path.exists(key_dir): os.makedirs(key_dir)
|
|
|
|
PATH_PRIVATE_KEY=os.path.join(key_dir,'private_key.pem')
|
|
|
|
PATH_PUBLIC_KEY=os.path.join(key_dir,'public_key.pem')
|
|
|
|
|
|
|
|
|
|
|
|
### CREATING KEYS
|
|
|
|
|
2020-08-17 13:33:26 +00:00
|
|
|
def new_keys(save=True,password=None):
|
2020-08-16 17:50:17 +00:00
|
|
|
private_key = rsa.generate_private_key(
|
|
|
|
public_exponent=65537,
|
|
|
|
key_size=2048,
|
|
|
|
backend=default_backend()
|
|
|
|
)
|
|
|
|
public_key = private_key.public_key()
|
|
|
|
|
|
|
|
if save:
|
2020-08-17 13:33:26 +00:00
|
|
|
save_private_key(private_key,password=password)
|
2020-08-16 17:50:17 +00:00
|
|
|
save_public_key(public_key)
|
|
|
|
|
|
|
|
return private_key,public_key
|
|
|
|
|
2020-08-20 08:55:50 +00:00
|
|
|
|
|
|
|
|
2020-08-17 16:23:40 +00:00
|
|
|
def save_private_key(private_key,fn=PATH_PRIVATE_KEY,password=None, return_instead=False):
|
2020-08-16 17:50:17 +00:00
|
|
|
pem = private_key.private_bytes(
|
|
|
|
encoding=serialization.Encoding.PEM,
|
|
|
|
format=serialization.PrivateFormat.PKCS8,
|
2020-08-17 13:33:26 +00:00
|
|
|
encryption_algorithm=serialization.NoEncryption() if not password else serialization.BestAvailableEncryption(password.encode())
|
2020-08-16 17:50:17 +00:00
|
|
|
)
|
2020-08-17 10:27:11 +00:00
|
|
|
if return_instead: return pem
|
2020-08-16 17:50:17 +00:00
|
|
|
with open(fn,'wb') as f: f.write(pem)
|
|
|
|
|
2020-08-17 10:27:11 +00:00
|
|
|
def save_public_key(public_key,fn=PATH_PUBLIC_KEY,return_instead=False):
|
2020-08-16 17:50:17 +00:00
|
|
|
pem = public_key.public_bytes(
|
|
|
|
encoding=serialization.Encoding.PEM,
|
|
|
|
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
|
|
|
)
|
2020-08-17 10:27:11 +00:00
|
|
|
if return_instead: return pem
|
2020-08-16 17:50:17 +00:00
|
|
|
with open(fn,'wb') as f: f.write(pem)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### LOADING KEYS
|
|
|
|
def load_keys():
|
2020-08-17 16:23:40 +00:00
|
|
|
return (load_private_key_from_file(), load_public_key_from_file())
|
2020-08-16 17:50:17 +00:00
|
|
|
|
2020-08-17 16:23:40 +00:00
|
|
|
def load_private_key(pem,password=None):
|
|
|
|
return serialization.load_pem_private_key(
|
|
|
|
pem,
|
|
|
|
password=password.encode() if password else None,
|
|
|
|
backend=default_backend()
|
|
|
|
)
|
|
|
|
|
|
|
|
def load_private_key_from_file(fn=PATH_PRIVATE_KEY,password=None):
|
2020-08-16 17:50:17 +00:00
|
|
|
with open(fn, "rb") as key_file:
|
2020-08-17 16:23:40 +00:00
|
|
|
return load_private_key(key_file.read(), password)
|
|
|
|
|
|
|
|
def load_public_key(pem):
|
|
|
|
return serialization.load_pem_public_key(
|
|
|
|
pem,
|
2020-08-16 17:50:17 +00:00
|
|
|
backend=default_backend()
|
|
|
|
)
|
|
|
|
|
2020-08-17 16:23:40 +00:00
|
|
|
def load_public_key_from_file(fn=PATH_PUBLIC_KEY):
|
2020-08-16 17:50:17 +00:00
|
|
|
with open(fn, "rb") as key_file:
|
2020-08-17 16:23:40 +00:00
|
|
|
return load_public_key(key_file.read())
|
2020-08-16 17:50:17 +00:00
|
|
|
|
|
|
|
### DE/ENCRYPTING
|
|
|
|
def encrypt_msg(message, public_key):
|
|
|
|
return public_key.encrypt(
|
2020-08-20 08:55:50 +00:00
|
|
|
message,
|
2020-08-16 17:50:17 +00:00
|
|
|
padding.OAEP(
|
|
|
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
|
|
|
algorithm=hashes.SHA256(),
|
|
|
|
label=None
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-08-20 08:55:50 +00:00
|
|
|
def encrypt_msg_symmetric(message):
|
|
|
|
import os
|
|
|
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
backend = default_backend()
|
|
|
|
key = os.urandom(32)
|
|
|
|
iv = os.urandom(16)
|
|
|
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
|
|
|
|
encryptor = cipher.encryptor()
|
|
|
|
|
|
|
|
ct = encryptor.update(message) + encryptor.finalize()
|
|
|
|
return ct
|
|
|
|
|
|
|
|
def decrypt_msg_symmetric():
|
|
|
|
import os
|
|
|
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
backend = default_backend()
|
|
|
|
key = os.urandom(32)
|
|
|
|
iv = os.urandom(16)
|
|
|
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
|
|
|
|
|
|
|
|
return ct
|
|
|
|
decryptor = cipher.decryptor()
|
|
|
|
decryptor.update(ct) + decryptor.finalize()
|
|
|
|
b'a secret message'
|
|
|
|
|
2020-08-16 17:50:17 +00:00
|
|
|
def decrypt_msg(encrypted, private_key):
|
|
|
|
return private_key.decrypt(
|
|
|
|
encrypted,
|
|
|
|
padding.OAEP(
|
|
|
|
mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
|
|
|
algorithm=hashes.SHA256(),
|
|
|
|
label=None
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
### SIGNING/VERIFYING
|
|
|
|
def sign_msg(message, private_key):
|
|
|
|
return private_key.sign(
|
|
|
|
message,
|
|
|
|
padding.PSS(
|
|
|
|
mgf=padding.MGF1(hashes.SHA256()),
|
|
|
|
salt_length=padding.PSS.MAX_LENGTH
|
|
|
|
),
|
|
|
|
hashes.SHA256()
|
|
|
|
)
|
|
|
|
|
|
|
|
def verify_msg(message, signature, public_key):
|
2020-08-20 01:39:26 +00:00
|
|
|
try:
|
|
|
|
verified = public_key.verify(
|
|
|
|
signature,
|
|
|
|
message,
|
|
|
|
padding.PSS(
|
|
|
|
mgf=padding.MGF1(hashes.SHA256()),
|
|
|
|
salt_length=padding.PSS.MAX_LENGTH
|
|
|
|
),
|
|
|
|
hashes.SHA256()
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
except InvalidSignature:
|
|
|
|
return False
|
|
|
|
return None
|
2020-08-16 17:50:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-17 10:27:11 +00:00
|
|
|
# #private_key,public_key = new_keys()
|
|
|
|
# private_key,public_key = load_keys()
|
2020-08-16 17:50:17 +00:00
|
|
|
|
2020-08-17 10:27:11 +00:00
|
|
|
# #print(private_key)
|
|
|
|
# #print(public_key)
|
2020-08-16 17:50:17 +00:00
|
|
|
|
|
|
|
|
2020-08-17 10:27:11 +00:00
|
|
|
# #enc = encrypt_msg('Drive your plow over the bones of the dead', public_key)
|
|
|
|
# #print(enc)
|
2020-08-16 17:50:17 +00:00
|
|
|
|
2020-08-17 10:27:11 +00:00
|
|
|
# dec = decrypt_msg(enc,private_key)
|
|
|
|
# #print(dec)
|
2020-08-16 17:50:17 +00:00
|
|
|
|
2020-08-17 10:27:11 +00:00
|
|
|
# msg = b'hello'
|
|
|
|
# signature = sign_msg(msg,private_key)
|
2020-08-16 17:50:17 +00:00
|
|
|
|
2020-08-17 10:27:11 +00:00
|
|
|
# #print(encrypt_msg(b'hello',public_key))
|
2020-08-16 17:50:17 +00:00
|
|
|
|
2020-08-20 01:39:26 +00:00
|
|
|
# print(verify_msg(msg+b'!!',signature,public_key))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## ONLY NEEDS RUN ONCE!
|
2020-08-20 09:51:57 +00:00
|
|
|
def gen_global_keys1(fn='.keys.global.json'):
|
2020-08-20 01:39:26 +00:00
|
|
|
from kivy.storage.jsonstore import JsonStore
|
|
|
|
|
|
|
|
private_key,public_key=new_keys(save=False,password=None)
|
|
|
|
pem_private_key = save_private_key(private_key,password=None,return_instead=True)
|
|
|
|
pem_public_key = save_public_key(public_key,return_instead=True)
|
|
|
|
|
|
|
|
store = JsonStore('./.keys.global.json')
|
|
|
|
|
|
|
|
store.put('_keys',private=str(pem_private_key.decode()),public=str(pem_public_key.decode())) #(private_key,password=passkey)
|
|
|
|
|
2020-08-20 09:51:57 +00:00
|
|
|
def gen_global_keys(fn='.keys.global.json'):
|
|
|
|
from kivy.storage.jsonstore import JsonStore
|
|
|
|
|
|
|
|
store = JsonStore('./.keys.global.json')
|
|
|
|
|
|
|
|
#store.put('_keys',private=str(pem_private_key.decode()),public=str(pem_public_key.decode())) #(private_key,password=passkey)
|
|
|
|
|
|
|
|
private_key = generate_rsa_key()
|
|
|
|
pem_private_key = serialize_privkey(private_key, password=None)# save_private_key(private_key,password=passkey,return_instead=True)
|
|
|
|
pem_public_key = serialize_pubkey(private_key.public_key())
|
|
|
|
store.put('_keys',private=pem_private_key.decode(),public=pem_public_key.decode()) #(private_key,password=passkey)
|
|
|
|
|
2020-08-20 08:55:50 +00:00
|
|
|
"""
|
|
|
|
import os
|
|
|
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
backend = default_backend()
|
|
|
|
key = os.urandom(32)
|
|
|
|
iv = os.urandom(16)
|
|
|
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
|
|
|
|
encryptor = cipher.encryptor()
|
|
|
|
ct = encryptor.update(b"a secret message") + encryptor.finalize()
|
|
|
|
decryptor = cipher.decryptor()
|
|
|
|
decryptor.update(ct) + decryptor.finalize()
|
2020-08-20 15:09:43 +00:00
|
|
|
b'a secret message'"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def aes_rsa_encrypt(message, recipient_rsa_pub):
|
|
|
|
aes_key = create_aes_key()
|
|
|
|
aes_ciphertext, iv = aes_encrypt(message, aes_key)
|
|
|
|
encry_aes_key = rsa_encrypt(aes_key, recipient_rsa_pub)
|
|
|
|
return aes_ciphertext, encry_aes_key, iv #, sign
|
|
|
|
|
|
|
|
|
|
|
|
def aes_rsa_decrypt(aes_ciphertext, rsa_priv, encry_aes_key, iv): #, hmac, hmac_signature, rsa_priv, iv, metadata):
|
|
|
|
aes_key = rsa_decrypt(encry_aes_key, rsa_priv)
|
|
|
|
plaintext = aes_decrypt(aes_ciphertext, aes_key, iv)
|
|
|
|
return plaintext
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def _decrypt_rsa(x,privkey=CORRECT_PRIV_KEY):
|
|
|
|
# x_decr = rsa_decrypt(x,privkey)
|
|
|
|
# return x_decr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# encrypt sender
|
|
|
|
#def _enc_sender(x):
|
|
|
|
# recv=receiver_pubkey
|
|
|
|
|
|
|
|
# encrypt recipient too?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# sender_pubkey_b_encr = sep2.join(
|
|
|
|
# aes_rsa_encrypt(
|
|
|
|
# serialize_pubkey(self.public_key), receiver_pubkey
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
|
|
|
|
# receiver_pubkey_b_encr = sep2.join(
|
|
|
|
# aes_rsa_encrypt(
|
|
|
|
# serialize_pubkey(receiver_pubkey_b, receiver_pubkey
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# msg_encr = sep2.join([val_encr,val_encr_key,iv])
|
|
|
|
# sender_encr = sep2.join(
|
|
|
|
# aes_rsa_encrypt(
|
|
|
|
# serialize_pubkey(self.public_key), receiver_pubkey
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
# signature_encr = sep2.join(
|
|
|
|
# rsa_encrypt(
|
|
|
|
# signature,
|
|
|
|
# receiver_pubkey
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# sender = sep2.join(sender_pubkey_b, signature)
|
|
|
|
|
|
|
|
# WDV = sep.join([
|
|
|
|
# time_b,
|
|
|
|
# receiver_encr,
|
|
|
|
# msg_encr,
|
|
|
|
# sender_encr,
|
|
|
|
# signature_encr
|
|
|
|
# ])
|