def decrypt(iv, key, ciphertext_bytes): """ Takes strings in and is expected to give strings out. All binary string conversion is internal only. """ assert type(iv) == bytes assert type(key) == bytes assert type(ciphertext_bytes) == bytes codec = AES.new(bytestring(key), AES.MODE_CFB, bytestring(iv)) return stdstring(codec.decrypt(ciphertext_bytes))
def decrypt(iv, key, ciphertext_bytes): """ Takes strings in and is expected to give strings out. All binary string conversion is internal only. """ assert type(iv) == bytes assert type(key) == bytes assert type(ciphertext_bytes) == bytes codec = crypto.AES.new(bytestring(key), crypto.AES.MODE_CFB, bytestring(iv)) msg = stdstring(codec.decrypt(ciphertext_bytes)) if msg[0] != '[' or msg[-1] != ']': return None, "Passphrase incorrect" return msg[1:-1], None
def encrypt_str(iv, key, plaintext): """ Takes strings in and is expected to give strings out. All binary string conversion is internal only. iv: 16 character standard string key: bytes array plaintext: standard string """ assert type(iv) == bytes assert type(key) == bytes assert type(plaintext) == str codec = AES.new(bytestring(key), AES.MODE_CFB, iv) bindata = codec.encrypt(bytestring(plaintext)) return bindata
def encrypt_str(iv, key, plaintext): """ Takes strings in and is expected to give strings out. All binary string conversion is internal only. iv: 16 character standard string key: bytes array plaintext: standard string """ assert type(iv) == bytes assert type(key) == bytes assert type(plaintext) == str # Put fixes on string to be able to recognize successful decryption formatted = "[" + plaintext + "]" codec = crypto.AES.new(bytestring(key), crypto.AES.MODE_CFB, iv) bindata = codec.encrypt(bytestring(formatted)) return bindata
def encode_prop(iv_bytes, key_salt_bytes, ciphertext_bytes): assert type(iv_bytes) == bytes assert len(iv_bytes) == IV_BLOCK_SIZE assert type(key_salt_bytes) == bytes assert len(key_salt_bytes) == SALT_BLOCK_SIZE assert type(ciphertext_bytes) == bytes tmp = iv_bytes + key_salt_bytes + ciphertext_bytes payload = base64.b64encode(bytestring(tmp)) return "{" + stdstring(payload) + "}"
def make_key(salt, passphrase): dk = hashlib.pbkdf2_hmac('sha256', bytestring(passphrase), bytestring(salt), 100000) return dk