Created
July 3, 2018 17:24
-
-
Save mevangelista-alvarado/351b21c8844730922d8ecc5fd00a5607 to your computer and use it in GitHub Desktop.
Test of the rsa library from python and compatible with the signature of jsrsasing library from javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rsa | |
import base64 | |
import binascii | |
def load_puk(name_file): | |
name_file = name_file + '.pem' | |
with open(name_file, 'rb') as file: | |
PublicKey = file.read() | |
puk = rsa.PublicKey.load_pkcs1_openssl_pem(PublicKey) | |
return puk | |
def load_prk(name_file): | |
name_file = name_file + '.pem' | |
with open(name_file, 'rb') as file: | |
PrivateKey = file.read() | |
prk = rsa.PrivateKey.load_pkcs1(PrivateKey) | |
return prk | |
def encrypt_data(message, puk): | |
try: | |
encrypt = rsa.encrypt(message, puk) | |
#encrypt_b64 = base64.b64encode(encrypt) | |
return encrypt | |
except Exception as e: | |
return print('[ERROR] {}'.format(e)) | |
def decrypt_data(encrypt_data, prk): | |
'''Here the value encrypt_data is equal a to function encrypt_data''' | |
try: | |
decrypt = rsa.decrypt(encrypt_data, prk) | |
decrypt_msg = decrypt.decode('utf-8') | |
return decrypt_msg | |
except Exception as e: | |
return print('[ERROR] {}'.format(e)) | |
def signature_data(message, puk): | |
try: | |
signature = rsa.sign(msj, Prk, 'SHA-1') | |
#print(base64.b64encode(signature) | |
return signature.hex() | |
except Exception as e: | |
return print('[ERROR] {}'.format(e)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment