I want to create the below encrypt and decrypt UDF's in Redshift.
Library:
create library pyaes
language plpythonu
from 's3://aws_python/library/pyaes/pyaes.zip'
credentials 'aws-role'
region as 'aws-region';
Encrypt:
CREATE OR REPLACE FUNCTION test.aes_encrypt(input varchar(max))
RETURNS varchar(max) AS
' if input is None:
return None
import pyaes
key = ''abcdefghijklopoo''
aes = pyaes.AESModeOfOperationCTR(key)
encrypted_msg = aes.encrypt(input)
return encrypted_msg
'
LANGUAGE plpythonu STABLE;
Tried with below options as well:
encrypted_msg = aes.encrypt(input.encode("utf8"))
key = key.encode('utf-8')
Decrypt:
CREATE OR REPLACE FUNCTION test.aes_decrypt(encrypted_msg varchar(max))
RETURNS varchar(max) AS
'
if encrypted_msg is None or len(str(encrypted_msg)) == 0:
return None
import pyaes
key = ''abcdefghijklopoo''
aes = pyaes.AESModeOfOperationCTR (key)
decrypted_msg = aes.decrypt(encrypted_msg).decode("utf8")
return decrypted_msg
'
LANGUAGE plpythonu STABLE;
select aes_encrypt('Testing'); select aes_decrypt('');
But it's throwing below error:
Error: Invalid operation: String contains invalid or unsupported UTF8 codepoints. Bad UTF8 hex sequence: d5 fc (error 4);
Please advise. Thanks in advance.