Oraclewrap和unwrap.docx
- 文档编号:8871171
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:15
- 大小:112.53KB
Oraclewrap和unwrap.docx
《Oraclewrap和unwrap.docx》由会员分享,可在线阅读,更多相关《Oraclewrap和unwrap.docx(15页珍藏版)》请在冰豆网上搜索。
Oraclewrap和unwrap
一.Wrap说明
官网的说明如下:
APL/SQLSourceTextWrapping
YoucanwrapthePL/SQLsourcetextforanyofthesestoredPL/SQLunits,therebypreventinganyonefromdisplayingoreditingthattext:
(1)Packagespecification
(2)Packagebody
(3)Typespecification
(4)Typebody
(5)Function
(6)Procedure
AfilecontainingwrappedPL/SQLsourcetextiscalledawrappedfile.Awrappedfilecanbemoved,backedup,orprocessedbySQL*PlusortheImportandExportutilities.
Toproduceawrappedfile,useeitherthePL/SQLWrapperutilityoraDBMS_DDLPL/SQLWrapperutilitywrapsthesourcetextofeverywrappablePL/SQLunitcreatedbyaspecifiedSQLfile.TheDBMS_DDLsubprogramswrapthesourcetextofsingledynamicallygeneratedwrappablePL/SQLunits.
BoththePL/SQLWrapperutilityandDBMS_DDLsubprogramsdetecttokenizationerrors(forexample,runawaystrings),butnotsyntaxorsemanticerrors(forexample,nonexistenttablesorviews).
Wrappedfilesareupward-compatiblebetweenOracleDatabasereleases.Forexample,youcanloadfilesproducedbythePL/SQLWrapperutilityintoaOracleDatabase.
itpub上有篇文章提到了wrap加密的原理:
From:
Oracle加密的原理就是先对源码进行lz压缩lzstr,然后对压缩数据进行SHA-1运算得到40位的加密串shstr,然后将加密串与压缩串拼接得到shstr+lzstr,然后对拼接后的字符串进行Oracle双字符转换(转换表)。
最后将转换后的字符串进行base64编码,最终得到wrap的加密串。
Thedefaultfileextensionforinput_fileissql.Thedefaultnameofoutput_fileis.Therefore,thesecommandsareequivalent:
wrapiname=/mydir/myfile
wrapiname=/mydir/oname=/mydir/
Thisexamplespecifiesadifferentfileextensionforinput_fileandadifferentnameforoutput_file:
wrapiname=/mydir/oname=/yourdir/
wrap的使用步骤如下:
(1)将我们要加密的sql语句保存到一个sql文本里。
(2)用wrap进行处理,指定输入的sql,即我们第一步的问题,然后指定输出的路径和文件名,默认扩展名是plb。
(3)执行我们第二部进过wrap处理的sql,即plb文件,创建我们的对象.
示例1:
wrapfuncation
--函数
CREATEORREPLACEFUNCTIONF_DAVE(
nint
)RETURNstring
IS
BEGIN
IFn=1THEN
RETURN'DaveisDBA!
';
ELSIFn=2THEN
RETURN'DavecomefromAnQing!
';
ELSE
RETURN'DavecomefromHuaiNing!
';
ENDIF;
END;
/
SYS@dave2(db2)>selectF_DAVE(4)fromdual;
F_DAVE(4)
--------------------------------------------------------------------------------
DavecomefromHuaiNing!
BTW:
今天群里有人问我的blog的例子里为啥有安庆,因为我是安庆怀宁人。
[oracle@db2~]$pwd
/home/oracle
[oracle@db2~]$cat
CREATEORREPLACEFUNCTIONF_DAVE(
nint
)RETURNstring
IS
BEGIN
IFn=1THEN
RETURN'DaveisDBA!
';
ELSIFn=2THEN
RETURN'DavecomefromAnQing!
';
ELSE
RETURN'DavecomefromHuaiNing!
';
ENDIF;
END;
/
[oracle@db2~]$wrapiname=
PL/SQLWrapper:
ReleaseonThuAug1822:
59:
142011
Copyright(c)1993,2004,Oracle.Allrightsreserved.
Processingto
[oracle@db2~]$ls
Desktop
[oracle@db2~]$cat
CREATEORREPLACEFUNCTIONF_DAVEwrapped
a000000
1
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
8
10de7
S9NWrpt8q6tkKEMxCcfYJz2aLF4wgxDQLZ4VfC9AkE6OnV4ydypXGhveHcDg8UXy98WIg6xR
crtc/BRdQJjutbna/9+g0LlaSx87/znV+y926S1AeC0IRi/tjPJTyvJereDdk8mftMo8QMjV
fw0xXn0zVagAawwNVhSAiy/JPTMKkrBkC5ruMwQSTe6JQNq7Q2QtJV0hgQou0rYuet4/gJ5B
wAj75ph6EA==
/
SYS@dave2(db2)>@
--再次调用函数,正常使用:
SYS@dave2(db2)>selectF_DAVE(4)fromdual;
F_DAVE(4)
--------------------------------------------------------------------------------
DavecomefromHuaiNing!
--查看函数源码,已经加过密了:
SYS@dave2(db2)>selecttextfromdba_sourcewherename='F_DAVE';
TEXT
--------------------------------------------------------------------------------
FUNCTIONF_DAVEwrapped
a000000
1
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
TEXT
--------------------------------------------------------------------------------
abcd
abcd
abcd
abcd
abcd
abcd
abcd
8
10de7
S9NWrpt8q6tkKEMxCcfYJz2aLF4wgxDQLZ4VfC9AkE6OnV4ydypXGhveHcDg8UXy98WIg6xR
crtc/BRdQJjutbna/9+g0LlaSx87/znV+y926S1AeC0IRi/tjPJTyvJereDdk8mftMo8QMjV
TEXT
--------------------------------------------------------------------------------
fw0xXn0zVagAawwNVhSAiy/JPTMKkrBkC5ruMwQSTe6JQNq7Q2QtJV0hgQou0rYuet4/gJ5B
wAj75ph6EA==
示例2:
函数
thedbms_ddlpackageprovideswrapfunctionsandcreate_wrappedprocedures,eachofwhichwrapsthepl/sqlsourcetextofasingledynamicallygeneratedwrappablepl/sqlunit.
TheDBMS_DDLpackagealsoprovidestheexceptionMALFORMED_WRAP_INPUT(),whichisraisediftheinputtoWRAPorCREATE_WRAPPEDisnotavalidwrappablePL/SQLunit.(ForthelistofwrappablePL/SQLunits,seetheintroductionto.)
EachWRAPfunctiontakesasinputasingleCREATEstatementthatcreatesawrappablePL/SQLunitandreturnsanequivalentCREATEstatementinwhichthePL/SQLsourcetextiswrapped.FormoreinformationabouttheWRAPfunctions,see.
EachCREATE_WRAPPEDproceduredoeswhatitscorrespondingWRAPfunctiondoesandthenrunsthereturnedCREATEstatement,creatingthespecifiedPL/SQLunit.FormoreinformationabouttheCREATE_WRAPPEDprocedures。
该示例直接参考官方文档:
DECLARE
package_textVARCHAR2(32767);--textforcreatingpackagespecandbody
FUNCTIONgenerate_spec(pkgnameVARCHAR2)RETURNVARCHAR2AS
BEGIN
RETURN'CREATEPACKAGE'||pkgname||'AUTHIDDEFINERAS
PROCEDUREraise_salary(emp_idNUMBER,amountNUMBER);
PROCEDUREfire_employee(emp_idNUMBER);
END'||pkgname||';';
ENDgenerate_spec;
FUNCTIONgenerate_body(pkgnameVARCHAR2)RETURNVARCHAR2AS
BEGIN
RETURN'CREATEPACKAGEBODY'||pkgname||'AS
PROCEDUREraise_salary(emp_idNUMBER,amountNUMBER)IS
BEGIN
UPDATEemployees
SETsalary=salary+amountWHEREemployee_id=emp_id;
ENDraise_salary;
PROCEDUREfire_employee(emp_idNUMBER)IS
BEGIN
DELETEFROMemployeesWHEREemployee_id=emp_id;
ENDfire_employee;
END'||pkgname||';';
ENDgenerate_body;
BEGIN
package_text:
=generate_spec('emp_actions');--Generatepackagespec
EXECUTEIMMEDIATEpackage_text;--Createpackagespec
package_text:
=generate_body('emp_actions');--Generatepackagebody
--Createwrappedpackagebody
END;
/
二.Unwrap说明
wrap的目的是为了加密,所以Oracle并没有提供unwrap的方法。
itpub上的一些牛人研究了一下这个问题,写了一些unwrap的代码。
具体讨论的过程,参考itpub的2个帖子:
我这里贴一下unwrap的代码:
/*Formattedon2011/8/1812:
59:
54(QP5*/
CREATEORREPLACEPACKAGEamosunwrapper
IS
FUNCTIONdeflate(srcINVARCHAR2)
RETURNRAW;
FUNCTIONdeflate(srcINVARCHAR2,qualityINNUMBER)
RETURNRAW;
FUNCTIONinflate(srcINRAW)
RETURNVARCHAR2;
END;
/
CREATEORREPLACEPACKAGEBODYamosunwrapper
IS
FUNCTIONdeflate(srcINVARCHAR2)
RETURNRAW
IS
BEGIN
RETURNdeflate(src,6);
END;
FUNCTIONdeflate(srcINVARCHAR2,qualityINNUMBER)
RETURNRAW
AS
LANGUAGEJAVA
NAME'(int)returnbyte[]';
FUNCTIONinflate(srcINRAW)
RETURNVARCHAR2
AS
LANGUAGEJAVA
NAME'(byte[])';
END;
/
/*Formattedon2011/8/1813:
00:
16(QP5*/
CREATEORREPLACEJAVASOURCENAMEDUNWRAPPER
ASimport.*;
importclassUNWRAPPER
{
publicstaticStringInflate(byte[]src)
{
try
{
ByteArrayInputStreambis=newByteArrayInputStream(src);
InflaterInputStreamiis=newInflaterInputStream(bis);
StringBuffersb=newStringBuffer();
for(intc=();c!
=-1;c=())
{
((char)c);
}
return();
}catch(Exceptione)
{
}
returnnull;
}
publicstaticbyte[]Deflate(Stringsrc,intquality)
{
try
{
byte[]tmp=newbyte[()+100];
Deflaterdefl=newDeflater(quality);
(("UTF-8"));
();
intcnt=(tmp);
byte[]res=newbyte[cnt];
for(inti=0;i res=tmp; returnres; }catch(Exceptione) { } returnnull; } } / ALTERJAVASOURCEUNWRAPPERCOMPILE / /*Formattedon2011/8/1813: 02: 57(QP5*/ --为了输出中文,要修改java过程 CREATEORREPLACEJAVASOURCENAMEDUNWRAPPER ASimport.*; import publicclassUNWRAPPER { publicstaticStringInflate(byte[]src) { try { ByteArrayInputStreambis=newByteArrayInputStream(src); InflaterInputStreamiis=newInflaterInputStream(bis); StringBuffersb=newStringBuffer(); for(intc=();c! =-1;c=()) { ((char)c); } Stringhello=newString().getBytes("iso8859-1"),"GBK"); returnhello; }catch(Exceptione) { } returnnull; } publicstaticbyte[]Deflate(Stringsrc,intquality) { try { byte[]tmp=newbyte[()+100]; Deflaterdefl=newDeflater(quality); (("UTF-8")); (); intcnt=(tmp); byte[]res=newbyte[cnt]; for(inti=0;i res=tmp; returnres; }catch(Exceptione) { } returnnull; } } / ALTERJAVASOURCEUNWRAPPERCOMPILE / /*Formattedon2011/8/1813: 00: 41(QP5*/ CREATEORREPLACEPROCEDUREunwrap(oINVARCHAR,nINVARCHAR,tINVARCHAR) AS vWrappedtextVARCHAR2(32767); vtrimtextVARCHAR2(32767); vCharVARCHAR2 (2); vRepcharVARCHAR2 (2); vLZinflatestrVARCHAR2(32767); nLenINTEGER; nLoopINTEGER; nCntINTEGER; codeVARCHAR(512); BEGIN code: = '3D6585B318DBE287F152AB634BB5A05F7D687B9B24C228678ADEA4261E03EB176F343E7A3FD2A96A0FE935561FB14D1078D975F6BC06F9ADD6D5297E869E79E505BA84CC6E278EB05DA8F39FD0A271B858DD2C38994C480755E4538C46B62DA5AF322240DC50C3A1258B9C16605CCFFD0C981CD4376D3C3A30E86C3147F533DA43C8E35E1994ECE6A39514E09D64FA5915C52FCABB0BDFF297BF0A76B449445A1DF0009621807F1A82394FC1A7D70DD1D8FF139370EE5BEFBE09B97772E7B254B72AC00E51EDF87C8F2EF412C62B83CDACCB3BC44EC0AE88FCAA4208A64557D39ABDE1238D924A1189746B91FBFEC901EA1BF7CE';表内容存到字符数组 vtrimtext: =''; SELECTCOUNT(*) INTOncnt FROMDBA_SOURCE WHEREowner=oANDName=nANDTYPE=t; IFncnt>0ANDncnt<=5 THEN FORiIN1..ncnt LOOP IFi=1 THEN SELECTRTRIM(SUBSTR(TEXT, INSTR(TEXT, CHR(10), 1, 20) +1), CHR(10))--保存去掉前边20行的BASE64码正文 INTOvLZinflatestr FROMDBA_SOURCE WHEREowner=oANDName=nANDTYPE=tANDline=i; ELSE SELECTtext INTOvLZinflatestr FROMDBA_SOURCE WHEREowner=oANDName=nANDTYPE=tANDline=i; ENDIF; vtrimtext: =vtrimtext||vLZinflatestr; ENDLOOP; ENDIF; vtrimtext: =REPLACE(vtrimtext,CHR(10),''); nLen: =LENGTH(vtrimtext)/256; vWrappedtext: =''; FORiIN0..nLen LOOP --ifi vWrappedtext
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Oraclewrap unwrap