GRANT EXECUTE ON DBMS_CRYPTO TO [계정명];
GRANT EXECUTE ON DBMS_OBFUSCATION_TOOLKIT TO [계정명];
//암호화
SELECT PKG_CRYPTO.ENCRYPT('TEST_PASS') FROM DUAL;
//복호화
SELECT PKG_CRYPTO.DECRYPT('51C6B8ABD615F6F097659BF9E75C353C') FROM DUAL;
/***헤더***/
CREATE OR REPLACE PACKAGE PKG_CRYPTO
IS
/******************************************************************************
암호화
******************************************************************************/
FUNCTION ENCRYPT(V_INPUT_STRING IN VARCHAR2,
V_KEY IN VARCHAR2 := 'KHJAVA301$') --> 비밀키 초기값 선언
RETURN RAW; --> RAW : 원시데이터타입. 알아볼 수 없도록 하기 위해서
/******************************************************************************
복호화
******************************************************************************/
FUNCTION DECRYPT(V_INPUT_STRING IN VARCHAR2, --> 암호화되어진 문자 그대로 넣어줌
V_KEY IN VARCHAR2 := 'KHJAVA301$') --> 복호화할 비밀키
RETURN VARCHAR2;
END PKG_CRYPTO;
/***바디***/
CREATE OR REPLACE PACKAGE BODY PKG_CRYPTO
IS
V_KEY_RAW RAW(64);
/******************************************************************************
암호화
******************************************************************************/
FUNCTION ENCRYPT(V_INPUT_STRING IN VARCHAR2,
V_KEY IN VARCHAR2 := 'KHJAVA301$') --> 비밀키 초기값 선언
RETURN RAW
IS
V_ORIGINAL_RAW RAW(64);
V_ENCRYPTED_RAW RAW(64);
BEGIN
V_ORIGINAL_RAW := UTL_I18N.STRING_TO_RAW(V_INPUT_STRING,'AL32UTF8'); --> 문자열을 RAW 타입으로 바꿔 줌
V_KEY_RAW := UTL_I18N.STRING_TO_RAW(V_KEY, 'AL32UTF8');
V_ENCRYPTED_RAW := DBMS_CRYPTO.ENCRYPT(SRC => V_ORIGINAL_RAW
,TYP => DBMS_CRYPTO.DES_CBC_PKCS5
,KEY => V_KEY_RAW, IV => NULL);
RETURN V_ENCRYPTED_RAW;
END ENCRYPT;
/******************************************************************************
복호화
******************************************************************************/
FUNCTION DECRYPT(V_INPUT_STRING IN VARCHAR2, --> 암호화되어진 문자 그대로 넣어줌
V_KEY IN VARCHAR2 := 'KHJAVA301$') --> 복호화할 비밀키
RETURN VARCHAR2
IS
V_DECRYPTED_RAW VARCHAR2(64);
V_DECRYPTED_CHAR VARCHAR2(64);
BEGIN
V_KEY_RAW := UTL_I18N.STRING_TO_RAW(V_KEY, 'AL32UTF8');
V_DECRYPTED_RAW := DBMS_CRYPTO.DECRYPT(SRC => V_INPUT_STRING
,TYP => DBMS_CRYPTO.DES_CBC_PKCS5
,KEY => V_KEY_RAW, IV => NULL);
V_DECRYPTED_CHAR := UTL_I18N.RAW_TO_CHAR(V_DECRYPTED_RAW,'AL32UTF8');
RETURN V_DECRYPTED_CHAR;
END DECRYPT;
END PKG_CRYPTO;