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;
}
相关推荐
骆晨学长8 分钟前
基于springboot的智慧社区微信小程序
java·数据库·spring boot·后端·微信小程序·小程序
AskHarries13 分钟前
利用反射实现动态代理
java·后端·reflect
@月落14 分钟前
alibaba获得店铺的所有商品 API接口
java·大数据·数据库·人工智能·学习
liuyang-neu19 分钟前
力扣 42.接雨水
java·算法·leetcode
z千鑫23 分钟前
【人工智能】如何利用AI轻松将java,c++等代码转换为Python语言?程序员必读
java·c++·人工智能·gpt·agent·ai编程·ai工具
Flying_Fish_roe37 分钟前
Spring Boot-Session管理问题
java·spring boot·后端
赚钱给孩子买茅台喝38 分钟前
智能BI项目第四期
java·spring boot·spring cloud·aigc
指尖流烟1 小时前
C#调用图表的使用方法
开发语言·c#
陈大爷(有低保)2 小时前
UDP Socket聊天室(Java)
java·网络协议·udp
kinlon.liu2 小时前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证