I have an outdated application written in PL / SQL that encrypts and decrypts data using 3DES. Now I need to perform similar encryption from a ruby application. Ultimately, the resulting hash must be decrypted by the same PL / SQL application using the existing algorithm.
The problem is that I get different encrypted results in PL / SQL and Ruby, and I don't know why.
First of all, here's how PL / SQL encryption works:
From Oracle docs about DBMS_OBFUSCATION_TOOLKIT http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_obtool.htm
"The Oracle 3DES implementation supports either a two-key or 3-key implementation in External Encryption (CBC) mode."
Functional Signature:
DBMS_OBFUSCATION_TOOLKIT.DES3Encrypt(
input_string IN VARCHAR2,
key_string IN VARCHAR2,
encrypted_string OUT VARCHAR2,
which IN PLS_INTEGER DEFAULT TwoKeyMode
iv_string IN VARCHAR2 DEFAULT NULL);
Note the parameter, which: "If = 0, (default), then TwoKeyMode is used. If = 1, then ThreeKeyMode is used." This helped me choose a cipher in the ruby version.
Here's how the application does it:
set serveroutput on;
declare
v_encrypted varchar2(100);
begin
dbms_obfuscation_toolkit.des3encrypt(
input_string => 'abcdefgh',
key_string => '16_byte_string_k',
encrypted_string => v_encrypted,
iv_string => 'xxxxxxxx');
dbms_output.put_line( lower(utl_raw.cast_to_raw(v_encrypted)) );
end;
Secondly, here is what I'm trying in Ruby:
OpenSSL :: Cipher docs:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html
OpenSSL docs to give me the name of the cipher: From http://www.openssl.org/docs/apps/enc.html
"des-ede-cbc Two key triple DES EDE in CBC mode"
require 'openssl'
cipher = OpenSSL::Cipher.new('des-ede-cbc')
cipher.encrypt
input = 'abcdefgh'
cipher.key = '16_byte_string_k'
cipher.iv = 'xxxxxxxx'
encrypted = cipher.update(input) + cipher.final
hex_representation = encrypted.unpack("H*")
puts hex_representation
As shown in the code, the ruby version computes a different hash value. What for? What needs to be changed to make them consistent?
Points I'm Not Sure About:
- Does des-ede-cbc really match what Oracle does.
- utl_raw.cast_to_raw unpack ( "H *" )
.
- encipher.final -
PL/SQL.
. , DES AES . , . , PL/SQL , ruby.