Oracle Clob加密與解密

2021-08-14 02:38:56 字數 2417 閱讀 3990

原創文章

工作需要,需要加密clob欄位,但網上很多教程都只能用於varchar的加密於是研究了一番,使用oracle官方的加密工具包,寫出了,clob加密的儲存過程和解密的儲存過程

clob加密到blob

--使用加密包需要oracle系統管理員許可權,grant execute on dbms_crypto to ****

create or replace

procedure encrypt_clobtoblob --加密clob,加密到blob

authid current_user --授予儲存過程當前使用者許可權

as input_clob clob; --加密輸入clob選擇器

output_blob_en blob; --加密輸出blob選擇器

num_key_bytes number := 256/8; -- key長度

key_bytes_raw raw (32); -- 儲存32長度的raw型的key

encryption_type pls_integer := -- 加密演算法的相關引數

dbms_crypto.encrypt_aes256

+ dbms_crypto.chain_cbc

+ dbms_crypto.pad_pkcs5;

begin

--輸入與輸出選擇器賦值

select doc_content into input_clob from tb_doc where id='123456';

--輸出將覆蓋選擇位置的原始值需要鎖表且原始值不能為空

--lob預設值可使用empty_clob(),empty_blob()

select encrypt_doc into output_blob_en from tb_encrypt_doc where id='123456'for update;

--轉換key型別為raw

key_bytes_raw := utl_raw.cast_to_raw ('ufsidf89ew8r9fh378fn837dwf893fgf');

dbms_crypto.encrypt

( dst => output_blob_en,

src => input_clob,

typ => encryption_type,

key => key_bytes_raw,

iv => null --使用預設值null

);commit;

end;

解密blob到clob

create or replace

procedure decrypt_blobtoclob

authid current_user

as output_clob clob;

input_blob_en blob;

num_key_bytes number := 256/8; -- key length 256 bits (32 bytes)

key_bytes_raw raw (32); -- stores 256-bit encryption key

encryption_type pls_integer := -- total encryption type

dbms_crypto.encrypt_aes256

+ dbms_crypto.chain_cbc

+ dbms_crypto.pad_pkcs5;

-- iv_raw raw (16);

begin

select encrypt_doc into input_blob_en from tb_encrypt_doc where id='123456';

select encrypt_doc_clob into output_clob from tb_encrypt_doc where id='123456' for update;

key_bytes_raw := utl_raw.cast_to_raw ('ufsidf89ew8r9fh378fn837dwf893fgf');

-- iv_raw := dbms_crypto.randombytes (16);

dbms_crypto.decrypt

(dst => output_clob,

src => input_blob_en,

typ => encryption_type,

key => key_bytes_raw,

iv => null

);commit;

end;

加密與解密

using system using system.text using system.security using system.security.cryptography using system.io function 的摘要說明 public class function dec 加密過程 ...

加密與解密

using system using system.collections.generic using system.text using system.security.cryptography using system.io 加密字元 加密字串 加密金鑰 返回加密 public class de...

加密與解密

客戶端和服務端公用一套金鑰,客戶端使用的加密演算法是公開的,客戶端向服務端傳送請求後,服務端返回對應金鑰,服務端解密和客戶端加密都是用的同一金鑰。無法確認公鑰是安全的。解決方法 ca ssh ca沒有解決本質問題,引入了第三方,增加了通訊成本,安全問題本質是人與人之間不信任導致的,所以才需要引入第三...