Hi,
I'm writing a java app that needs to retrieve encrypted values in a table, and decrypt them on the java side. The oracle side encryption and decryption works fine, but I'm getting null on the java side.
Here's Oracle code that works to decrypt the value:
Hre's my java code to read these values from the table and decrypt
And here's the DesEncrypter class I found:
The values from the table are coming across to java correctly. I just can't get the decryption to work. Any have a suggestion of what I should try?
Thanks in advance!
I'm writing a java app that needs to retrieve encrypted values in a table, and decrypt them on the java side. The oracle side encryption and decryption works fine, but I'm getting null on the java side.
Here's Oracle code that works to decrypt the value:
Code:
SELECT CONVERT(key_1, 'WE8ISO8859P1', 'UTF8'),
CONVERT(key_2, 'WE8ISO8859P1', 'UTF8')
INTO v_key1, v_key2
FROM encrypted_keys;
v_result := dbms_obfuscation_toolkit.desdecrypt(input_string => v_key2,
key_string => v_key1);
Hre's my java code to read these values from the table and decrypt
Code:
Connection conn = getConnection();
PreparedStatement ps = null;
String key1, key2, decrypted;
key1 = key2 = decrypted = null;
try {
ps = conn.prepareStatement("SELECT CONVERT(key_1, 'WE8ISO8859P1', 'UTF8'), CONVERT(key_2, 'WE8ISO8859P1', 'UTF8') from encrypted_keys");
ResultSet rs = ps.executeQuery();
rs.next();
key1 = rs.getString(1);
key2 = rs.getString(2);
rs.close();
ps.close();
conn.close();
DesEncrypter encrypter = new DesEncrypter(key1);
decrypted = encrypter.decrypt(key2);
} catch (SQLException e) {}
And here's the DesEncrypter class I found:
Code:
import java.io.UnsupportedEncodingException;
import java.security.spec.KeySpec;
import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
public class DesEncrypter {
Cipher ecipher;
Cipher dcipher;
DesEncrypter(String passPhrase) {
try {
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray());
SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.spec.InvalidKeySpecException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
}
}
public String encrypt(String str) {
try {
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) { return e.toString();
} catch (IllegalBlockSizeException e) { return e.toString();
} catch (UnsupportedEncodingException e) { return e.toString();
} catch (java.io.IOException e) { return e.toString();
}
}
public String decrypt(String str) {
try {
byte[] dec = str.getBytes();
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
} catch (javax.crypto.BadPaddingException e) { return e.toString();
} catch (IllegalBlockSizeException e) { return e.toString();
} catch (UnsupportedEncodingException e) { return e.toString();
} catch (java.io.IOException e) { return e.toString();
}
}
}
The values from the table are coming across to java correctly. I just can't get the decryption to work. Any have a suggestion of what I should try?
Thanks in advance!