「已注销」 2024-06-09 22:52 采纳率: 40%
浏览 61
已结题

基于随笔画的加解密系统

img

img

A.菜单设计
绘图部分要求用户能够通过鼠标在窗体中进行随笔绘画。窗体菜单的功能选择应至少包括:绘图,加密,解密。同时还需设置子菜单供用户选择调整线条类型,线条颜色等。
加密部分要求提供文件保存对话框让用户选择文件名和保存位置。
解密部分要求提供文件打开对话框让用户选择加密文件。B.加解密实现
a.生成对称密钥:可以选择使用Aes.Create()方法生成对称密钥和初始化向量(IV)。并使用AES算法对绘图数据进行加密,加密后的数据存储在文件中。
b.生成非对称密钥对:生成RSA密钥对,并使用RSA算法加密对称密钥和iv。将加密的对称密钥、IV和绘图数据写入文件。
c.读取加密数据:读取分离出加密的对称密钥、IV和加密的绘图数据,使用RSA私钥解密对称密钥和IV。
d.解密绘图数据:使用解密得到的对称密钥和IV解密绘图数据。进而从解密后的数据中恢复绘图点,并重新绘制图像。
目前已经实现了A.菜单设计,如何通过加密键“加密ToolStripMenultem”和解密键“解密ToolStripMenultem”实现加密和解密?

  • 写回答

21条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-06-09 22:52
    关注
    获得0.45元问题酬金

    引自免费微信小程序:皆我百晓生

    要实现基于随笔画的加解密系统,你需要在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加密算法以及如何读写文件来保存和加载加密数据。你需要根据实际情况调整代码,例如获取绘图数据、私钥参数和在窗体上绘制图像的具体实现。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月17日
  • 创建了问题 6月9日