问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

如何取BLOB类型的MD5加密值

发布网友 发布时间:2022-04-22 03:55

我来回答

1个回答

热心网友 时间:2022-04-14 02:25

from ragini.deshpande
Here is the my_encryption package code ...........

CREATE OR REPLACE PACKAGE BODY my_encryption IS
/*
|| Local variable to hold the current encryption key.
*/
ps_encryption_key RAW(32);

/*
|| Local exception to hide Oracle -28231 Error Code.
*/
INTERNAL_BAD_KEY exception;
PRAGMA EXCEPTION_INIT(INTERNAL_BAD_KEY, -28231);

/*
|| Local exception to hide Oracle -28232 Error Code.
*/
INTERNAL_BAD_DATA exception;
PRAGMA EXCEPTION_INIT(INTERNAL_BAD_DATA, -28232);

/*
|| Local function to get the encryption key for a particular case.
*/
FUNCTION get_case_encryption_key(pi_cas_id IN ELS_CASES.ID%TYPE) RETURN RAW IS

/*
|| The key to be returned.
*/
key RAW(16);

/*
|| Cursor to return the case encyption key in encrypted format.
*/
CURSOR c_case_key(b_cas_id ELS_CASES.ID%TYPE) IS
SELECT encryption_key
FROM els_cases
WHERE id = b_cas_id;

BEGIN

OPEN c_case_key(pi_cas_id);
FETCH c_case_key INTO key;
CLOSE c_case_key;
RETURN key;

EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE NO_CASE;
END;

/*
|| Procere to initialize package with the master key.
|| The master key will be held elsewhere from the database.
*/
PROCEDURE set_master_key(pi_key IN RAW) IS
BEGIN
IF LENGTHB(pi_key) != 32 THEN
RAISE BAD_KEY;
END IF;
ps_encryption_key := pi_key;
END;

/*
|| Procere to initialize package with the master key.
|| Always returns 'Y'
|| The master key will be held elsewhere from the database.
*/
FUNCTION set_master_key(pi_key IN RAW) RETURN VARCHAR2 IS
BEGIN
set_master_key(pi_key);
RETURN 'Y';
END;

/*
|| Procere to initialize package with the case encryption key.
*/
PROCEDURE set_case_key(pi_master_key IN RAW,
pi_cas_id IN ELS_CASES.ID%TYPE) IS
BEGIN
ps_encryption_key := pi_master_key;
ps_encryption_key := decrypt(pi_data=>get_case_encryption_key(pi_cas_id));
END;

/*
|| Function to initialize package with the case encryption key.
|| Always returns 'Y'
*/
FUNCTION set_case_key(pi_master_key IN RAW,
pi_cas_id IN ELS_CASES.ID%TYPE) RETURN VARCHAR2 IS
BEGIN
set_case_key(pi_master_key,pi_cas_id);
RETURN 'Y';
END;

/*
|| Function to encrypt data using the master key. Note the length of
|| pi_data, in bytes, must be at most 2000 bytes and be divisible by 8.
*/
FUNCTION encrypt(pi_data IN RAW) RETURN RAW IS
BEGIN
RETURN dbms_obfuscation_toolkit.DES3Encrypt(input => pi_data,
key => ps_encryption_key);
EXCEPTION
WHEN INTERNAL_BAD_DATA THEN
RAISE BAD_DATA;
WHEN INTERNAL_BAD_KEY THEN
RAISE BAD_KEY;
END;

/*
|| Function to encrypt a BLOB using the current encryption key.
*/
FUNCTION encrypt(pi_blob IN BLOB) RETURN BLOB IS

/*
|| Temporary blob variable to hold the encrypted contents.
*/
result blob;

/*
|| Variable to hold the length of the blob.
*/
blob_length PLS_INTEGER := dbms_lob.getlength(pi_blob);

/*
|| The Oracle encryption routines can only encrypt data whose length is <=2000.
*/
max_chunk_length PLS_INTEGER := 2000;

/*
|| Variable to hold the length of the current chunk that is being encrypted.
*/
chunk_length PLS_INTEGER;

/*
|| Variable to remember which how much of the input blob has been encrypted.
*/
pointer PLS_INTEGER := 1;

/*
|| Variable to hold the next bit of data to be encrypted.
*/
chunk RAW(2000);

/*
|| Variable to hold a pad byte used to pad the last chunk.
*/
pad RAW(1) := utl_raw.substr(utl_raw.cast_to_raw('0'),1,1);

BEGIN

/*
|| Create the temporary blob using the database memory buffer.
*/
dbms_lob.createtemporary(result, TRUE, dbms_lob.call);

/*
|| Loop through the input blob
*/
WHILE (pointer <= blob_length) LOOP

/*
|| Grab at most 2000 bytes from the input blob.
*/
chunk_length := LEAST(max_chunk_length,blob_length-pointer+1);
chunk := dbms_lob.substr(pi_blob,chunk_length,pointer);

/*
|| Pad any chunk (ie the last) so its length is divisible by 8 (another Oracle limitation on encryption)!.
*/
WHILE mod(chunk_length,8) !=0 LOOP
chunk := utl_raw.concat(chunk,pad);
chunk_length := chunk_length+1;
END LOOP;

/*
|| Encrypt the chunk and write it to the end of the temporary blob.
*/
dbms_lob.writeappend(result,
chunk_length,
encrypt(pi_data => chunk)
);

/*
|| Advance the pointer by the length of the last chunk.
*/
pointer := pointer + chunk_length;
END LOOP;

/*
|| All Done!
*/
RETURN result;
END;

/*
|| Function to decrypt data using the master key. Note the length of
|| pi_data, in bytes, must be at most 2000 bytes and be divisible by 8.
*/
FUNCTION decrypt(pi_data IN RAW) RETURN RAW IS
BEGIN
RETURN dbms_obfuscation_toolkit.DES3Decrypt(input => pi_data,
key => ps_encryption_key);
EXCEPTION
WHEN INTERNAL_BAD_DATA THEN
RAISE BAD_DATA;
WHEN INTERNAL_BAD_KEY THEN
RAISE BAD_KEY;
END;

/*
|| Function to decrypt a BLOB using the current encryption key.
*/
FUNCTION decrypt(pi_blob IN BLOB,
pi_size IN PLS_INTEGER) RETURN BLOB IS
/*
|| Temporary blob variable to hold the encrypted contents.
*/
result BLOB;

/*
|| Variable to hold the length of the blob.
*/
blob_length PLS_INTEGER := dbms_lob.getlength(pi_blob);

/*
|| The Oracle encryption routines can only encrypt data whose length is <=2000.
*/
max_chunk_length PLS_INTEGER := 2000;

/*
|| Variable to hold the length of the current chunk that is being encrypted.
*/
chunk_length PLS_INTEGER;

/*
|| Variable to remember which how much of the input blob has been encrypted.
*/
pointer PLS_INTEGER := 1;

BEGIN

/*
|| Create the temporary blob using the database memory buffer.
*/
dbms_lob.createtemporary(result, TRUE, dbms_lob.call);

/*
|| Loop through the input blob
*/
WHILE (pointer <= blob_length) LOOP

/*
|| Grab at most 2000 bytes from the input blob.
*/
chunk_length := LEAST(max_chunk_length,blob_length-pointer+1);

/*
|| Decrypt the chunk and write it to the end of the temporary blob.
*/
dbms_lob.writeappend(result,
chunk_length,
decrypt(pi_data => dbms_lob.substr(pi_blob,
chunk_length,
pointer
)
)
);

/*
|| Advance the pointer by the length of the last chunk.
*/
pointer := pointer + chunk_length;
END LOOP;

/*
|| Remove the padding bytes that were added when the data was encrypted.
*/
dbms_lob.trim(result,pi_size);

/*
|| All Done!
*/
RETURN result;
END;

/*
|| Procere to clear session state of stored keys.
*/
PROCEDURE CLEAR IS
BEGIN
ps_encryption_key:=null;
END;

END;
/

and here is the PL/sql I run before running the sql stmt

DECLARE

mkey LONG RAW;

BEGIN

mkey := UTL_RAW.CAST_TO_RAW ('&&key');

my_encryption.set_master_key(mkey);

my_encryption.set_case_key(mkey,&&case_id);

END;

mkey is a 16 digit key .
and the encrypted_contents I'm trying to decrypt is a BLOB.

select my_encryption.decrypt(encrypted_contents,file_size),mime_type
from my_drafts where id = &&draft_id;

I hope this makes sense .

Ragini
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
美国在多少个国家有驻军? 美国在哪些国家有驻军? 美国在哪些国家派有驻军? 美国在其本土以外的驻军有多少 美国都在哪些国家有驻军 空调制热的正确调法 守捉郎与侠客行 《长安十二时辰》乱弹之五 小孩子给母亲节的祝福 孩子送给妈妈的母亲节祝福语句子2024 中国篮球a级教练都是谁 如何用plc实现闭环pid 对图像进行超像素分割后怎样提取前景和背景区域 文件格式 oppor9黑夜拍照用哪种模式 背英语单词的技巧 100个有趣的英文单词(中文加英文) 英语单词读音相同的单词 ipad上如何处理raw格式图片 求视频:12306上火车票最多提前几天买 林书豪的职业生涯 怎么用12306买火车票 林书豪简介 林书豪的简介 林书豪简介? 如何建立真正有价值的人脉 如何建立自己有效的人脉 最近怎么没有林书豪消息 大学生如何建立自己的人脉? 作为职场人,如何建立自己的人脉? 人脉是什么,怎样建立人脉? KPG是什么的 扩展名 关于合成孔径雷达 泰剧征服太阳演平苏的男演员叫什么 为什么RAW转成JPG是黑的,求解 Python的 raw_input 函数可以在cmd下运行么? 求用python计算圆周率小数点后10万位的最快算法 ,... 用手机拍摄的照片放大到100%有模糊感,怎么办? python怎么输出球的全面积 《那年花开月正圆》在历史上有没有相应的原型? 《那年花开月正圆》是根据真实事件改编的吗 各人物... 那年花开月正圆的原型是什么? 《那年花开月正圆》讲的是哪里的故事? 《那年花开月正圆》讲的是哪的故事? 《那年花开月正圆》里的沈星移的历史原型是谁? 《那年花开月正圆》周莹历史真实原型介绍是什么? 那年花开月正圆周莹历史原型是谁?周莹嫁给沈星移吗 那年花开月正圆原著叫什么?周莹历史原型是什么 那年花开月正圆沈星移的历史原型是谁 《那年花开月正圆》周莹历史真实原型介绍是什么? 那年花开月正圆改编自什么历史故事 周莹历史原型是谁