Skip to Content
Oracle PL/SQL for DBAs
book

Oracle PL/SQL for DBAs

by Arup Nanda, Steven Feuerstein
October 2005
Intermediate to advanced
454 pages
14h 44m
English
O'Reilly Media, Inc.
Content preview from Oracle PL/SQL for DBAs

Decrypting Data

On the other side of the coin is the decryption process, which decodes the encrypted string using the same key used originally for encryption. Let’s write a new function for decryption, called get_dec_val, using the DBMS_CRYPTO package, as follows.

    /* File on web: get_dec_val_2.sql */

    CREATE OR REPLACE FUNCTION get_dec_val (
       p_in_val      IN   VARCHAR2,

       p_key         IN   VARCHAR2,
       p_algorithm   IN   VARCHAR2 := 'AES128',
       p_iv          IN   VARCHAR2 := NULL
    )
       RETURN VARCHAR2
    IS
       l_dec_val    RAW (4000);
       l_enc_algo   PLS_INTEGER;
       l_in         RAW (4000);
       l_iv         RAW (4000);
       l_key        RAW (4000);
       l_ret        VARCHAR2 (4000);
    BEGIN
       l_enc_algo :=
          CASE p_algorithm
             WHEN 'DES'
                THEN DBMS_CRYPTO.encrypt_des
             WHEN '3DES_2KEY'
                THEN DBMS_CRYPTO.encrypt_3des_2key
             WHEN '3DES'
                THEN DBMS_CRYPTO.encrypt_3des
             WHEN 'AES128'
                THEN DBMS_CRYPTO.encrypt_aes128
             WHEN 'AES192'
                THEN DBMS_CRYPTO.encrypt_aes192
             WHEN 'AES256'
                THEN DBMS_CRYPTO.encrypt_aes256
             WHEN 'RC4'
                THEN DBMS_CRYPTO.encrypt_rc4
          END;
       l_in := hextoraw(p_in_val);
       l_iv := utl_i18n.string_to_raw (p_iv, 'AL32UTF8');
       l_key := utl_i18n.string_to_raw (p_key, 'AL32UTF8');
       l_dec_val :=
          DBMS_CRYPTO.decrypt (src      => l_in,
                               KEY      => l_key,
                               iv       => l_iv,
                               typ      =>   l_enc_algo
                                           + DBMS_CRYPTO.chain_cbc
                                           + DBMS_CRYPTO.pad_pkcs5
                              );
       l_ret := utl_i18n.raw_to_char (l_dec_val, 'AL32UTF8');
       RETURN l_ret;
    END;

Let’s test this function. To decrypt the value encrypted earlier, I can use:

    SQL> SELECT get_dec_val ('2137F30B29BE026DFE7D61A194BC34DD', '1234567890123456')
      2>  FROM DUAL
      3>  / GET_DEC_VAL('2137F30B29BE026DFE7D61A194BC34DD','1234567890123456') ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Oracle Database 12c PL/SQL Programming

Oracle Database 12c PL/SQL Programming

Michael McLaughlin
Resilient Oracle PL/SQL

Resilient Oracle PL/SQL

Stephen B. Morris
Expert PL/SQL Practices for Oracle Developers and DBAs

Expert PL/SQL Practices for Oracle Developers and DBAs

John Beresniewicz, Adrian Billington, Martin Büchi, Melanie Caffrey, Ron Crisco, Lewis Cunningham, Dominic Delmolino, Sue Harper, Torben Holm, Connor McDonald, Arup Nanda, Stephan Petit, Michael Rosenblum, Robyn Sands, Riyaj Shamsudeen

Publisher Resources

ISBN: 0596005873Errata Page