Java中重复的Oracle DES加密 [英] Duplicate Oracle DES encrypting in Java
问题描述
我的问题是数据库已经以某种方式加密数据,我需要Java代码来复制该功能,以便一个系统加密的文本可以被其他的,反之亦然。
我希望加密与数据库已经在做的事情兼容,但找不到描述Oracle正在做什么的文档。如何在Java中复制?
dbms_obfuscation_toolkit.DESEncrypt(
input_string => v_string,
key_string => key_string,
encrypted_string => encrypted_string);
返回UTL_RAW.CAST_TO_RAW(encrypted_string);
无论我尝试什么,似乎Java DES加密与Oracle不同。 p>
我发现这样做:
KeySpec ks = new DESKeySpec(new byte [] {'s','e','c','r','e','t','!','!'})
SecretKeyFactory skf = SecretKeyFactory.getInstance(DES);
SecretKey sk = skf.generateSecret(ks);
密码c = Cipher.getInstance(DES / CBC / NoPadding);
IvParameterSpec ips = new IvParameterSpec(new byte [] {0,0,0,0,0,0,0,0});
c.init(Cipher.ENCRYPT,sk,ips);
//或
c.init(Cipher.DECRYPT,sk,ips);
缺少的部分是Initialization Vector(ips),它必须为8位。当您在Java中使用null时,您会得到不同的东西。
I recently asked a question about Oracle Encryption. Along the way to finding a solution for myself I decided to move the encryption (well, obfuscation) to the application side for certain tasks.
My problem is that the database is already encrypting data a certain way and I need Java code to duplicate that functionality, so that text encrypted by one system can be decrypted by the other and vice versa.
I want the encryption to be compatible with what the DB was already doing but couldn't find the documentation that describes exactly what Oracle is doing. How do I replicate this in Java?
dbms_obfuscation_toolkit.DESEncrypt(
input_string => v_string,
key_string => key_string,
encrypted_string => encrypted_string );
RETURN UTL_RAW.CAST_TO_RAW(encrypted_string);
No matter what I try, it seems as if the Java DES encryption is different than Oracle's.
I found this works:
KeySpec ks = new DESKeySpec(new byte[] {'s','e','c','r','e','t','!','!'});
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
IvParameterSpec ips = new IvParameterSpec(new byte[] {0,0,0,0,0,0,0,0});
c.init(Cipher.ENCRYPT, sk, ips);
// or
c.init(Cipher.DECRYPT, sk, ips);
The missing piece was the Initialization Vector (ips) which must be 8 zeros. When you use null in Java you get something different.
这篇关于Java中重复的Oracle DES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!