我必须在c#中加密与java相同的数据。 我没有得到c#的相同输出,请指导我在哪里做错了
这是java代码
DESedeKeySpec keyspec
= new DESedeKeySpec(key.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(EncryptionAlgorithm.DESede.name());
SecretKey deskey = keyfactory.generateSecret(keyspec);
// Create an 8-byte initialization vector
byte[] iv = new byte[]{(byte) 0x39, 0x39, 0x39, (byte) 0x39, 0x39, 0x39, 0x39, 0x39};
IvParameterSpec ivVector = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(EncryptionAlgorithm.DESede.name() + "/" + EncryptionMode.CBC.name() + "/"
+ EncryptionPadding.PKCS5Padding.name());
byte[] ciphertext = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, deskey, ivVector);
ciphertext = cipher.doFinal(pass.getBytes());
} catch (Exception e) {
}
这是我的C#代码..
public string Encrypt(string toEncrypt, bool useHashing,string key)
{
byte[] keyArray;
byte[] toEncryptArray = Encoding.ASCII.GetBytes(toEncrypt);
string retvalue = "";
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(Encoding.ASCII.GetBytes(key));
//Always release the resources and flush data
// of the Cryptographic service provide. Best Practice
hashmd5.Clear();
}
else
keyArray = Encoding.ASCII.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.IV = new byte[] { 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39 };
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateEncryptor(keyArray, keyArray), CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cryptoStream))
{
writer.Write(toEncrypt);
cryptoStream.FlushFinalBlock();
writer.Flush();
retvalue = BitConverter.ToString(memoryStream.GetBuffer(), 0, (int)memoryStream.Length).Replace("-","");
writer.Close();
}
cryptoStream.Close();
}
memoryStream.Close();
}
return retvalue;
}
答案 0 :(得分:1)
您在C#版本中以错误的顺序刷新缓冲区。首先刷新CryptoStream
,表示您已完成加密操作,然后刷新StreamWriter
,表示您现在已经完成了最后一次写入流在冲洗操作期间加密。
如果你切换这两个操作,一切都应该有效,但是你可以对所涉及的操作更加明确,而不需要手动刷新和关闭缓冲区,将其留到using
块:
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateEncryptor(keyArray, keyArray), CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cryptoStream))
{
writer.Write(toEncrypt);
}
byte[] buffer = memoryStream.ToArray();
retvalue = BitConverter.ToString(buffer, 0, buffer.Length).Replace("-", "");
}
}
答案 1 :(得分:0)
请在C#代码中尝试以下操作。
//using (MemoryStream memoryStream = new MemoryStream())
//{
// using (CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateEncryptor(keyArray, keyArray), CryptoStreamMode.Write))
// {
// using (StreamWriter writer = new StreamWriter(cryptoStream))
// {
// writer.Write(toEncrypt);
// cryptoStream.FlushFinalBlock();
// writer.Flush();
// retvalue = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length).Replace("-", "");
// writer.Close();
// }
// cryptoStream.Close();
// }
// memoryStream.Close();
//}
//return retvalue;
ICryptoTransform ct = tdes.CreateEncryptor();
byte[] input = Encoding.UTF8.GetBytes(toEncrypt);
var output = ct.TransformFinalBlock(input, 0, input.Length);
return Convert.ToBase64String(output);
我们可以使用以下内容在java端获得Base64String。
final String encodedCipherText = new sun.misc.BASE64Encoder().encode(ciphertext);
return encodedCipherText;