SHARE
TWEET

Untitled

a guest Nov 12th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Decrypt.py
  2.  
  3. from cryptography.hazmat.primitives import hashes
  4. from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
  5. from cryptography.hazmat.primitives.ciphers import algorithms, modes, Cipher
  6. from cryptography.hazmat.backends import default_backend
  7. from os import path
  8. from os import urandom
  9. import base64
  10.  
  11.  
  12. STUDENTNAME = "IvanusicIvan" # ne koriste se HR slova (čćžšđ)
  13. SALT = "DAJSOLI" # pitajte profesora na vježbama
  14.  
  15. # Generate hash
  16. def hash_me(msg, hash_function):
  17.     digest = hashes.Hash(hash_function, backend=default_backend())
  18.     if not isinstance(msg, bytes):
  19.         msg = msg.encode()
  20.     digest.update(msg)
  21.     return digest.finalize()
  22.  
  23. def decrypt_CTR(key, iv, ciphertext, cipher=algorithms.AES):
  24.     if not isinstance(key, bytes):
  25.         key = key.encode()
  26.  
  27.     decryptor = Cipher(cipher(key), modes.CTR(iv), backend=default_backend()).decryptor()
  28.     plaintext = decryptor.update(ciphertext)
  29.     plaintext += decryptor.finalize()
  30.  
  31.     return plaintext
  32.  
  33. if __name__ =='__main__':
  34.     #Calculating hash for filename
  35.     filename = hash_me(str.encode(STUDENTNAME + SALT), hashes.SHA256()).hex()
  36.     print("\nFile name is: " + filename + "\n")
  37.     f_in = open(filename + ".enc", 'rb' )
  38.    
  39.     #Reading file
  40.     challenge = f_in.read()
  41.     print(challenge)
  42.     challenge_decoded = base64.b64decode(challenge)
  43.  
  44.     #Splitting decoded text
  45.     iv = challenge_decoded[:16]
  46.     ciphertext = challenge_decoded[16:]
  47.     for x in range(4096):
  48.         x_str = str(x)
  49.         x_encoded = x_str.encode()
  50.         KDF = Scrypt(
  51.             length=32,
  52.             salt=b'salt',
  53.             n=2**15,
  54.             r=8,
  55.             p=1,
  56.             backend=default_backend()
  57.         )
  58.         key = KDF.derive(x_encoded)
  59.         print(x)
  60.  
  61.         #Ciphertext decryption
  62.         plaintext=decrypt_CTR(key, iv, ciphertext)
  63.         if "Chuck".encode() in plaintext:
  64.             print("Plaintext:\n")
  65.             print(plaintext)
  66.             break
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top