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;
}
相关推荐
雾月5514 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
24k小善1 小时前
Flink TaskManager详解
java·大数据·flink·云计算
想不明白的过度思考者1 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
猿周LV2 小时前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
晨集2 小时前
Uni-App 多端电子合同开源项目介绍
java·spring boot·uni-app·电子合同
时间之城2 小时前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel
椰羊~王小美2 小时前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言
凯酱2 小时前
MyBatis-Plus分页插件的使用
java·tomcat·mybatis
程序员总部2 小时前
如何在IDEA中高效使用Test注解进行单元测试?
java·单元测试·intellij-idea