Home » Developer & Programmer » JDeveloper, Java & XML » about ORACLE desencrypt and Java Encrypt function
about ORACLE desencrypt and Java Encrypt function [message #91949] Mon, 01 December 2003 22:31
Sheldon Tien
Messages: 4
Registered: May 2003
Junior Member
Hi all,
we have some data encrypted using oracle sys.dbms_obfuscation_toolkit.desencrypt function, and we want to decrypt the data using java decrypt functions. Then we have met a probem , with the same text and key , oracle and java give out defrerent encrypted text.

we use this ORACEL function to encrypt the text:
create or replace function testDesCrypt
(
p_str VARCHAR2 -- text to be encrypted
)
RETURN VARCHAR2 --encryted text
IS
v_str VARCHAR2 (36);
v_str_enc VARCHAR2 (64) := '';
v_enc_key VARCHAR2 (32);
BEGIN
v_enc_key := '12345678';
sys.dbms_obfuscation_toolkit.desencrypt (
input_string=> p_str,
key_string=> v_enc_key,
encrypted_string=> v_str_enc
);
RETURN UTL_RAW.cast_to_raw (v_str_enc);
END;

SQL> select testDESCrypt('12345678901234567890123456789012') from dual;

TESTDESCRYPT('1234567890123456
--------------------------------------------------------------------------------
775456C67B5B25A765A9359715E2CC6C51F8558BB885A4BEB18ADE4D7B3FD3FA

And We use this java program to encrypt the text
package com.massky.util;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;

/**
*

Title:

*

Description:

*

Copyright: Copyright (c) 2003

*

Company:

* @author not attributable
* @version 1.0
*/

public class DESCrypto {
public DESCrypto() {
}

private static Cipher m_cipher;
public static String DESEncrypt(String strData, String strKey)
{
try
{
if (m_cipher==null)
{
m_cipher = Cipher.getInstance("DES");
}
DESKeySpec keyspec = new DESKeySpec(strKey.getBytes());
SecretKeyFactory keyfactroy = SecretKeyFactory.getInstance("DES");
Key key = keyfactroy.generateSecret(keyspec);
m_cipher.init(Cipher.ENCRYPT_MODE, key);
byte [[]] cipherData = m_cipher.doFinal(strData.getBytes());
return byteToBinText(cipherData);
}
catch (Throwable e)
{
e.printStackTrace();
}
return null;
}

public static String byteToBinText(byte[[]] byteData)
{
String strData = "";
int nLength = byteData.length;
for (int i=0; i<nLength; i++)
{
int nValue = byteData[[i]];
if (nValue<0)
{
nValue = 255+nValue;
}

String str = Integer.toHexString(nValue);
if (str.length()==1)
{
str = "0" + str;
}
strData += str;
}
return strData;
}

public static void main(String args[[]])
{
String strCleanText = "12345678901234567890123456789012";
String strKey = "12345678";
String strCipherText = DESEncrypt(strCleanText,strKey);
System.out.println("Clean text = '"+ strCleanText+"',length="+ new Integer(strCleanText.length()).toString());
System.out.println(" key = '"+ strKey+"',length="+ new Integer(strKey.length()).toString());
System.out.println("cipherText = '" + strCipherText + "',length="+ new Integer(strCipherText.length()/2).toString());
}

}
the output is :
Clean text = '12345678901234567890123456789012',length=32
key = '12345678',length=8
cipherText = '95cf028778d48b886925a62e20b726f6c49b3c60b9a0e6e5fcbb407244ecb5b3fdb859b6d3642fca',length=40

you see, the encrypt result is deferent , Can somebody tell me how to solve this problem? thank you
Previous Topic: XML insert from forms
Next Topic: ORA-01729: for XMLType
Goto Forum:
  


Current Time: Sat Jul 31 16:53:46 CDT 2021