引自免费微信小程序:皆我百晓生
要实现基于随笔画的加解密系统,你需要在C#中使用Windows Forms结合加密算法。下面是一个概要步骤和代码示例,用于实现加密和解密功能。
加密功能实现
首先,确保你已经导入了必要的命名空间:
using System.Security.Cryptography;
using System.IO;
接下来,定义加密过程。当用户点击“加密”菜单项时,执行以下代码:
private void encryptToolStripMenuItem_Click(object sender, EventArgs e)
{
// 1. 获取绘图数据(假设你已经有了绘图数据列表)
List<Point> drawingPoints = GetDrawingPoints();
// 2. 生成对称密钥和IV
using (Aes aes = Aes.Create())
{
byte[] symmetricKey = aes.Key;
byte[] iv = aes.IV;
// 3. 使用AES加密绘图数据
byte[] encryptedData = EncryptData(drawingPoints, aes);
// 4. 生成非对称密钥对
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] encryptedSymmetricKey = rsa.Encrypt(symmetricKey, false);
byte[] encryptedIv = rsa.Encrypt(iv, false);
// 5. 保存加密的数据和密钥到文件
SaveEncryptedDataToFile(encryptedSymmetricKey, encryptedIv, encryptedData);
}
}
private byte[] EncryptData(List<Point> points, Aes aes)
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
BinaryWriter writer = new BinaryWriter(cs);
foreach (var point in points)
{
writer.Write(point.X);
writer.Write(point.Y);
}
writer.Close();
}
return ms.ToArray();
}
}
private void SaveEncryptedDataToFile(byte[] encryptedSymmetricKey, byte[] encryptedIv, byte[] encryptedData)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog.FileName;
using (FileStream fs = File.OpenWrite(filePath))
{
BinaryWriter writer = new BinaryWriter(fs);
writer.Write(encryptedSymmetricKey.Length);
writer.Write(encryptedSymmetricKey);
writer.Write(encryptedIv.Length);
writer.Write(encryptedIv);
writer.Write(encryptedData.Length);
writer.Write(encryptedData);
writer.Close();
}
}
}
解密功能实现
当用户点击“解密”菜单项时,执行以下代码:
private void decryptToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
byte[] encryptedSymmetricKey, encryptedIv, encryptedData;
LoadEncryptedDataFromFile(filePath, out encryptedSymmetricKey, out encryptedIv, out encryptedData);
// 1. 使用RSA私钥解密对称密钥和IV
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(GetPrivateKeyParameters()); // 假设你有私钥参数
byte[] symmetricKey = rsa.Decrypt(encryptedSymmetricKey, false);
byte[] iv = rsa.Decrypt(encryptedIv, false);
// 2. 使用解密得到的对称密钥和IV解密绘图数据
List<Point> drawingPoints = DecryptData(encryptedData, symmetricKey, iv);
// 3. 在窗体上重新绘制图像
DrawPointsOnForm(drawingPoints);
}
}
private void LoadEncryptedDataFromFile(string filePath, out byte[] encryptedSymmetricKey, out byte[] encryptedIv, out byte[] encryptedData)
{
encryptedSymmetricKey = null;
encryptedIv = null;
encryptedData = null;
using (FileStream fs = File.OpenRead(filePath))
{
BinaryReader reader = new BinaryReader(fs);
int keyLength = reader.ReadInt32();
encryptedSymmetricKey = reader.ReadBytes(keyLength);
int ivLength = reader.ReadInt32();
encryptedIv = reader.ReadBytes(ivLength);
int dataLength = reader.ReadInt32();
encryptedData = reader.ReadBytes(dataLength);
reader.Close();
}
}
private List<Point> DecryptData(byte[] encryptedData, byte[] symmetricKey, byte[] iv)
{
List<Point> points = new List<Point>();
using (Aes aes = Aes.Create())
{
aes.Key = symmetricKey;
aes.IV = iv;
using (MemoryStream ms = new MemoryStream(encryptedData))
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
BinaryReader reader = new BinaryReader(cs);
while (reader.PeekChar() > -1)
{
points.Add(new Point(reader.ReadInt32(), reader.ReadInt32()));
}
reader.Close();
}
}
}
return points;
}
private void DrawPointsOnForm(List<Point> points)
{
// 根据points列表在窗体上绘制图像
// 这里省略具体实现
}
这个示例展示了如何使用AES和RSA加密算法以及如何读写文件来保存和加载加密数据。你需要根据实际情况调整代码,例如获取绘图数据、私钥参数和在窗体上绘制图像的具体实现。