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) # We have 2x6 bytes in the message, so that should result in two # bigfile. cryptfile.seek(0) varblocks = list(varblock.yield_varblocks(cryptfile)) self.assertEqual(2, len(varblocks))
def decrypt_bigfile(infile, outfile, priv_key): """Decrypts an encrypted VARBLOCK file, writing it to 'outfile' .. deprecated:: 3.4 This function was deprecated in Python-RSA version 3.4 due to security issues in the VARBLOCK format. See the documentation_ for more information. .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files :param infile: file-like object to read the crypto in VARBLOCK format from :param outfile: file-like object to write the cleartext to :param priv_key: :py:class:`rsa.PrivateKey` to decrypt with """ warnings.warn("The 'rsa.bigfile.decrypt_bigfile' function was deprecated in Python-RSA version " "3.4 due to security issues in the VARBLOCK format. See " "https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files " "for more information.", DeprecationWarning, stacklevel=2) if not isinstance(priv_key, key.PrivateKey): raise TypeError('Private key required, but got %r' % priv_key) for block in varblock.yield_varblocks(infile): cleartext = pkcs1.decrypt(block, priv_key) outfile.write(cleartext)
def decrypt_bigfile(infile, outfile, priv_key): """Decrypts an encrypted VARBLOCK file, writing it to 'outfile' :param infile: file-like object to read the crypto in VARBLOCK format from :param outfile: file-like object to write the cleartext to :param priv_key: :py:class:`rsa.PrivateKey` to decrypt with """ if not isinstance(priv_key, key.PrivateKey): raise TypeError('Private key required, but got %r' % priv_key) for block in varblock.yield_varblocks(infile): cleartext = pkcs1.decrypt(block, priv_key) outfile.write(cleartext)
def decrypt_bigfile(infile, outfile, priv_key): '''Decrypts an encrypted VARBLOCK file, writing it to 'outfile' :param infile: file-like object to read the crypto in VARBLOCK format from :param outfile: file-like object to write the cleartext to :param priv_key: :py:class:`rsa.PrivateKey` to decrypt with ''' if not isinstance(priv_key, key.PrivateKey): raise TypeError('Private key required, but got %r' % priv_key) for block in varblock.yield_varblocks(infile): cleartext = pkcs1.decrypt(block, priv_key) outfile.write(cleartext)
def decrypt_bigfile(infile, outfile, priv_key): if not isinstance(priv_key, key.PrivateKey): raise TypeError('Private key required, but got %r' % priv_key) for block in varblock.yield_varblocks(infile): cleartext = pkcs1.decrypt(block, priv_key) outfile.write(cleartext)
def test_yield_varblock(self): infile = StringIO("\x01\x0512345\x06Sybren") varblocks = list(varblock.yield_varblocks(infile)) self.assertEqual(["12345", "Sybren"], varblocks)
def test_yield_varblock(self): infile = BytesIO(b('\x01\x0512345\x06Sybren')) varblocks = list(varblock.yield_varblocks(infile)) self.assertEqual([b('12345'), b('Sybren')], varblocks)
pub_key = rsa.PublicKey.load_pkcs1(keydata) with open('private.pem', mode='rb') as privatefile: keydata = privatefile.read() priv_key = rsa.PrivateKey.load_pkcs1(keydata) with open(inpfile, 'rb') as infile, open(outfile, 'wb') as oufile: encrypt_bigfile(infile, oufile, pub_key) key_bytes = common.byte_size(pub_key.n) * BITS padding_bytes = 11 * BITS messages = [] ciphers = [] with open(outfile, 'rb') as oufile: for block in varblock.yield_varblocks(oufile): cleartext = pkcs1.decrypt(block, priv_key) message = BitArray(bytes=cleartext).bin cipher = BitArray(bytes=block).bin if len(message) == (key_bytes - padding_bytes): messages.append(np.array(map(int, message))) ciphers.append(np.array(map(int, cipher))) messages = np.array(messages) ciphers = np.array(ciphers) training_dataset = (messages, ciphers) with open("m2c_training_dataset.p", "wb") as f: pickle.dump(training_dataset, f)