解密被RSA加密过的string类型JSON字符串并将其保存进XML文件(已解决解密时的长度问题)

using Newtonsoft.Json;//必须导入
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;


namespace ConsoleApp1gg
{
    class RSADecryptor_SaveXml
    {
        public void SaveXml(byte[] result)//保存为XML文件 传入参数为Decryptor的返回值
        {


            string content = Encoding.UTF8.GetString(result) ;

            string formatContent = string.Empty;//JSON字符串

//--------------防止JSON字符串前含有乱码---------------------------------------

/*

乱码eg:

?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????[{"Name":"THS","Age":"21","Status":"Alive","Sex":"Man","Address":Earth,"born":"2018-01-11 07:05:01","Phone":"4008000000","Hope":"HOP"}]

*/ 

           int a = content.IndexOf("[");

            if (a > 0)
            {
                formatContent = content.Substring(a);
            }
            else

            {

                formatContent = content;
            }
//-----------------------------------------------------

           
            //反序列化JSON字符串,将JSON字符串转换成LIST列表  
            List<Hashtable> _list = JsonConvert.DeserializeObject<List<Hashtable>>(formatContent);//可以使用自定义model类,但是自定义model类灵活性不够


            XmlDocument doc = new XmlDocument();
            XmlElement Root = doc.CreateElement("DataCollection");//主内容
            doc.AppendChild(Root);
            for (int i = 0; i < _list.Count(); i++)
            {
                XmlElement Child1 = doc.CreateElement("Data" + (i + 1));//子内容头
                XmlAttribute attr1 = doc.CreateAttribute("1st");//属性名
                XmlAttribute attr2 = doc.CreateAttribute("2nd");
                XmlAttribute attr3 = doc.CreateAttribute("3rd");
                attr1.Value = _list[i]["Name"].ToString();//属性值
                attr2.Value = _list[i]["Age"].ToString();
                attr3.Value = _list[i]["Sex"].ToString();
                Child1.Attributes.Append(attr1);
                Child1.Attributes.Append(attr2);
                Child1.Attributes.Append(attr3);
                Root.AppendChild(Child1);
                //这一行和上面顺序不能反//arr1就你的字段,如字段中有引号就要用\' ,最好不要用xml 的text段存内容
                //如果你有170 你的循环要对 应该有两个循环 一个在attr1 这 用于添加150个字段 一个在child1 用于添加几行
            }


            // doc.InnerXml  这个属性就是你的xml 内容
            doc.Save("e://1.xml");//保存这个xml
        }

//----------------------分割线-------------------------------------------------

//注意:RSA解密时密文解密长度不可超过密钥长度

        public byte[] Decryptor(string content,string privatekey)//RSA解密 content为解密内容 privatekey为私有key
        {
            System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
            byte[] EncryptDada = Convert.FromBase64String(content);

            var rsa = new RSACryptoServiceProvider();

            rsa.FromXmlString(privatekey);

            int keySize = rsa.KeySize / 8; byte[] buffer = new byte[keySize];

            using (MemoryStream input = new MemoryStream(EncryptDada))
            using (MemoryStream output = new MemoryStream())
            {
                while (true)
                {
                    int readLine = input.Read(buffer, 0, keySize);
                    if (readLine <= 0)
                    {
                        break;
                    }
                    byte[] temp = new byte[readLine];
                    Array.Copy(buffer, 0, temp, 0, readLine);
                    byte[] decrypt = rsa.Decrypt(temp, false);
                    output.Write(decrypt, 0, decrypt.Length);
                }
                return output.ToArray();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值