我有一个用Java编写的身份验证过程,它将字符串加密到MD5并生成一个字符串,只占用该字符串的前8位数字。 在此示例中,生成的字符串为“89a5c474”。
之后我有下面这段Java代码,其中我使用了TripleDesEncryption。
public static byte[] encrypt(byte[] keybyte, byte[] src) throws NoSuchAlgorithmException, NoSuchPaddingException, Exception
{
System.out.println("Key Byte " + byte2hex(keybyte) + "Key Byte Array" + keybyte);
System.out.println("Key String " + byte2hex(src));
SecretKey deskey = new SecretKeySpec(keybyte, DESede);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
}
public static void main(String[] args)
{
final byte[] rawKey = "89a5c474".getBytes();
final byte[] keyBytes = new byte[24];
for (int i = 0; i <rawKey.length; i++)
{
keyBytes[i] = rawKey[i];
}
for (int i = rawKey.length; i <keyBytes.length; i++)
{
keyBytes[i] = (byte)0;
}
String szSrc = "20126303$4A6D9BD0DDD094B76C111577A49EB87A$Guest$PC$193.92.123.5$$Reserved$CTC";
byte[] encoded = null;
try
{
encoded = encrypt(keyBytes, szSrc.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
}
}
导致加密字节的字符串表示形式,我能够成功通过平台进行身份验证。但是,由于我必须将代码部署到C#中的控制台应用程序中,因此我尝试复制上述代码无效,并返回错误“指定密钥是'TripleDES'的已知弱密钥,无法使用。”
public static string AuthenticatePassword(string token, string hashPassword)
{
byte[] rawKey = UTF8Encoding.UTF8.GetBytes(hashPassword);
byte[] keyBytes = new byte[24];
for (var i = 0; i < rawKey.Length; ++i)
{
keyBytes[i] = rawKey[i];
}
for (int i = rawKey.Length; i < keyBytes.Length; i++)
{
keyBytes[i] = 0;
}
string keyString = "20126303$" + token + "$Guest$PC$193.92.123.5$$Reserved$CTC";
return Encrypt(keyBytes, System.Text.Encoding.ASCII.GetBytes(keyString), rawKey, keyString);
}
public static string Encrypt(byte[] keyBytes, byte[] keyString)
{
try
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = keyBytes;
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.PKCS7;
ICryptoTransform ic = des.CreateEncryptor();
byte[] enc = ic.TransformFinalBlock(keyString, 0, keyString.Length);
}
catch (Exception e)
{
Console.WriteLine("[Encryption Error] {0}", e.Message);
}
return string.Join(string.Empty, enc.Select(x => x.ToString("X2")));
}
我搜索了几个来源,我偶然发现了这个解决方法,应用程序成功地设法生成了一个密钥,但不幸的是它与Java产生的代码不相同。
TripleDESCryptoServiceProvider sm = new TripleDESCryptoServiceProvider();
MethodInfo mi = sm.GetType().GetMethod("_NewEncryptor", BindingFlags.NonPublic | BindingFlags.Instance);
object[] Par = { EmptyKey, CipherMode.ECB, keyBytes, sm.FeedbackSize, 0 };
ICryptoTransform trans = mi.Invoke(sm, Par) as ICryptoTransform;
byte[] enc = trans.TransformFinalBlock(keyString, 0, keyString.Length);
Java = d68d8423eb01421e8f23c118d3aef6a6998d8f2a62ceb697377195aa979fe5e97141454716e6d6b41c56d0af296bc4d6ab2979c7d9233898baef5c9f38fa9fd286d8a6c2a2a4b6697d1eb7c
C#= FF9772125DC1E3A4C9B63DFD429FB3CDA43732331025F9B73A092A942121F6869C372AE40B0DB1991DB0FD04CE5924EB213B8F303721C79F8F4CCA384711B7E2ADCC862E0003E18EF3CC0DA2CD4B7488
答案 0 :(得分:1)
我设法绕过并使用Java中使用的函数和构造函数在C#上重新创建3DES加密。对于遇到此问题的所有人,您可以使用Bouncy Castle的程序集并按照这些链接获取更多信息:http://www.bouncycastle.org/csharp/和http://www.go4expert.com/articles/bouncy-castle-net-implementation-triple-t24829/