C# TripleDESCryptoServiceProvider奇怪字符
我试图解密一些我加密的文本,但是,我得到了奇怪的字符: ﶇ쒏栎ル찢샋�㨺⸸⓬왛㓘瞅苽景崲胏퐛㽵㈊褗邌≟䒙쉪ਊᮻ 以下是加密文本: 1Cu+45SHXGSSHMJIYUZX3UQYPR6N4VXUWIVZYTDCUS05L4TZT4JJBJLG6UJBZZGGJRWWTVMOWC3FGLBU/IsvM6U3il3i 如何获取常规文本C# TripleDESCryptoServiceProvider奇怪字符,c#,C#,我试图解密一些我加密的文本,但是,我得到了奇怪的字符: ﶇ쒏栎ル찢샋�㨺⸸⓬왛㓘瞅苽景崲胏퐛㽵㈊褗邌≟䒙쉪ਊᮻ 以下是加密文本: 1Cu+45SHXGSSHMJIYUZX3UQYPR6N4VXUWIVZYTDCUS05L4TZT4JJBJLG6UJBZZGGJRWWTVMOWC3FGLBU/IsvM6U3il3i 如何获取常规文本 public static string DecryptData(string encryptedtext, string key) {
public static string DecryptData(string encryptedtext, string key)
{
key = m_Key;
try
{
TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();
TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);
// Convert the encrypted text string to a byte array.
byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);
// Create the stream.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Create the decoder to write to the stream.
CryptoStream decStream = new CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
// Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();
// Convert the plaintext stream to a string.
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}
catch (Exception)
{
return DecryptDataOldKey(encryptedtext, string.Empty);
}
}
这是我的加密代码:
public static string EncryptData(string plaintext, string key = "")
{
TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();
TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);
// Convert the plaintext string to a byte array.
byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);
// Create the stream.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Create the encoder to write to the stream.
CryptoStream encStream = new CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
// Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
encStream.FlushFinalBlock();
// Convert the encrypted stream to a printable string.
return Convert.ToBase64String(ms.ToArray());
}
加密和解密数据时应使用相同的编码,例如UTF8。将System.Text.Encoding.Unicode更改为System.Text.Encoding.UTF8,另外,还有相当多的对象,它们的类实现了
IDisposable
接口。您应该使用语句将这些对象的生存期包装在中,以确保正确的确定性处理。此外,DecryptData()
似乎有点奇怪-您正在用类级成员m_key
覆盖传入的参数key
。也许您在这里使用不同的密钥进行加密和解密?您好,我使用Unicode对其进行加密。从现在起我应该使用UTF8吗?