Advertisement
The_Defalt

crypter.py

Jul 9th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. # This is a sort of tweak to a previous script I made called crypto.py
  2. # I made this for personal use but figured I'd share it.
  3. # The reason I made this is to ease the pain of incorporating encryption into a script
  4. # Just import this, and you'll have everything you need for AES256 encryption
  5. # The genHash() function hashes keys for integrity, it can use any algorithm supported by your hashlib version
  6. # padChar is a random character taken from os.urandom() that gets used for padding strings before encryption
  7. # padChar is generated by default but you can specify any character within the arguments of the functions
  8. # Happy crypting! :) -Defalt
  9.  
  10. # BIG FUCKING WARNING: Pycrypto is deprecated so please DO NOT use this code
  11. # I'll be publishing a new version of crypter.py using pycryptodome soon
  12.  
  13. from Crypto.Cipher import AES
  14. import os
  15. import hashlib
  16.  
  17. padChar = os.urandom(1)
  18.  
  19. def genKey(length=32):
  20.         if not length in [16, 24, 32]:
  21.                 raise Exception
  22.         return os.urandom(length)
  23.  
  24. def genHash(toHash, hashType="sha256"):
  25.         hasher = hashlib.new(hashType)
  26.         hasher.update(toHash)
  27.         return (hasher.digest(), hashType)
  28.  
  29. padToLen = lambda s, bs, p: s + (bs-len(s)%bs)*p
  30.  
  31. def encryptAES(toEncrypt, key, padChar=padChar, blockSize=16):
  32.         if len(str(padChar)) > 1:
  33.                 raise Exception
  34.         return (AES.new(key).encrypt(padToLen(toEncrypt, blockSize, str(padChar))), padChar)
  35.  
  36. def decryptAES(toDecrypt, key, padChar=padChar, blockSize=16):
  37.         if len(str(padChar)) > 1:
  38.                 raise Exception
  39.         return AES.new(key).decrypt(toDecrypt).rstrip(padChar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement