Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Suman.

Asked: March 22, 2016 - 10:43 am UTC

Last updated: July 05, 2019 - 5:35 am UTC

Version: Oracle 10g

Viewed 1000+ times

You Asked

I have a requirement to decrypt a password coming in source extract file in our project. Currently the same thing is happening through a SQL server stored procedure. But the SQL server will be decommissioned in near future and the whole thing needs to be done in oracle. I have tried the approach described below:-

I have tried to replicate the sql server stored procedure in Oracle. It is being able to decrypt password for the character but failed to do the same for Numerals. Please find below the anonymous block for your reference.

Existing SQL Server stored procedure
------------------------------------

CREATE PROCEDURE spSE_EncryptDecryptPwd( @encrypted_pwd_vc varchar(100) output, @password_vc varchar(100))
AS
DECLARE @encrypted_password_vc varchar(100),
@index_i int, @char_c char(1)
if @password_vc is null
BEGIN
select @encrypted_password_vc = NULL
END
else
BEGIN
select @encrypted_password_vc = ''
select @index_i = 1
WHILE (1=1)
BEGIN
select @char_c = ''
select @char_c = SUBSTRING(@password_vc, @index_i, 1)
if (ltrim(@char_c) = '')
BEGIN
BREAK
END
select @encrypted_password_vc = @encrypted_password_vc +
CHAR(ASCII(@char_c) ^ 239)
select @index_i = @index_i + 1
END
SELECT @encrypted_pwd_vc = @encrypted_password_vc
END


My replicated Oracle Stored Procedure
-------------------------------------
declare
encrypted_password_vc varchar(100);
password_vc varchar(100);
index_i int;
char_c varchar2(100);
ascii_val int;
dec_char_var int;

begin
password_vc :='¢Š›ƒ†‰ŠÝßßÜ';
if password_vc is null THEN
begin
encrypted_password_vc := null;
END;
else
begin
encrypted_password_vc := '';
index_i := 1;
WHILE (index_i<=length(password_vc))
loop
char_c := SUBSTR(password_vc, index_i, 1);
ascii_val := ascii(CONVERT(char_c,'WE8MSWIN1252'));
dec_char_var :=(ascii_val+239)-BITAND(ascii_val,239)*2;
encrypted_password_vc := encrypted_password_vc || chr(dec_char_var);
index_i := index_i + 1;
end loop;
dbms_output.put_line(encrypted_password_vc);

end;
END IF;
end;

It is giving output "Metlife" in oracle whereas the expected output(which is correctly showing in sql server) is "Metlife2003"


The source encryption algorithm is not known. However, the encryption can easily be decrypted by the sql server sp but not be oracle replica. Also, the password should return ‘Metlife2003’ instead only ‘Metlife’.

It will be great if you can provide a solution on this.

Thanks in Advance.

Regards,
Suman Roychowdhury


and Connor said...

The conversion of the last four characters fails when run in SQL:

ORA-29275: partial multibyte character


So it's related to that.

How did you decide to use the character set WE8MSWIN1252?

What's the algorithm for encryption/decryption? Can you explain it in words?

--

I get the following output from the PL/SQL:

Metlife

What exactly is the output you're expecting from the block?

I don't have any copies of SQL Server handy to verify ;)

Please update this question explaining why the Oracle version doesn't work.

=====================

Addenda March 26:

I ran your code in SQL Developer - and it looks fine

SQLDEV_DECRYPT

So if you're not getting that on your system, it's unlikely to be your algorithm, its more likely a characterset conversion issue on input or output.

Cheers,
Connor

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Preferrable character set not suggested

Suman Roychowdhury, March 30, 2016 - 6:46 am UTC

If possible, please provide me the preferable character set that I should use in my code provided in my question.
Connor McDonald
March 31, 2016 - 2:59 am UTC

My general recommendation nowadays is that new databases should be created with utf characeterset support (eg AL32UTF8) so you know that you are covered for just about anything now and in future.

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database