generate_aes_testdata.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  6. import codecs
  7. import subprocess
  8. from yt_dlp.aes import aes_encrypt, key_expansion
  9. from yt_dlp.utils import intlist_to_bytes
  10. secret_msg = b'Secret message goes here'
  11. def hex_str(int_list):
  12. return codecs.encode(intlist_to_bytes(int_list), 'hex')
  13. def openssl_encode(algo, key, iv):
  14. cmd = ['openssl', 'enc', '-e', '-' + algo, '-K', hex_str(key), '-iv', hex_str(iv)]
  15. prog = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  16. out, _ = prog.communicate(secret_msg)
  17. return out
  18. iv = key = [0x20, 0x15] + 14 * [0]
  19. r = openssl_encode('aes-128-cbc', key, iv)
  20. print('aes_cbc_decrypt')
  21. print(repr(r))
  22. password = key
  23. new_key = aes_encrypt(password, key_expansion(password))
  24. r = openssl_encode('aes-128-ctr', new_key, iv)
  25. print('aes_decrypt_text 16')
  26. print(repr(r))
  27. password = key + 16 * [0]
  28. new_key = aes_encrypt(password, key_expansion(password)) * (32 // 16)
  29. r = openssl_encode('aes-256-ctr', new_key, iv)
  30. print('aes_decrypt_text 32')
  31. print(repr(r))