zoukankan      html  css  js  c++  java
  • oracle加密-des 简单举例.


    Declare
      v_seed Raw(128);
      v_key_1 Raw(64);
      v_key_2 Raw(64);
     
      v_Text_for_encrypted Raw(64);
      v_mw  Raw(64);
      v_cleartext Raw(64);
    Begin
       --128位种子,产生密匙
       v_seed:=utl_raw.cast_to_raw('goognightgooddayadkajdfkladjfa;kf;akjdfakd;fadfadfaoeifakdjfakdjfafdafadfaf34343434343434343434343434343434343434343434343434343');  
       dbms_obfuscation_toolkit.DESGetKey(seed => v_seed,key => v_key_1);
       dbms_output.put_line(utl_raw.cast_to_varchar2(v_key_1));
      
       --用密匙加密明文
       v_text_for_encrypted:=utl_raw.cast_to_raw('China is a great country');
       dbms_obfuscation_toolkit.DESEncrypt(input =>v_text_for_encrypted ,key => v_key_1,encrypted_data =>v_mw );
       dbms_output.put_line(v_mw);
      
       --用密匙 解密 密文
       dbms_obfuscation_toolkit.DESDecrypt(input =>v_mw ,key =>v_key_1 ,decrypted_data =>v_cleartext );
       dbms_output.put_line(utl_raw.cast_to_varchar2(v_cleartext));
      
       ---des-3算法测试
       dbms_obfuscation_toolkit.DES3GetKey(which => 0,seed => v_seed,key => v_key_1);
       dbms_output.put_line(utl_raw.cast_to_varchar2(v_key_1));
      
       dbms_obfuscation_toolkit.DES3Encrypt(input =>v_text_for_encrypted ,key =>v_key_1 ,encrypted_data =>v_mw ,which => 0,iv =>Null);
      
       dbms_obfuscation_toolkit.DES3Decrypt(input => v_mw,key =>v_key_1,decrypted_data =>v_cleartext,which => 0,iv => Null);
       dbms_output.put_line(utl_raw.cast_to_varchar2(v_cleartext));
      
       
    End;  

    输出如下:
       7埓K束觿
    8332BE58695F720C4563BAD4CDAACAF687F9A3461B779D7C
    China is a great country
    x埮>豌h@?镵A.7
    China is a great country

     关于des3算法,请参考oracle <<PL/SQL Packages and Types Reference>>


    2009/10/23 补充如下:
    1)如果在其它的应用环境中调用以上过程方法,那么请务必注意变量处理的问题,否则可能导致无法正确地解密
    2)对于不同的oracle实例,其密匙都需要重新生成的。

    有两个函数:
     create or replace function func_des3_JiaMi(ClearText  in varchar2) return Varchar2 Is
    /*-----------------------------------------------------------------------
      名称:    
      功能简述:对传入的密码进行加密
      参数:
           Ename   Cname   availableValue     
           pwdd    密码
      返回:
           明文密码
      算法:
           DES-3
      注意事项:
           1)对于明文,必须是8的倍数,由于这里是加密密码的,所以,暂时算有24位。
           2)对于key而言,不同的时间产生的不一样的,不同机器产生的也不能混用,所以可以使用其它方法生成一个
             key之后,再代入到以下代码中。
      数据源:
           
      修改记录;
      lzf     2009-10-20     新增
    */  
      --v_seed Raw(128);
      v_key_1 Raw(64);
      vTmp   Varchar2(24):=rpad(ClearText,24,' ');
      --vTmp   Varchar2(24):=ClearText;
      v_cleartext Raw(64);
      v_mw  Raw(64);
    Begin
       --128位种子
     --v_seed:=utl_raw.cast_to_raw('riceWaterMeatMilkOrangeAppleBananaVegetableChickenDeskPaperInkCupTeaOilGasKnifeGunSexAgeNiceGirlBeautifulCarBusFatherGoodChinese');      
       --des-3 ,产生密匙
       --dbms_obfuscation_toolkit.DES3GetKey(which => 0,seed => v_seed,key => v_key_1);      
       --des-3-加密      
       v_key_1:=utl_raw.cast_to_raw('$?ⅣU7覡q詹m?.');
       v_cleartext:=utl_raw.cast_to_raw(vTmp);
       dbms_obfuscation_toolkit.DES3Encrypt(input =>v_cleartext ,key =>v_key_1 ,encrypted_data =>v_mw ,which => 0,iv =>Null);
       Return  utl_raw.cast_to_varchar2(v_mw);
       --Return v_mw;
    exception
      when others Then
         dbms_output.put_line(Sqlerrm);
         return null;  
    end func_des3_JiaMi;
     create or replace function func_des3_JieMi(pwd  in Varchar2) return varchar2 Is
    /*-----------------------------------------------------------------------
      名称:    function func_des3_JieMi
      功能简述:对传入的密码进行解密
      参数:
           Ename   Cname   availableValue     
           pwdd    密码
      返回:
           明文密码
      算法:
           DES-3
      注意事项:
         pwd必须是varchar2格式,不是raw格式.  
      数据源:
           
      修改记录;
      lzf     2009-10-20     新增
    */  
      --v_seed Raw(128);
      v_key_1 Raw(64);
      v_cleartext Raw(64);
      v_mw   Raw(64):=utl_raw.cast_to_raw(pwd);
      --v_mw   Raw(64):=pwd;
    Begin

       --128位种子
       --v_seed:=utl_raw.cast_to_raw('riceWaterMeatMilkOrangeAppleBananaVegetableChickenDeskPaperInkCupTeaOilGasKnifeGunSexAgeNiceGirlBeautifulCarBusFatherGoodChinese');    
       ---des-3 ,产生密匙
       --dbms_obfuscation_toolkit.DES3GetKey(which => 0,seed => v_seed,key => v_key_1);      
       --des-3-解密   
       v_key_1:=utl_raw.cast_to_raw('$?ⅣU7覡q詹m?.');
       dbms_obfuscation_toolkit.DES3Decrypt(input => v_mw,key =>v_key_1,decrypted_data =>v_cleartext,which => 0,iv => Null);  
       Return  utl_raw.cast_to_varchar2(v_cleartext);
    exception
      when others then
         return null;  
    end function func_des3_JieMi;
    /

  • 相关阅读:
    ASP.NET零碎
    My97DatePicker 和转换 数据库中日期(/Date(1351699200000)/) 的格式
    生成验证码
    自己封装的AJAX (带JSON)
    WebForm
    配置进程外Session
    使用Xcode和Instruments调试解决iOS内存泄露
    IOS block 教程<转>
    iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)
    支付宝在ios应用上的开发[转]
  • 原文地址:https://www.cnblogs.com/huapox/p/3509882.html
Copyright © 2011-2022 走看看