Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is a sort of tweak to a previous script I made called crypto.py
- # I made this for personal use but figured I'd share it.
- # The reason I made this is to ease the pain of incorporating encryption into a script
- # Just import this, and you'll have everything you need for AES256 encryption
- # The genHash() function hashes keys for integrity, it can use any algorithm supported by your hashlib version
- # padChar is a random character taken from os.urandom() that gets used for padding strings before encryption
- # padChar is generated by default but you can specify any character within the arguments of the functions
- # Happy crypting! :) -Defalt
- # BIG FUCKING WARNING: Pycrypto is deprecated so please DO NOT use this code
- # I'll be publishing a new version of crypter.py using pycryptodome soon
- from Crypto.Cipher import AES
- import os
- import hashlib
- padChar = os.urandom(1)
- def genKey(length=32):
- if not length in [16, 24, 32]:
- raise Exception
- return os.urandom(length)
- def genHash(toHash, hashType="sha256"):
- hasher = hashlib.new(hashType)
- hasher.update(toHash)
- return (hasher.digest(), hashType)
- padToLen = lambda s, bs, p: s + (bs-len(s)%bs)*p
- def encryptAES(toEncrypt, key, padChar=padChar, blockSize=16):
- if len(str(padChar)) > 1:
- raise Exception
- return (AES.new(key).encrypt(padToLen(toEncrypt, blockSize, str(padChar))), padChar)
- def decryptAES(toDecrypt, key, padChar=padChar, blockSize=16):
- if len(str(padChar)) > 1:
- raise Exception
- return AES.new(key).decrypt(toDecrypt).rstrip(padChar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement