def test_pad_requirement(self): key = 32 * b"x" for s in (b"", b"foo", b"foobarbaz"): with self.assertRaises(ValueError): cbc_encrypt(key, s, pad=False) with self.assertRaises(ValueError): cbc_decrypt(key, s, pad=False)
def test_pad_requirement(self): key = 32 * b'x' for s in (b'', b'foo', b'foobarbaz'): with self.assertRaises(ValueError): cbc_encrypt(key, s, pad=False) with self.assertRaises(ValueError): cbc_decrypt(key, s, pad=False)
def test_passes(self): iv = urandom(8) key = 32 * b"x" for pt in (b"foo", b"foobarba", b"foobarbaz", 16 * b"x"): ct = cbc_encrypt(key, pt, iv) dt = cbc_decrypt(key, ct) self.assertSequenceEqual(pt, dt)
def test_passes(self): iv = urandom(8) key = 32 * b'x' for pt in (b'foo', b'foobarba', b'foobarbaz', 16 * b'x'): ct = cbc_encrypt(key, pt, iv) dt = cbc_decrypt(key, ct) self.assertEqual(pt, dt)
def encrypt(text, mode, key, iv=Random.new().read(BLOCKSIZE)): encrypted_data = None if mode == 'ECB': encrypted_data = ecb_encrypt( key, GOST.add_padding(text.encode('utf-8'))) iv = b'' elif mode == 'CBC': encrypted_data = cbc_encrypt( key, GOST.add_padding(text.encode('utf-8')), iv) elif mode == 'CFB': encrypted_data = cfb_encrypt( key, GOST.add_padding(text.encode('utf-8')), iv) return iv.hex(), b64encode(encrypted_data).decode('utf-8')
def test_meshing(self): pt = urandom(MESH_MAX_DATA * 3) key = urandom(32) ct = cbc_encrypt(key, pt) dt = cbc_decrypt(key, ct) self.assertSequenceEqual(pt, dt)
def test_iv_existence_check(self): key = 32 * b"x" with self.assertRaises(ValueError): cbc_decrypt(key, 8 * b"x") iv = urandom(8) cbc_decrypt(key, cbc_encrypt(key, 8 * b"x", iv))