Rsa 2049 Key Generation Python

Python helper class to perform RSA encryption, decryption, signing, verifying signatures & generate new keys
rsa.py
2049Download

Generate the CSR code and Private key for your certificate by running this command: openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out servercsr.txt. Note: server.key and servercsr.txt are the Private key and the CSR code files. Feel free to use any file names, as long as you keep the.key and.txt extensions. RSA (Rivest–Shamir–Adleman) is an algorithm used by modern computers to encrypt and decrypt messages. It is an asymmetric cryptographic algorithm. Asymmetric means that there are two different keys. https://luxetree242.weebly.com/blog/halo-2-mac-download-free. This is also called public key cryptography, because one of the keys can be given to anyone. Tuneup utilities 2013 key generator. The other key must be kept private.

# RSA helper class for pycrypto
# Copyright (c) Dennis Lee
# Date 21 Mar 2017
# Description:
# Python helper class to perform RSA encryption, decryption,
# signing, verifying signatures & keys generation
# Dependencies Packages:
# pycrypto
# Documentation:
# https://www.dlitz.net/software/pycrypto/api/2.6/
# Sample usage:
''
import rsa
from base64 import b64encode, b64decode
msg1 = 'Hello Tony, I am Jarvis!'
msg2 = 'Hello Toni, I am Jarvis!'
keysize = 2048
(public, private) = rsa.newkeys(keysize)
encrypted = b64encode(rsa.encrypt(msg1, private))
decrypted = rsa.decrypt(b64decode(encrypted), private)
signature = b64encode(rsa.sign(msg1, private, 'SHA-512'))
verify = rsa.verify(msg1, b64decode(signature), public)
print(private.exportKey('PEM'))
print(public.exportKey('PEM'))
print('Encrypted: ' + encrypted)
print('Decrypted: '%s' % decrypted)
print('Signature: ' + signature)
print('Verify: %s' % verify)
rsa.verify(msg2, b64decode(signature), public)
''
fromCrypto.PublicKeyimportRSA
fromCrypto.CipherimportPKCS1_OAEP
fromCrypto.SignatureimportPKCS1_v1_5
fromCrypto.HashimportSHA512, SHA384, SHA256, SHA, MD5
fromCryptoimportRandom
frombase64importb64encode, b64decode
hash='SHA-256'
defnewkeys(keysize):
random_generator=Random.new().read
key=RSA.generate(keysize, random_generator)
private, public=key, key.publickey()
returnpublic, private
defimportKey(externKey):
returnRSA.importKey(externKey)
defgetpublickey(priv_key):
returnpriv_key.publickey()
defencrypt(message, pub_key):
#RSA encryption protocol according to PKCS#1 OAEP
cipher=PKCS1_OAEP.new(pub_key)
returncipher.encrypt(message)
defdecrypt(ciphertext, priv_key):
#RSA encryption protocol according to PKCS#1 OAEP
cipher=PKCS1_OAEP.new(priv_key)
returncipher.decrypt(ciphertext)
defsign(message, priv_key, hashAlg='SHA-256'):
globalhash
hash=hashAlg
signer=PKCS1_v1_5.new(priv_key)
if (hash'SHA-512'):
digest=SHA512.new()
elif (hash'SHA-384'):
digest=SHA384.new()
elif (hash'SHA-256'):
digest=SHA256.new()
elif (hash'SHA-1'):
digest=SHA.new()
else:
digest=MD5.new()
digest.update(message)
returnsigner.sign(digest)
defverify(message, signature, pub_key):
signer=PKCS1_v1_5.new(pub_key)
if (hash'SHA-512'):
digest=SHA512.new()
elif (hash'SHA-384'):
digest=SHA384.new()
elif (hash'SHA-256'):
digest=SHA256.new()
elif (hash'SHA-1'):
digest=SHA.new()
else:
digest=MD5.new()
digest.update(message)
returnsigner.verify(digest, signature)

Rsa 2048 Key Strength

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment