fork download
  1. '''
  2. In this project you will implement two encryption/decryption systems, one using AES in CBC mode and another using AES in counter mode (CTR). In both cases the 16-byte encryption IV is chosen at random and is prepended to the ciphertext. For CBC encryption we use the PKCS5 padding scheme discussed in class (13:50).
  3.  
  4. While we ask that you implement both encryption and decryption, we will only test the decryption function. In the following questions you are given an AES key and a ciphertext (both are hex encoded) and your goal is to recover the plaintext and enter it in the input boxes provided below.
  5.  
  6. For an implementation of AES you may use an existing crypto library such as PyCrypto (Python), Crypto++ (C++), or any other. While it is fine to use the built-in AES functions, we ask that as a learning experience you implement CBC and CTR modes yourself.
  7. '''
  8.  
  9. from cryptography.hazmat.primitives import serialization
  10. from cryptography.hazmat.primitives.asymmetric import padding
  11. from cryptography.hazmat.backends import default_backend
  12. import base64
  13.  
  14. plaintext = '20260225022803Aa123456'
  15. public_key_pem = '''-----BEGIN PUBLIC KEY-----
  16. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt68LZ9gaZGKsJJ0ipbtk
  17. KHKarXSwElY2hZikQTa+9YZ6LlOQFA+HLkk/s9CpZsMBenibvENl7Qg8/turgKr7
  18. vwfPDf6cIp05cApxwSFsEQhlEGulAuG6fo3IdtWS65w9eHWKIjYaSLxkn0OPxcKu
  19. 86OWo8rGutq3gtB6f1tjNF31D8TpYvYB+H1fJgCz2fckTg4oocFr8iWtti+ZpnZD
  20. zafJ8qblT3Dy9gHptabRj2d9FZR9BfPeziYo/aLqSmo0psVvA1PkG0H6MEuWZfN2
  21. dHQRmka3i/CeCrGr/j/gaULBZg7TOsJdMzG6e7AP/MnfSFrgkIUSoDkKUvGNA616
  22. QwIDAQAB
  23. -----END PUBLIC KEY-----'''
  24.  
  25. public_key = serialization.load_pem_public_key(public_key_pem.encode(), backend=default_backend())
  26. encrypted = public_key.encrypt(plaintext.encode('utf-8'), padding.PKCS1v15())
  27. print(base64.b64encode(encrypted).decode('ascii'))
Success #stdin #stdout 0.13s 21040KB
stdin
Standard input is empty
stdout
FQaW4g3jt0/VWSTGYSPBIs3rGGgN/KYA7OFxBWNqp1BqUWpc4Qc/9FcbguYQNZk3k9mMNdiUPaYYXZSIMpt9Zk/QtsZ5gNqVmvZyQjGbPPq8FMv/TJfAc4/CxeMEDZbKMlAfjkb6m4XrkZyeZ0KLBKz1D6CFPNoljfJQnZXvbRkpw6X8eGFH3rGlNwVUncezyZ/wePwI1jAPgNYdFf9x+0FyaVB8jxI2I+r/QiAid87jctlhXh4hHeg3W+OUayajoc7XbrftJPowwjhq9fDn1Vx40TYGT8i8LtsBkNAQnB94HMize0VaOHsyxTfJj9BJ0kSgd+emvudfewVRPyP9mg==