尝试在 java 中解密 oracle 过程 DBMS_OBFUSCATION_TOOLKIT.DESDECRYPT
Trying to decript in java the oracle procedure DBMS_OBFUSCATION_TOOLKIT.DESDECRYPT
我正在尝试使此代码适应 java 以减少对 DB 的调用次数:
--set serveroutput on
declare
l_aux NUMBER;
l_cle RAW(9) := utl_raw.cast_to_raw('example21');
l_crypt_raw VARCHAR2(200);
l_crypt_str VARCHAR2(200);
p_txt_desencrip varchar2(200):='8387F8937F5F842F805C44B88429D2CD';
BEGIN
l_crypt_raw := utl_raw.cast_to_raw(utl_raw.cast_to_varchar2( p_txt_desencrip));
DBMS_OBFUSCATION_TOOLKIT.DESDECRYPT ( input => p_txt_desencrip
, key => l_cle
, decrypted_data => l_crypt_raw
);
l_crypt_str := utl_raw.cast_to_varchar2(l_crypt_raw);
l_aux := LENGTH(l_crypt_str);
l_crypt_str := RPAD(l_crypt_str,l_aux-ASCII(SUBSTR(l_crypt_str,l_aux)));
DBMS_OUTPUT.PUT_LINE('Decypted message->' || l_crypt_str);
END;
我已经看到解决此任务的所有尝试,但无论如何我的主要问题是我的密钥有 9 个字符。这是我的 java 代码:
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;
public class Test{
private static Cipher ecipher;
private static Cipher dcipher;
private static SecretKey key;
public static void main(String[] args) {
try {
String clave = "example21";
// generate secret key using DES algorithm
SecretKey key2 = new SecretKeySpec(clave.getBytes(), 0, 9, "DES");
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
// initialize the ciphers with the given key
ecipher.init(Cipher.ENCRYPT_MODE, key2);
dcipher.init(Cipher.DECRYPT_MODE, key2);
String encrypted = encrypt("text to encrypt");
System.out.println(encrypted);
String decrypted = decrypt(encrypted);
System.out.println("Decrypted: " + decrypted);
}catch (NoSuchAlgorithmException e) {
System.out.println("No Such Algorithm:" + e.getMessage());
return;
}
catch (NoSuchPaddingException e) {
System.out.println("No Such Padding:" + e.getMessage());
return;
}
catch (InvalidKeyException e) {
System.out.println("Invalid Key:" + e.getMessage());
return;
}
}
public static String encrypt(String str) {
try {
// encode the string into a sequence of bytes using the named charset
// storing the result into a new byte array.
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
// encode to base64
enc = BASE64EncoderStream.encode(enc);
return new String(enc);
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String str) {
try {
// decode with base64 to get bytes
byte[] dec = BASE64DecoderStream.decode(str.getBytes());
byte[] utf8 = dcipher.doFinal(dec);
// create new string based on the specified charset
return new String(utf8, "UTF8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我尝试了不同的方法和算法,但我总是收到错误消息“无效的 Key:Wrong 密钥大小”...
有人对我应该尝试什么有什么建议吗?
提前致谢!
- 首先,
DBMS_OBFUSCATION_TOOLKIT
已弃用,不应使用。
- 其次,
DES
20多年前就坏了,不应该再用了
关于你的问题。的文档
DBMS_OBFUSCATION_TOOLKIT.DESDECRYPT
关于关键参数有如下说法:
If the key length is missing or is less than 8 bytes, then the
procedure raises the error ORA-28234 "Key length too short." Note that
if larger keys are used, extra bytes are ignored. So a 9-byte key will
not generate an exception.
所以 Java 只是稍微验证一下密钥。
我正在尝试使此代码适应 java 以减少对 DB 的调用次数:
--set serveroutput on
declare
l_aux NUMBER;
l_cle RAW(9) := utl_raw.cast_to_raw('example21');
l_crypt_raw VARCHAR2(200);
l_crypt_str VARCHAR2(200);
p_txt_desencrip varchar2(200):='8387F8937F5F842F805C44B88429D2CD';
BEGIN
l_crypt_raw := utl_raw.cast_to_raw(utl_raw.cast_to_varchar2( p_txt_desencrip));
DBMS_OBFUSCATION_TOOLKIT.DESDECRYPT ( input => p_txt_desencrip
, key => l_cle
, decrypted_data => l_crypt_raw
);
l_crypt_str := utl_raw.cast_to_varchar2(l_crypt_raw);
l_aux := LENGTH(l_crypt_str);
l_crypt_str := RPAD(l_crypt_str,l_aux-ASCII(SUBSTR(l_crypt_str,l_aux)));
DBMS_OUTPUT.PUT_LINE('Decypted message->' || l_crypt_str);
END;
我已经看到解决此任务的所有尝试,但无论如何我的主要问题是我的密钥有 9 个字符。这是我的 java 代码:
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;
public class Test{
private static Cipher ecipher;
private static Cipher dcipher;
private static SecretKey key;
public static void main(String[] args) {
try {
String clave = "example21";
// generate secret key using DES algorithm
SecretKey key2 = new SecretKeySpec(clave.getBytes(), 0, 9, "DES");
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
// initialize the ciphers with the given key
ecipher.init(Cipher.ENCRYPT_MODE, key2);
dcipher.init(Cipher.DECRYPT_MODE, key2);
String encrypted = encrypt("text to encrypt");
System.out.println(encrypted);
String decrypted = decrypt(encrypted);
System.out.println("Decrypted: " + decrypted);
}catch (NoSuchAlgorithmException e) {
System.out.println("No Such Algorithm:" + e.getMessage());
return;
}
catch (NoSuchPaddingException e) {
System.out.println("No Such Padding:" + e.getMessage());
return;
}
catch (InvalidKeyException e) {
System.out.println("Invalid Key:" + e.getMessage());
return;
}
}
public static String encrypt(String str) {
try {
// encode the string into a sequence of bytes using the named charset
// storing the result into a new byte array.
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
// encode to base64
enc = BASE64EncoderStream.encode(enc);
return new String(enc);
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String str) {
try {
// decode with base64 to get bytes
byte[] dec = BASE64DecoderStream.decode(str.getBytes());
byte[] utf8 = dcipher.doFinal(dec);
// create new string based on the specified charset
return new String(utf8, "UTF8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我尝试了不同的方法和算法,但我总是收到错误消息“无效的 Key:Wrong 密钥大小”...
有人对我应该尝试什么有什么建议吗?
提前致谢!
- 首先,
DBMS_OBFUSCATION_TOOLKIT
已弃用,不应使用。 - 其次,
DES
20多年前就坏了,不应该再用了
关于你的问题。的文档
DBMS_OBFUSCATION_TOOLKIT.DESDECRYPT
关于关键参数有如下说法:
If the key length is missing or is less than 8 bytes, then the procedure raises the error ORA-28234 "Key length too short." Note that if larger keys are used, extra bytes are ignored. So a 9-byte key will not generate an exception.
所以 Java 只是稍微验证一下密钥。