java - 从 C# 到 JAVA 的 3DES 加密

标签 java c# encryption 3des

我有这段 C# 代码,我需要将其迁移到 Java。 我需要使用 3DES 加密。这是强制性的。

C#:

public void test() 
{
    string sKSN = "ffff1234560006800010";
    string sBDK = "E08A46B616230152230DB9C8DF94C75E";
    byte[] dikKSN = new byte[10];
    byte[] KSN8 = new byte[8];
    byte[] BDK = new byte[16];
    byte[] lKey = new byte[8];
    byte[] rKey = new byte[8];
    string retKey = string.Empty;
    string lgTxt = string.Empty;
    dikKSN = this.FromHex(sKSN);  // convert hex to byte array
    BDK = this.FromHex(sBDK); // convert hex to byte array
    KSN8 = this.CopyByte8(dikKSN); //use the first 8 values
    lKey = this.TDESEncrypt(KSN8, BDK);
}
private byte[] TDESEncrypt(byte[] data, byte[] key) 
{
    byte[] retVal = null;
    byte[] IV = new byte[16];
    System.IO.MemoryStream ms = null;
    System.Security.Cryptography.TripleDES tDES = System.Security.Cryptography.TripleDES.Create();
    tDES.BlockSize = 64;
    tDES.KeySize = 128;
    tDES.Padding = System.Security.Cryptography.PaddingMode.None;
    tDES.Mode = System.Security.Cryptography.CipherMode.ECB;
    System.Security.Cryptography.CryptoStream csEncrypt = null;
    try
    {
        ms = new System.IO.MemoryStream(data);
        csEncrypt = new System.Security.Cryptography.CryptoStream(ms, tDES.CreateEncryptor(key, IV), System.Security.Cryptography.CryptoStreamMode.Write);
        retVal = new byte[data.Length];
        csEncrypt.Write(data, 0, data.Length);
        retVal = ms.ToArray();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        ms.Close();
    }
    return retVal;
}

JAVA:

public void test() {
    String sKSN = "ffff1234560006800010";
    String sBDK = "E08A46B616230152230DB9C8DF94C75E";
    byte[] dikKSN = new byte[10];
    byte[] KSN8 = new byte[8];
    byte[] BDK = new byte[16];
    byte[] lKey = new byte[8];
    byte[] rKey = new byte[8];
    String retKey = "";
    String lgTxt = "";
    dikKSN = this.fromHex(sKSN);  // convert hex to byte array
    BDK = this.fromHex(sBDK); // convert hex to byte array
    KSN8 = this.copyByte8(dikKSN); //use the first 8 values
    lKey = this.tDESEncrypt(KSN8, BDK);
}
private byte[] tDESEncrypt(byte[] plainTextBytes, byte[] kb) {
  byte[] cipherText = null;
  try {
    final MessageDigest md = MessageDigest.getInstance("md5");
    final byte[] digestOfPassword = md.digest(kb);
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
      keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    final Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding"); 
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    cipherText = cipher.doFinal(plainTextBytes);
  } catch (Exception ex) {
    ex.printStackTrace();
  }
  return cipherText;
}

执行 desEncrypt 方法时进行测试:

C#

data = {255, 255, 18, 52, 86, 0, 6, 128};
key = {224, 138, 70, 182, 22, 35, 1, 82, 35, 13, 185, 200, 223, 148, 199, 94};

JAVA

plainBytes = {-1, -1, 18, 52, 86, 0, 6, -128};
kb = {-32, -118, 70, -74, 22, 35, 1, 82, 35, 13, -71, -56, -33, -108, -57, 94}

我认为我的主要问题是在 Java 中字节介于 -128 和 127 之间,而在 C# 中字节原语可以包含 255 数字。 data和key变量的值都是由同一个十六进制字符串转换为字节得到的。

我需要 Java 代码返回与 C# 代码相同的值。 此时,以下值为返回值:

Java = {99, -104, 95, 92, 59, -75, -30, -16};
C# = {171, 58, 144, 248, 46, 146, 227, 224};

这两个结果之间没有任何共同点。

最佳答案

解决方案在这里:

3DES Decryption Error Invalid Key Length

我删除了 MessageDigest key 生成并使用了以下代码:

private byte[] genTwoKey3DES(byte[] key) {
    byte[] keyAux;
    if (key.length == 16) {
        keyAux = new byte[24];
        System.arraycopy(key, 0, keyAux, 0, 16);
        System.arraycopy(key, 0, keyAux, 16, 8);
    } else {
        keyAux = key;
    }
    return keyAux;
} 

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

相关文章:

c# - 将c#解密方法转换为android

java - 密码未初始化以进行加密/解密

java - org.powermock.reflect.exceptions.TooManyConstructorsFoundException : Several matching constructors found

java - Apache Shiro LDAP 配置(两步身份验证)

java - 如何在同一 Activity 中从一个布局切换回父布局

c# - 添加到 "open recent files"

c# - 如何使小数变量保留/显示小数位,即使小数位仅为零?

java - 检查 Firebase ui 查询类是否未返回任何文档

c# - 在 C# 中初始化锯齿状数组会导致索引超出范围异常

encryption - 使用 Crypto++ 连接 Base64 字符串中的 IV 和数据?