3DES encrypt from C# to JAVA

1k views Asked by At

I have this code in C# and I need to migrate it to Java. I need to use 3DES encryptation. It's mandatory.

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;
}

Test when I execute desEncrypt method:

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}

I think that my main problem is in Java that a byte is between -128 and 127 and in C# the byte primitive can contain the 255 number. The values of data and key variables were obtained from conversion from the same hexadecimal string to byte.

I need that the Java code returns the same value than C# code. In this moment, the values below are the returns:

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

I don't have any value in common between both results.

1

There are 1 answers

0
dani77 On BEST ANSWER

The solution is here:

3DES Decryption Error Invalid Key Length

I removed the MessageDigest key generation and I used this code:

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;
}