解密被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
    评论
好的,以下是一个 PHP 中使用 AES/CBC/PKCS7Padding 和 RSA 公钥/私钥加密解密的例子: ```php // AES 加密函数 function aesEncrypt($data, $key) { $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-128-cbc')); $encrypted = openssl_encrypt($data, 'aes-128-cbc', $key, OPENSSL_RAW_DATA, $iv); $result = base64_encode($iv . $encrypted); return $result; } // AES 解密函数 function aesDecrypt($data, $key) { $data = base64_decode($data); $iv = substr($data, 0, openssl_cipher_iv_length('aes-128-cbc')); $encrypted = substr($data, openssl_cipher_iv_length('aes-128-cbc')); $result = openssl_decrypt($encrypted, 'aes-128-cbc', $key, OPENSSL_RAW_DATA, $iv); return $result; } // RSA 公钥加密函数 function rsaEncryptPublic($data, $publicKey) { $publicKey = openssl_get_publickey($publicKey); openssl_public_encrypt($data, $encrypted, $publicKey, OPENSSL_PKCS1_PADDING); openssl_free_key($publicKey); $result = base64_encode($encrypted); return $result; } // RSA 私钥解密函数 function rsaDecryptPrivate($data, $privateKey) { $privateKey = openssl_get_privatekey($privateKey); $data = base64_decode($data); openssl_private_decrypt($data, $decrypted, $privateKey, OPENSSL_PKCS1_PADDING); openssl_free_key($privateKey); return $decrypted; } // 例子:加密一个字符串 $data = 'Hello World!'; $aesKey = '1234567890123456'; $rsaPublicKey = '-----BEGIN PUBLIC KEY-----\n...公钥字符串...\n-----END PUBLIC KEY-----'; $rsaPrivateKey = '-----BEGIN PRIVATE KEY-----\n...私钥字符串...\n-----END PRIVATE KEY-----'; // 先使用 AES 加密数据 $encryptedData = aesEncrypt($data, $aesKey); // 再使用 RSA 公钥加密 AES 密钥 $encryptedAesKey = rsaEncryptPublic($aesKey, $rsaPublicKey); // 解密 AES 密钥 $decryptedAesKey = rsaDecryptPrivate($encryptedAesKey, $rsaPrivateKey); // 使用解密后的 AES 密钥解密数据 $decryptedData = aesDecrypt($encryptedData, $decryptedAesKey); echo $decryptedData; // 输出:Hello World! ``` 注意,这只是一个简单的例子,实际使用还需要考虑一些安全问题,比如密钥管理、数据完整性等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值