Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Search

Free tool

Look and Feel Project

13 octobre 2005 4 13 /10 /octobre /2005 00:00

How to encrypt/decrypt strings with the dbms_obfuscation_toolkit / dbms_crypto packages

Oracle 9i dbms_obfuscation_toolkit package

CREATE OR REPLACE PACKAGE Cryptit AS
   FUNCTION encrypt( Str VARCHAR2 ) RETURN RAW;
   FUNCTION decrypt( xCrypt VARCHAR2 ) RETURN VARCHAR2;
END Cryptit;
/

CREATE OR REPLACE PACKAGE BODY Cryptit AS
   crypt_raw RAW(2000);
   crypt_str VARCHAR(2000);

   -- Encrypt the string --
   FUNCTION encrypt( Str VARCHAR2 ) RETURN RAW AS

   l INTEGER := LENGTH(str);
   i INTEGER;
   padblock RAW(2000);
   Cle RAW(8) := UTL_RAW.CAST_TO_RAW('frankzap');

   BEGIN
      i := 8-MOD(l,8);
      padblock := utl_raw.cast_to_raw(str||RPAD(CHR(i),i,CHR(i)));

      dbms_obfuscation_toolkit.DESEncrypt(
               input     => padblock,
               KEY       => Cle,
               encrypted_data => crypt_raw );
      RETURN crypt_raw ;
   END;

   -- Decrypt the string --
   FUNCTION decrypt( xCrypt VARCHAR2 ) RETURN VARCHAR2 AS
   l NUMBER;
   Cle RAW(8) := UTL_RAW.CAST_TO_RAW('frankzap');
   crypt_raw RAW(2000) := utl_raw.cast_to_raw(utl_raw.cast_to_varchar2(xCrypt)) ;
   BEGIN
      dbms_obfuscation_toolkit.DESDecrypt(
               input     => xCrypt,
               KEY       => Cle,
               decrypted_data => crypt_raw );
      crypt_str := utl_raw.cast_to_varchar2(crypt_raw);
      l := LENGTH(crypt_str);
      crypt_str := RPAD(crypt_str,l-ASCII(SUBSTR(crypt_str,l)));
      RETURN crypt_str;
   END;
END Cryptit;
/

 SQL*Plus: Release 9.0.1.3.0 - Production on Je Oct 13 10:28:55 2005

(c) Copyright 2001 Oracle Corporation.  All rights reserved.


Connecté à :
Oracle9i Enterprise Edition Release 9.2.0.5.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.5.0 - Production

SQL> set serveroutput on
SQL>
SQL> DECLARE
  2    LC$Code VARCHAR2(100) := 'Music is the best!' ;
  3  BEGIN
  4 
  5    -- Get the encrypted string --
  6    LC$Code := Cryptit.Encrypt( LC$Code ) ;
  7    dbms_output.put_line( LC$Code ) ;
  8 
  9    -- Get the decrypted string --
 10    LC$Code := Cryptit.Decrypt( LC$Code ) ;
 11    dbms_output.put_line( LC$Code ) ;
 12 
 13  END ;  
 14 
 15  /
7840712517939382157F5BA660E3AAB094E9C2BA67FF4234
Music is the best!

Procédure PL/SQL terminée avec succès.

SQL>

 
Oracle 10g dbms_crypto package

SQL> set serveroutput on
SQL>
SQL> DECLARE
  2   LC$Source    VARCHAR2(19) := 'Music is the best!';
  3   LR$Source    RAW(128) := utl_raw.cast_to_raw(LC$Source);
  4   LR$Key       RAW(128) := utl_raw.cast_to_raw('FrankZappa');
  5   LR$Crypted   RAW(2048);
  6   LR$Decrypted RAW(2048);
  7  
  8  BEGIN
  9 
 10    dbms_output.put_line('Source string : ' || LC$Source);
 11 
 12    LR$Crypted := dbms_crypto.encrypt(LR$Source,
 13    dbms_crypto.des_cbc_pkcs5, LR$Key);
 14 
 15    dbms_output.put_line('Encrypted raw : ' ||
 16    RAWTOHEX(utl_raw.cast_to_raw(LR$Crypted)));
 17 
 18    LR$Decrypted := dbms_crypto.decrypt(src => LR$Crypted,
 19    typ => dbms_crypto.des_cbc_pkcs5, key => LR$Key);
 20 
 21    dbms_output.put_line('Decrypted string : ' ||
 22    utl_raw.cast_to_varchar2(LR$Decrypted));
 23  END;
 24  /
Source string : Music is the best!
Encrypted raw :
33454635363638343931453734313342373337454141313133453637323639373934373433463442
4533464246333831
Decrypted string : Music is the best!

PL/SQL procedure successfully completed.

SQL>

Francois

Partager cet article

Repost 0
Francois Degrelle - dans Oracle PL-SQL
commenter cet article

commentaires

Simon 13/10/2005 15:53

I'm guessing that answer probably lies here?
http://metalink.oracle.com/metalink/plsql/showdoc?db=NOT&id=225214.1

Simon 13/10/2005 15:47

sent test case via contact page

Simon 13/10/2005 14:43

Will have to do some digging original case was with confidential data.

Francois 13/10/2005 14:18

I don't know. Could you test ?

Simon 13/10/2005 13:40

However if you try and decrypt something created outside Oracle (with RSA key supplied) it fails?