我正在尝试解密我加密的一些文本,但是,我得到了奇怪的字符:
لمح쒏栎ル찢샋㨺⸸⓬왛㓘瞅苽景崲胏퐛㽵㈊褗邌≟䒙쉪ਊᮻ
以下是加密文字:
1CU + 45SHXGsSHmJiYuZx3uQYPr6N4vXUwIvzYtDcUs05l4tZZT4jJBjlG6uJbzZgjQOwjRwwTvmowC3FGlbU / IsvM6U3il3i
如何获得常规文字?
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());
}
答案 0 :(得分:0)
加密和解密数据时应使用相同的编码,例如 UTF8 。将 System.Text.Encoding.Unicode 更改为 System.Text.Encoding.UTF8 。