我在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个数字。 数据和关键变量的值是从相同的十六进制字符串转换为字节。
我需要Java代码返回与C#代码相同的值。 在这一刻,下面的值是回报:
Java = {99, -104, 95, 92, 59, -75, -30, -16};
C# = {171, 58, 144, 248, 46, 146, 227, 224};
我在两个结果之间没有任何共同的价值。
答案 0 :(得分:1)
解决方案在这里:
3DES Decryption Error Invalid Key Length
我删除了MessageDigest密钥生成,并使用了以下代码:
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;
}