以下是来自Java的代码:
public static String encrypt(String strToEncrypt)
{
try
{
String secretKey = "1234567890123456";
DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
BASE64Encoder base64encoder = new BASE64Encoder();
byte[] cleartext = strToEncrypt.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
String encrypted = base64encoder.encode(cipher.doFinal(cleartext));
return encrypted;
}
catch (Exception e)
{
return e.getMessage();
}
}
如何在C#中解密?尝试了解决方案:
Encrypt in java and Decrypt in C# For AES 256 bit
但它不起作用。
答案 0 :(得分:2)
基本上,由于您没有向Cipher.Init调用提供初始化向量(IV),因此无法根据您提供的代码可靠地解密该输出。因此,使用随机IV。
以上问题可能在纠正上述问题时起作用。您需要使用正确的IV替换null。
public static string Decrypt(string encrypted)
{
string secretKey = "1234567890123456";
byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
byte[] ivBytes = null;
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
ICryptoTransform dec = csp.CreateDecryptor(keyBytes, ivBytes);
byte[] cipherText = Encoding.UTF8.GetBytes(encrypted);
string plainText = null;
using (MemoryStream ms = new MemoryStream(cipherText, false))
{
ms.Position = 0;
using (CryptoStream cryptStrm = new CryptoStream(ms, dec, CryptoStreamMode.Read))
{
StreamReader rdr = new StreamReader(cryptStrm);
plainText = rdr.ReadToEnd();
}
}
return plainText;
}
虽然您正在做的事情存在许多安全问题(同样在我提供的代码中也是如此)。如果这不是一个爱好/理解项目,我建议您重新考虑您的安全设计: