【发布时间】:2017-07-28 09:21:10
【问题描述】:
我正在尝试在 C# 中模拟 Java 的相同行为。但我没有得到想要的结果。两者生成的密钥不同。
JAVA 代码
public static String generateDecryptedKey(String secretKey, String authKey)
{
String strDecryptedKey = "";
byte[] salt = { (byte)0x09, (byte)0xD5, (byte)0xA1, (byte)0xA6, (byte)0xA3, (byte)0xA7, (byte)0xA9, (byte)0xA0 };
int iterationCount = 10;
KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
dcipher = Cipher.getInstance(key.getAlgorithm());
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
byte[] enc = Base64.decodeBase64(authKey.getBytes());
byte[] utf8 = dcipher.doFinal(enc);
strDecryptedKey = new String(utf8, "UTF-8");
return strDecryptedKey;
}
C# 代码
public static string generateDecryptedKey(string secretKey,string authKey)
{
string strDecryptedKey = string.Empty;
byte[] salt = { (byte)0x09, (byte)0xD5, (byte)0xA1, (byte)0xA6, (byte)0xA3, (byte)0xA7, (byte)0xA9, (byte)0xA0 };
int iterationCount = 10;
PKCSKeyGenerator kp = new PKCSKeyGenerator();
ICryptoTransform crypt = kp.Generate(secretKey, salt, iterationCount, 1);
var bytes = Encoding.UTF8.GetBytes(authKey);
byte[] resultBytes = crypt.TransformFinalBlock(bytes, 0, bytes.Length);
strDecryptedKey = Convert.ToBase64String(resultBytes);
return strDecryptedKey;
}
两个函数在相同输入时生成的结果是错误的。我是密码学新手,请有人解释我做错了什么。下面是我在 C# 转换中使用的 BobJanova 编写的类的链接。
https://www.codeproject.com/Articles/16450/Emulating-PBEWithMD-AndDES-Encryption-under-NET
注意:我不想透露我的 SALT 值,所以我更改了一些值。希望你能理解。
【问题讨论】:
标签: java c# encryption cryptography tripledes