3 from __future__ import unicode_literals
5 # Allow direct execution
9 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11 from youtube_dl.aes import aes_decrypt, aes_encrypt, aes_cbc_decrypt, aes_cbc_encrypt, aes_decrypt_text
12 from youtube_dl.utils import bytes_to_intlist, intlist_to_bytes
15 # the encrypted data can be generate with 'devscripts/generate_aes_testdata.py'
18 class TestAES(unittest.TestCase):
20 self.key = self.iv = [0x20, 0x15] + 14 * [0]
21 self.secret_msg = b'Secret message goes here'
23 def test_encrypt(self):
26 encrypted = aes_encrypt(bytes_to_intlist(msg), key)
27 decrypted = intlist_to_bytes(aes_decrypt(encrypted, key))
28 self.assertEqual(decrypted, msg)
30 def test_cbc_decrypt(self):
31 data = bytes_to_intlist(
32 b"\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd"
34 decrypted = intlist_to_bytes(aes_cbc_decrypt(data, self.key, self.iv))
35 self.assertEqual(decrypted.rstrip(b'\x08'), self.secret_msg)
37 def test_cbc_encrypt(self):
38 data = bytes_to_intlist(self.secret_msg)
39 encrypted = intlist_to_bytes(aes_cbc_encrypt(data, self.key, self.iv))
42 b"\x97\x92+\xe5\x0b\xc3\x18\x91ky9m&\xb3\xb5@\xe6'\xc2\x96.\xc8u\x88\xab9-[\x9e|\xf1\xcd")
44 def test_decrypt_text(self):
45 password = intlist_to_bytes(self.key).decode('utf-8')
46 encrypted = base64.b64encode(
47 intlist_to_bytes(self.iv[:8]) +
48 b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae'
50 decrypted = (aes_decrypt_text(encrypted, password, 16))
51 self.assertEqual(decrypted, self.secret_msg)
53 password = intlist_to_bytes(self.key).decode('utf-8')
54 encrypted = base64.b64encode(
55 intlist_to_bytes(self.iv[:8]) +
56 b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83'
58 decrypted = (aes_decrypt_text(encrypted, password, 32))
59 self.assertEqual(decrypted, self.secret_msg)
62 if __name__ == '__main__':