我有一个数据块,可以使用以下代码在C#中解密:
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt);
byte[] key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, iv);
var csp = new TripleDESCryptoServiceProvider();
csp.Mode = CipherMode.CBC;
csp.IV = iv;
csp.Key = key;
csp.Padding = PaddingMode.None;
var plainTextBytes = new byte[32*1024];
var decryptor = csp.CreateDecryptor();
int decr = decryptor.TransformBlock(cipherText, 0, cipherText.Length, plainTextBytes, 0);
我的主要目标是在java中做同样的事情。我试图通过这个 PasswordDerivedBytes port解决方案来做到这一点。
这是我的代码:
PasswordDeriveBytes myPass = new PasswordDeriveBytes(password, salt);
SecretKeyFactory kf;
try {
Cipher desEDE = Cipher.getInstance("DESede/CB/NoPadding");
kf = SecretKeyFactory.getInstance("DESede");
key = myPass.getBytes(192);
desEDEKey= kf.generateSecret(new DESedeKeySpec(key));
byte[] iv = DatatypeConverter.parseBase64Binary(ivText);
desEDE.init(Cipher.DECRYPT_MODE, desEDEKey, new IvParameterSpec(iv));
byte[] ct = desEDE.doFinal(DatatypeConverter.parseBase64Binary(texts));
}
问题是C#CryptDerviedKey创建的密钥不等于java中使用getBytes(192)生成的密钥。当这个键只是从C#复制到java然后一切正常,所以问题恰好在这个地方。请帮助正确地将CryptDeriveKey(" TripleDES"," SHA1",192,iv)算法移植到java;