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) ;
{
formatContent = content.Substring(a);
}
else
}
//-----------------------------------------------------
//反序列化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
}
{
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] EncryptDada = Convert.FromBase64String(content);
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();
}
}
}
}
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();
}
}
}
}