java - 3DES 加密 Oracle/JAVA 等效

标签 java oracle encryption tripledes

我正在尝试将 oracle 方法 dbms_obfuscation_toolkit.DES3Encrypt 迁移到 Java 函数。我的问题是我在两个场景中都没有得到相同的加密值。

对于 Oracle 中的这个过程:

set serveroutput on;
declare
input raw(128);
encrypted raw(2048);
cadena varchar2(60);
begin
dbms_obfuscation_toolkit.DES3Encrypt(
input => utl_raw.cast_to_raw('TESTDATATESTDATATESTDATA'), 
key => utl_raw.cast_to_raw('GD6GTT56HKY4HGF6FH3JG9J5F62FT1'), 
encrypted_data => encrypted
);
dbms_output.put_line(rawtohex(encrypted));
end;

我得到这个输出:

8A2E6792E39B0C850377F9A0E054033963F979E4A3FBA25B

但是,对于这个 Java 类:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;


public class TripleDes2 
{
    private static final String PLAIN_TEXT = "TESTDATATESTDATATESTDATA";
    private static final String SHARED_KEY = "GD6GTT56HKY4HGF6FH3JG9J5F62FT1";

   public static void main(String args []) throws Exception

{

    String algorithm = "DESede";
    String transformation = "DESede/CBC/PKCS5Padding";

    byte[] keyValue = SHARED_KEY.getBytes("UTF-8");

    DESedeKeySpec keySpec = new DESedeKeySpec(keyValue);

    IvParameterSpec iv = new IvParameterSpec(new byte[8]);

    SecretKey key = SecretKeyFactory.getInstance(algorithm).generateSecret(keySpec);
    Cipher encrypter = Cipher.getInstance(transformation);
    encrypter.init(Cipher.ENCRYPT_MODE, key, iv);

    byte[] input = PLAIN_TEXT.getBytes("UTF-8");

    byte[] encrypted = encrypter.doFinal(input);

    System.out.println(new String(Hex.encodeHex(encrypted)).toUpperCase());
}
}

我得到这个值:

82EBC149F298DE55E4FF1540615E60ACDB7743FE79CD2CF4BB6FD232893F83D0

我不确定我的 Java 代码是否正确。你能帮帮我吗?

非常感谢。

最佳答案

这是我的最终代码,它就像一个魅力:

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class TripleDes3 {
    private Cipher cipher = null;
    private SecretKey key = null;
    private byte[] bytes = null;
    private IvParameterSpec iv = null;

    public static void main(String[] args) throws Exception {
        try {
            String hexKey = "GD6GTT56HKY4HGF6FH3JG9J5";
            //TripleDes3 encryptor = new TripleDes3(new String(Hex.decodeHex(hexKey.toCharArray())));
            TripleDes3 encryptor = new TripleDes3(hexKey);
            String original = "ABC";
            System.out.println("Oringal: \"" + original + "\"");

            String enc = encryptor.encrypt(original);
            System.out.println("Encrypted: \"" + enc.toUpperCase() + "\"");

            String dec = encryptor.decrypt(enc);
            System.out.println("Decrypted: \"" + dec.toUpperCase() + "\"");

            if (dec.equals(original)) {
                System.out.println("Encryption ==> Decryption Successful");
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.toString());
        }
    }

    public TripleDes3(String encryptionKey) throws GeneralSecurityException, DecoderException {
        cipher = Cipher.getInstance("DESede/CBC/NoPadding");
        try {
            key = new SecretKeySpec(encryptionKey.getBytes("ISO8859_15"), "DESede");
            iv = new IvParameterSpec(Hex.decodeHex("0123456789abcdef".toCharArray()));

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String encrypt(String input) throws GeneralSecurityException, UnsupportedEncodingException {
        bytes = input.getBytes("ISO8859_15");
        bytes = Arrays.copyOf(bytes, ((bytes.length+7)/8)*8);
        return new String(Hex.encodeHex(encryptB(bytes)));
    }

    public String decrypt(String input) throws GeneralSecurityException, DecoderException, UnsupportedEncodingException {
        bytes = Hex.decodeHex(input.toCharArray());
        String decrypted = new String(decryptB(bytes), "ISO8859_15");
        if (decrypted.indexOf((char) 0) > 0) {
            decrypted = decrypted.substring(0, decrypted.indexOf((char) 0));
        }
        return decrypted;
    }

    public byte[] encryptB(byte[] bytes) throws GeneralSecurityException {
        cipher.init(Cipher.ENCRYPT_MODE, (Key) key, iv);
        return cipher.doFinal(bytes);
    }

    public byte[] decryptB(byte[] bytes) throws GeneralSecurityException {
        cipher.init(Cipher.DECRYPT_MODE, (Key) key, iv);
        return cipher.doFinal(bytes);
    }
}

这是 Oracle 代码:

   DECLARE
      v_data   VARCHAR2(255);
      v_retval RAW(255);
      p_str   VARCHAR2(255);
      p_key RAW(255);
   BEGIN
      p_str := 'ABC';
      p_key := utl_raw.cast_to_raw('GD6GTT56HKY4HGF6FH3JG9J5F62FT1');
      v_data := RPAD(p_str, CEIL(LENGTH(p_str)/8)*8, CHR(0));
      dbms_obfuscation_toolkit.DES3Encrypt
         (
            input => utl_raw.cast_to_raw(v_data),
            key   => p_key,
            which => 1,
            encrypted_data => v_retval
         );
      dbms_output.put_line(v_retval);
   END;

关于java - 3DES 加密 Oracle/JAVA 等效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590279/

相关文章:

java - 我可以使用 Maven 构建多个项目,然后通过 m2eclipse 运行 Jetty 吗?

java - 插入查询仅插入数据库表中结果集中的第一行

sql - 如果存在多个表的记录,如何返回 bool 值

sql - 在 oracle 中的存储过程的源代码 (DDL) 中查找字符串

javascript - 删除逗号并使其成为字符串?

ssl - ejabberd s2s 到 GTalk/Hangout with TLS

java - 按行提取子字符串

oracle - Oracle_SID 是否与 Oracle 上的登录名相同?

kotlin - Kotlin-如何实现围栏密码?

java - 将Java中的addAll函数制作一个副本