Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
0
 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)
Beispiel #4
0
 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')
Beispiel #6
0
 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)
Beispiel #7
0
 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))