Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_encrypt_decrypt_bigfile(self):
# Expected block size + 11 bytes padding
pub_key, priv_key = rsa.newkeys((6 + 11) * 8)
# Encrypt the file
message = b('123456Sybren')
infile = BytesIO(message)
outfile = BytesIO()
bigfile.encrypt_bigfile(infile, outfile, pub_key)
# Test
crypto = outfile.getvalue()
cryptfile = BytesIO(crypto)
clearfile = BytesIO()
bigfile.decrypt_bigfile(cryptfile, clearfile, priv_key)
self.assertEquals(clearfile.getvalue(), message)
def _generateKeys(privatePath, publicPath, size):
global publicKey, privateKey
# no keys, let's generate them
log.message("Generating RSA keys of size {0}, please wait...".format(size))
publicKey, privateKey = rsa.newkeys(size)
with open(privatePath, 'w') as privKeyFile:
privKeyFile.write(privateKey.save_pkcs1())
with open(publicPath, 'w') as pubKeyFile:
pubKeyFile.write(publicKey.save_pkcs1())
def newkeys():
global my_prv_key,my_pub_key
#Usually the auditee would reuse a keypair from the previous session
#but for privacy reasons the auditee may want to generate a new key
my_pub_key, my_prv_key = rsa.newkeys(1024)
my_pem_pubkey = my_pub_key.save_pkcs1()
my_pem_privkey = my_prv_key.save_pkcs1()
with open(join(current_session_dir, 'myprivkey'), 'wb') as f: f.write(my_pem_privkey)
with open(join(current_session_dir, 'mypubkey'), 'wb') as f: f.write(my_pem_pubkey)
#also save the keys as recent, so that they could be reused in the next session
if not os.path.exists(join(data_dir, 'recentkeys')): os.makedirs(join(data_dir, 'recentkeys'))
with open(join(data_dir, 'recentkeys', 'myprivkey'), 'wb') as f: f.write(my_pem_privkey)
with open(join(data_dir, 'recentkeys', 'mypubkey'), 'wb') as f: f.write(my_pem_pubkey)
pubkey_export = b64encode(shared.bi2ba(my_pub_key.n))
return pubkey_export
raise ValueError("Invalid Private Key File")
# get private key
rsa_public_filepath = click.prompt("RSA authentication public key filepath", type=str, default="./public.pem")
with open(rsa_public_filepath, "rb") as f:
rsa_public_filepath = os.path.realpath(f.name)
data = f.read()
try:
rsa.PublicKey.load_pkcs1(data)
except:
raise ValueError("Invalid Public Key File")
# if they don't have an RSA key, make one
else:
print("Now generating RSA authentication key for hub. This will allow your hub to prove its identity to the MechWolf resolver. This step may take a few seconds.")
public, private = rsa.newkeys(2048)
with open("public.pem", "wb+") as f:
f.write(public.save_pkcs1())
rsa_public_filepath = os.path.realpath(f.name)
with open("private.pem", "wb+") as f:
f.write(private.save_pkcs1())
rsa_private_filepath = os.path.realpath(f.name)
# store key location
config_data["resolver_info"]["rsa_private_filepath"] = rsa_private_filepath
config_data["resolver_info"]["rsa_public_filepath"] = rsa_public_filepath
print("Now generating SSL certificate to encrypt communications between hub and client...")
create_self_signed_cert()
def register(name):
# hit api to see if name is already registered
if check_name(name)['status'] == 'error':
print('{} already registered.'.format(name))
else:
# generate new keypair
(pub, priv) = rsa.newkeys(512)
if os.path.exists(KEY_LOCATION) == False:
os.mkdir(KEY_LOCATION)
# save to disk
with open('{}/.key'.format(KEY_LOCATION), 'wb') as f:
pickle.dump((pub, priv), f, pickle.HIGHEST_PROTOCOL)
r = requests.post('{}/names'.format(API_LOCATION), data = {'name' : name, 'n' : pub.n, 'e' : pub.e})
if r.json()['status'] == 'success':
print('Successfully registered new name: {}'.format(name))
else:
print('Error registering name: {}'.format(name))
def create_keys(key_size=2048):
(pub_key, pri_key) = rsa.newkeys(key_size)
return (
pub_key.save_pkcs1().decode(),
pri_key.save_pkcs1().decode()
)
def generate_key(key_size):
#create rsa priv & public key
print ("[+]Creating-RSA-pair-key")
(public_key,private_key) = rsa.newkeys(key_size,poolsize=8)
print ("\t[+]Pair-key-created")
return private_key, public_key
"""
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.2 $"
__date__ = "$Date: 2010/03/06 $"
__revision__ = "$Date: 2013/02/16 $"
__copyright__ = "Copyright (c) 2010-2018 Cedric Bonhomme"
__license__ = "GPL v3"
import pickle
import rsa
import conf
print("Generating", conf.NB_BITS, "bits RSA keys ...")
pub, priv = rsa.newkeys(conf.NB_BITS)
public_key = open(conf.PUBLIC_KEY, "wb")
private_key = open(conf.PRIVATE_KEY, "wb")
print("Dumping Keys")
pickle.dump(pub, public_key)
pickle.dump(priv, private_key)
public_key.close()
public_key.close()
print("Done.")
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
import rsa
import hashlib
# Generate New RSA Keys
rsa_key = rsa.newkeys(512)
def getRSAKeys():
"""Returns a 512-Bit RSA keys"""
return rsa_key
def PrivateKey(d, n):
"""
@param d: {long | str}private exponent
@param n: {long | str}modulus
"""
if isinstance(d, bytes):
d = rsa.transform.bytes2int(d)
if isinstance(n, bytes):
n = rsa.transform.bytes2int(n)