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;
}
相关推荐
LallanaLee几秒前
常见面试题
java·开发语言
bestcxx2 分钟前
c# UTC 时间赋值注意事项
c#·utc
酷炫码神5 分钟前
C#运算符
开发语言·c#
爱尚你199326 分钟前
Java 泛型与类型擦除:为什么解析对象时能保留泛型信息?
java
电商数据girl1 小时前
酒店旅游类数据采集API接口之携程数据获取地方美食品列表 获取地方美餐馆列表 景点评论
java·大数据·开发语言·python·json·旅游
CircleMouse1 小时前
基于 RedisTemplate 的分页缓存设计
java·开发语言·后端·spring·缓存
ktkiko111 小时前
顶层架构 - 消息集群推送方案
java·开发语言·架构
zybsjn1 小时前
后端系统做国际化改造,生成多语言包
java·python·c#
Unity官方开发者社区2 小时前
《Cryptical Path》开发诀窍:像玩游戏一样开发一款类Rogue游戏
java·游戏·玩游戏
敲代码的 蜡笔小新2 小时前
【行为型之迭代器模式】游戏开发实战——Unity高效集合遍历与场景管理的架构精髓
unity·设计模式·c#·迭代器模式