首页 > 解决方案 > 3DES , Java 代码结果与 3des 在线工具和 oracle 数据库脚本产生不同的结果

问题描述

使用 java 应用 3des 加密时,与 3des 在线工具和 oracle DB 脚本相比,java 代码生成错误的结果

3des加密,Java

  SET SERVEROUTPUT ON;

    declare
    l_mod_ofb pls_integer;  
     l_mod_ecb pls_integer;
      v_key raw(32);   
      v_iv  raw(16);
      v_test_in raw(16);   v_ciphertext raw(16);

    begin
      l_mod_ecb := dbms_crypto.ENCRYPT_3DES_2KEY + dbms_crypto.CHAIN_CBC  + DBMS_CRYPTO.PAD_NONE ;
      v_key      :=   hextoraw  ('1FB3F48A6D51832CE91C1C734554086D');
      v_iv       :=   hextoraw  ('041234FFFFFFFFFF');                
      v_test_in  :=   hextoraw  ('0000211111111110');                

      v_ciphertext := dbms_crypto.encrypt(src => v_test_in,
                                          TYP => l_mod_ecb,
                                          key => v_key,
                                          iv => v_iv);

      dbms_output.put_line(' Ciphertext  : '||rawtohex(v_ciphertext));

    end;

--------------------Java代码

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class TripleDes {

private static String SHARED_KEY = "1FB3F48A6D51832CE91C1C734554086D";
private static String ivString = "041234FFFFFFFFFF";
private static String valueStr = "0000211111111110";
static final String HEXES = "041217DB6AEFF7CF";

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

    TripleDes tr = new TripleDes();
    byte[] plain = Hex.decodeHex(valueStr.toCharArray());
    byte[] codedtext = tr.encrypt(plain);
    System.out.println("Encrypted Result = " + getHex(codedtext));
}



public byte[] encrypt(byte[] plainTextBytes) throws Exception {
    TripleDes tr = new TripleDes();
    byte[] keyValue = getKey(Hex.decodeHex(SHARED_KEY.toCharArray()));
    System.out.println(Hex.encodeHex(keyValue));
    IvParameterSpec iv = new IvParameterSpec(Hex.decodeHex(ivString.toCharArray()));
    //DESede
    final SecretKey key = new SecretKeySpec(keyValue, "DESede");
    final Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    final byte[] cipherText = cipher.doFinal(plainTextBytes);
    return cipherText;
}

public static byte[] getKey(byte[] keyBytes) {
    byte[] key;
    if (keyBytes.length == 16) {
        key = new byte[24];
        System.arraycopy(keyBytes, 0, key, 0, 16);
        System.arraycopy(keyBytes, 0, key, 16, 8);
    } else {
        key = keyBytes;
    }

    return key;
}

public static String getHex(byte[] raw) {
    if (raw == null) {
        return null;
    }
    final StringBuilder hex = new StringBuilder(2 * raw.length);
    for (final byte b : raw) {
        hex.append(HEXES.charAt((b & 0xF0) >> 4))
                .append(HEXES.charAt((b & 0x0F)));
    }
    return hex.toString();
}
}

--------------- 在线工具使用以下参数

Input type: Text
Input text:(hex)0000211111111110
Function: 3DES  
Mode: CBS   
Key: (hex)1FB3F48A6D51832CE91C1C734554086D1FB3F48A6D51832C
Init. vector:   041234FFFFFFFFFF

标签: javaoracle3des

解决方案


  • HEXES 变量没有正确的值,因此将其替换为 0123456789ABCDEF

  • byte[] plain = Hex.decodeHex(valueStr.toCharArray()); 错误的赋值替换为 DatatypeConverter.parseHexBinary(valueStr);


推荐阅读