MySQL Forums
Forum List  »  Security

Re: AES_ENCRYPT / AES_DECRYPT key_str length question
Posted by: Kot Demon
Date: September 13, 2010 04:50AM

I had to port my Application to Oracle lately. Added two new functions in Oracle in substitute to MySQL AES_ENCRYPT / AES_DECRYPT:

Comments are welcomed:

-------------------------------------------------------------------------------
create or replace
FUNCTION aes_decrypt(
cipher_in IN BLOB,
key_str_in IN VARCHAR2 DEFAULT 'This is my AES 128 Key')
RETURN VARCHAR2
IS
key_raw RAW (16); -- stores 128-bit encryption key
decrypted_raw RAW (2000); -- stores dencrypted Binary text
encryption_type PLS_INTEGER := DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5; -- total encryption type
BEGIN
-- DBMS_OUTPUT.PUT_LINE ( 'Original Data: ' || cipher_in);

-- Generate a 128 Bits AES Symmetric Key from input "key_str_in"
key_raw := DBMS_CRYPTO.Hash (src => UTL_I18N.STRING_TO_RAW (key_str_in, 'AL32UTF8'), typ => dbms_crypto.hash_md5);

-- Encrypt Input Data by AES 128 Block Cipher Algorithm
decrypted_raw := DBMS_CRYPTO.DECRYPT ( src => cipher_in, typ => encryption_type, KEY => key_raw );

-- DBMS_OUTPUT.PUT_LINE ('Decrypted Data: ' || decrypted_str);
RETURN (UTL_I18N.RAW_TO_CHAR (decrypted_raw, 'AL32UTF8')) ;
END;
-------------------------------------------------------------------------------
create or replace
FUNCTION aes_encrypt(
data_in IN VARCHAR2 DEFAULT 'Invalid Data',
key_str_in IN VARCHAR2 DEFAULT 'This is my AES 128 Key')
RETURN RAW
IS
key_raw RAW (16); -- stores 128-bit encryption key
encrypted_raw RAW (2000); -- stores encrypted binary text
encryption_type PLS_INTEGER := DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5; -- total encryption type
BEGIN
-- DBMS_OUTPUT.PUT_LINE ( 'Original string: ' || data_in);

-- Generate a 128 Bits AES Symmetric Key from input "key_str_in"
key_raw := DBMS_CRYPTO.Hash (src => UTL_I18N.STRING_TO_RAW (key_str_in, 'AL32UTF8'), typ => dbms_crypto.hash_md5);

-- Encrypt Input Data by AES 128 Block Cipher Algorithm
encrypted_raw := DBMS_CRYPTO.ENCRYPT ( src => UTL_I18N.STRING_TO_RAW (data_in, 'AL32UTF8'), typ => encryption_type, KEY => key_raw );

-- DBMS_OUTPUT.PUT_LINE ('Encrypted string: ' || encrypted_raw);
RETURN (encrypted_raw) ;
END;
-------------------------------------------------------------------------------


thank you



Edited 1 time(s). Last edit at 09/13/2010 08:00AM by Kot Demon.

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: AES_ENCRYPT / AES_DECRYPT key_str length question
6611
September 13, 2010 04:50AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.