DESCryptoServiceProvider 解密不起作用。获取带有 \0 字符和第一个字母的输出
DESCryptoServiceProvider decryption not working. Getting output with \0 character and just first letter
我正在根据以下逻辑从以下代码加密创建许可证:
private void SaveLicenseInfoWithEncryption(LicenseInfo obj)
{
string strLicenseString = "Kamran";
//string strLicenseString = JsonConvert.SerializeObject(obj, Formatting.None);
byte[] buffer = new byte[strLicenseString.Length * sizeof(char)];
Buffer.BlockCopy(strLicenseString.ToCharArray(), 0, buffer, 0, buffer.Length);
DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
{
Key = new UTF8Encoding().GetBytes(ConfigurationManager.AppSettings["EncryptionKey"])
};
desCryptSrvckey.IV = desCryptSrvckey.Key;
using (MemoryStream stmCipherText = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(stmCipherText, desCryptSrvckey.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
string licPath = Path.Combine(txtFolderPath.Text, @"Test.lic");
File.WriteAllBytes(licPath, stmCipherText.ToArray());
MessageBox.Show(@"License File Generated Successfully");
}
}
}
许可证生成正常(我暂时放入了示例文本)。问题是我正在尝试解密我的许可证,但我遇到了一些无效行为。
我的代码是:
private void button1_Click(object sender, EventArgs e)
{
var fileDialog = new OpenFileDialog
{
Filter = @"License Files|*.lic",
Title = @"Select a License File"
};
if (fileDialog.ShowDialog() == DialogResult.OK)
{
byte[] buffer = File.ReadAllBytes(fileDialog.FileName);
DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
{
Key = new UTF8Encoding().GetBytes(ConfigurationManager.AppSettings["EncryptionKey"])
};
desCryptSrvckey.IV = desCryptSrvckey.Key;
using (MemoryStream encryptedStream = new MemoryStream(buffer))
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(encryptedStream, desCryptSrvckey.CreateDecryptor(), CryptoStreamMode.Read))
{
using (StreamWriter sw = new StreamWriter(ms))
{
using (StreamReader sr = new StreamReader(cs))
{
sw.Write(sr.ReadToEnd());
sw.Flush();
}
ms.Position = 0;
txtContent.Text = new StreamReader(ms).ReadToEnd();
}
}
}
}
}
}
在手表中 window 我收到文本 K[=12=]a[=12=]m[=12=]r[=12=]a[=12=]n[=12=]
但文本框中的最终输出只是 K
.
丢失BlockCopy
并将字符串转换为UTF-8:
buffer = Encoding.UTF8.GetBytes(strLicenseString);
或 使用您当前的代码并告诉 reader 文本是 UTF-16(这就是内部字符串编码 & 最终以进行原始块复制时的缓冲区)
new StreamReader(cs, Encoding.Unicode)
您还可以简化为:
...
desCryptSrvckey.IV = desCryptSrvckey.Key;
using (MemoryStream encryptedStream = new MemoryStream(buffer))
using (CryptoStream cs = new CryptoStream(encryptedStream, desCryptSrvckey.CreateDecryptor(), CryptoStreamMode.Read))
using (StreamReader sr = new StreamReader(cs))
{
txtContent.Text = sr.ReadToEnd();
}
我正在根据以下逻辑从以下代码加密创建许可证:
private void SaveLicenseInfoWithEncryption(LicenseInfo obj)
{
string strLicenseString = "Kamran";
//string strLicenseString = JsonConvert.SerializeObject(obj, Formatting.None);
byte[] buffer = new byte[strLicenseString.Length * sizeof(char)];
Buffer.BlockCopy(strLicenseString.ToCharArray(), 0, buffer, 0, buffer.Length);
DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
{
Key = new UTF8Encoding().GetBytes(ConfigurationManager.AppSettings["EncryptionKey"])
};
desCryptSrvckey.IV = desCryptSrvckey.Key;
using (MemoryStream stmCipherText = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(stmCipherText, desCryptSrvckey.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
string licPath = Path.Combine(txtFolderPath.Text, @"Test.lic");
File.WriteAllBytes(licPath, stmCipherText.ToArray());
MessageBox.Show(@"License File Generated Successfully");
}
}
}
许可证生成正常(我暂时放入了示例文本)。问题是我正在尝试解密我的许可证,但我遇到了一些无效行为。
我的代码是:
private void button1_Click(object sender, EventArgs e)
{
var fileDialog = new OpenFileDialog
{
Filter = @"License Files|*.lic",
Title = @"Select a License File"
};
if (fileDialog.ShowDialog() == DialogResult.OK)
{
byte[] buffer = File.ReadAllBytes(fileDialog.FileName);
DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
{
Key = new UTF8Encoding().GetBytes(ConfigurationManager.AppSettings["EncryptionKey"])
};
desCryptSrvckey.IV = desCryptSrvckey.Key;
using (MemoryStream encryptedStream = new MemoryStream(buffer))
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(encryptedStream, desCryptSrvckey.CreateDecryptor(), CryptoStreamMode.Read))
{
using (StreamWriter sw = new StreamWriter(ms))
{
using (StreamReader sr = new StreamReader(cs))
{
sw.Write(sr.ReadToEnd());
sw.Flush();
}
ms.Position = 0;
txtContent.Text = new StreamReader(ms).ReadToEnd();
}
}
}
}
}
}
在手表中 window 我收到文本 K[=12=]a[=12=]m[=12=]r[=12=]a[=12=]n[=12=]
但文本框中的最终输出只是 K
.
丢失BlockCopy
并将字符串转换为UTF-8:
buffer = Encoding.UTF8.GetBytes(strLicenseString);
或 使用您当前的代码并告诉 reader 文本是 UTF-16(这就是内部字符串编码 & 最终以进行原始块复制时的缓冲区)
new StreamReader(cs, Encoding.Unicode)
您还可以简化为:
...
desCryptSrvckey.IV = desCryptSrvckey.Key;
using (MemoryStream encryptedStream = new MemoryStream(buffer))
using (CryptoStream cs = new CryptoStream(encryptedStream, desCryptSrvckey.CreateDecryptor(), CryptoStreamMode.Read))
using (StreamReader sr = new StreamReader(cs))
{
txtContent.Text = sr.ReadToEnd();
}