C# XML 加密解密

步骤 1: 生成RSA密钥

首先,我们需要生成一个RSA密钥对,用于加密和解密。

using System;
using System.Security.Cryptography;
using System.Xml;

public class XmlEncryptionExample
{
    public static RSAParameters publicKey;
    public static RSAParameters privateKey;

    static void Main()
    {
        GenerateKeys();
        string originalXml = "<root><child>Secret Data</child></root>";

        // 加密
        string encryptedXml = EncryptXml(originalXml);
        Console.WriteLine("Encrypted XML:");
        Console.WriteLine(encryptedXml);

        // 解密
        string decryptedXml = DecryptXml(encryptedXml);
        Console.WriteLine("\nDecrypted XML:");
        Console.WriteLine(decryptedXml);
    }

    public static void GenerateKeys()
    {
        using (var rsa = new RSACryptoServiceProvider(2048))
        {
            publicKey = rsa.ExportParameters(false);
            privateKey = rsa.ExportParameters(true);
        }
    }
}

步骤 2: 加密XML

然后,我们可以创建一个函数来加密XML文档。

public static string EncryptXml(string xmlContent)
{
    var xmlDoc = new XmlDocument();
    xmlDoc.PreserveWhitespace = true;
    xmlDoc.LoadXml(xmlContent);

    var encryptedXml = new EncryptedXml(xmlDoc);

    var encryptedData = new EncryptedData
    {
        Type = EncryptedXml.XmlEncElementUrl,
        EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url)
    };

    var encryptedElement = encryptedXml.Encrypt(xmlDoc.DocumentElement, publicKey);
    encryptedData.CipherData.CipherValue = encryptedElement.CipherValue;

    var encryptedXmlDocument = new XmlDocument();
    encryptedXmlDocument.PreserveWhitespace = true;
    encryptedXmlDocument.AppendChild(encryptedXmlDocument.CreateElement("root"));
    encryptedXmlDocument.DocumentElement.AppendChild(encryptedXmlDocument.ImportNode(encryptedData.GetXml(), true));

    return encryptedXmlDocument.InnerXml;
}

步骤 3: 解密XML

最后,我们需要一个函数来解密加密后的XML。

public static string DecryptXml(string encryptedXmlContent)
{
    var encryptedXmlDocument = new XmlDocument();
    encryptedXmlDocument.LoadXml(encryptedXmlContent);

    var encryptedXml = new EncryptedXml(encryptedXmlDocument);
    encryptedXml.AddKeyNameMapping("RSAKey", privateKey);

    var encryptedData = new EncryptedData();
    encryptedData.LoadXml(encryptedXmlDocument.DocumentElement);

    var decryptedXmlElement = encryptedXml.DecryptData(encryptedData, privateKey);
    var xmlDoc = new XmlDocument();
    xmlDoc.PreserveWhitespace = true;
    xmlDoc.LoadXml(decryptedXmlElement);

    return xmlDoc.InnerXml;
}
相关推荐
二十七剑几秒前
jvm调试和查看工具
java·linux·jvm
且听风吟ayan22 分钟前
leetcode day20 滑动窗口209+904
算法·leetcode·c#
南宫生28 分钟前
力扣每日一题【算法学习day.133】
java·学习·算法·leetcode
獨枭30 分钟前
如何在 Mac 上安装并配置 JDK 环境变量
java·macos·jdk
m0_7383556942 分钟前
java泛型
java·开发语言
web2u1 小时前
Docker入门及基本概念
java·运维·服务器·spring·docker·容器
qq_218753311 小时前
常用Git命令
java·git
计算机小白一个1 小时前
蓝桥杯 Java B 组之背包问题(01背包、完全背包)
java·职场和发展·蓝桥杯
计算机毕设定制辅导-无忧学长1 小时前
Maven 基础环境搭建与配置(二)
java·maven
逸狼2 小时前
【JavaEE进阶】Spring IoC
java·spring·java-ee