C# net  XML读取,解析,AES、RSA加解密类 C# net XML读取,解析,AES、RSA加解密类

C# net XML读取,解析,AES、RSA加解密类

  XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(conf);
            XmlElement xmlElement = null;
            xmlElement = xmlDocument.DocumentElement;
            XmlNodeList xmlNodeList = xmlElement.SelectNodes("/items/psw");
            foreach (object obj in xmlNodeList)
            {
                XmlNode xmlNode = (XmlNode)obj;
                this.psw = xmlNode.InnerText;
            }
            xmlNodeList = xmlElement.SelectNodes("/items/cer");
            foreach (object obj2 in xmlNodeList)
            {
                XmlNode xmlNode = (XmlNode)obj2;
                this.publicKeyPath = xmlNode.InnerText;
            }
            xmlNodeList = xmlElement.SelectNodes("/items/pfx");
            foreach (object obj3 in xmlNodeList)
            {
                XmlNode xmlNode = (XmlNode)obj3;
                this.privateKeyPath = xmlNode.InnerText;
            }
、、加密
public static string Encrypt(string toEncrypt, string key, string iv)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            byte[] bytes2 = Encoding.UTF8.GetBytes(iv);
            byte[] bytes3 = Encoding.UTF8.GetBytes(toEncrypt);
            ICryptoTransform cryptoTransform = new RijndaelManaged
            {
                Key = bytes,
                IV = bytes2,
                Mode = CipherMode.CBC,
                Padding = PaddingMode.Zeros
            }.CreateEncryptor();
            byte[] array = cryptoTransform.TransformFinalBlock(bytes3, 0, bytes3.Length);
            return Convert.ToBase64String(array, 0, array.Length);
        }
  、、解密
    public static string Decrypt(string toDecrypt, string key, string iv)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            byte[] bytes2 = Encoding.UTF8.GetBytes(iv);
            byte[] array = Convert.FromBase64String(toDecrypt);
            ICryptoTransform cryptoTransform = new RijndaelManaged
            {
                Key = bytes,
                IV = bytes2,
                Mode = CipherMode.CBC,
                Padding = PaddingMode.Zeros
            }.CreateDecryptor();
            byte[] bytes3 = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
            string @string = Encoding.UTF8.GetString(bytes3);
            char[] trimChars = new char[1];
            return @string.TrimEnd(trimChars);
        }
=========================RSA类

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace RSAUtil
{
    // Token: 0x0200000C RID: 12
    public class RSAUtil
    {
        // Token: 0x06000048 RID: 72 RVA: 0x00004904 File Offset: 0x00002B04
        public static string RSASign(string data, string privateKeyPem, string psw)
        {
            RSACryptoServiceProvider rsacryptoServiceProvider = RSAUtil.LoadCertificateFile(privateKeyPem);
            byte[] bytes = Encoding.UTF8.GetBytes(data + psw);
            byte[] inArray = rsacryptoServiceProvider.SignData(bytes, "SHA1");
            return Convert.ToBase64String(inArray);
        }

        // Token: 0x06000049 RID: 73 RVA: 0x00004944 File Offset: 0x00002B44
        public static bool RSAVerfiy(string data, string signature, string publicKeyPem, string psw)
        {
            RSACryptoServiceProvider rsapfromPubli = RSAUtil.getRSAPFromPubli(publicKeyPem);
            byte[] bytes = Encoding.UTF8.GetBytes(data + psw);
            byte[] signature2 = Convert.FromBase64String(signature);
            return rsapfromPubli.VerifyData(bytes, "SHA1", signature2);
        }

        // Token: 0x0600004A RID: 74 RVA: 0x00004984 File Offset: 0x00002B84
        public static string RSAEncrypt(string data, string publicKeyPem)
        {
            RSACryptoServiceProvider rsapfromPubli = RSAUtil.getRSAPFromPubli(publicKeyPem);
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            byte[] inArray = rsapfromPubli.Encrypt(bytes, false);
            return Convert.ToBase64String(inArray);
        }

        // Token: 0x0600004B RID: 75 RVA: 0x000049B8 File Offset: 0x00002BB8
        public static string RSADecrypt(string data, string privateKeyPem)
        {
            RSACryptoServiceProvider rsacryptoServiceProvider = RSAUtil.LoadCertificateFile(privateKeyPem);
            byte[] rgb = Convert.FromBase64String(data);
            byte[] bytes = rsacryptoServiceProvider.Decrypt(rgb, false);
            return Encoding.UTF8.GetString(bytes);
        }

        // Token: 0x0600004C RID: 76 RVA: 0x000049EC File Offset: 0x00002BEC
        private static byte[] GetPem(string type, byte[] data)
        {
            string @string = Encoding.UTF8.GetString(data);
            string text = string.Format("-----BEGIN {0}-----\\n", type);
            string value = string.Format("-----END {0}-----", type);
            int num = @string.IndexOf(text) + text.Length;
            int num2 = @string.IndexOf(value, num);
            string s = @string.Substring(num, num2 - num);
            return Convert.FromBase64String(s);
        }

        // Token: 0x0600004D RID: 77 RVA: 0x00004A54 File Offset: 0x00002C54
        private static RSACryptoServiceProvider LoadCertificateFile(string filename)
        {
            RSACryptoServiceProvider result;
            using (FileStream fileStream = File.OpenRead(HttpContext.Current.Server.MapPath(filename)))
            {
                byte[] array = new byte[fileStream.Length];
                byte[] privkey = null;
                fileStream.Read(array, 0, array.Length);
                if (array[0] != 48)
                {
                    privkey = RSAUtil.GetPem("RSA PRIVATE KEY", array);
                }
                try
                {
                    return RSAUtil.DecodeRSAPrivateKey(privkey);
                }
                catch (Exception ex)
                {
                }
                result = null;
            }
            return result;
        }

        // Token: 0x0600004E RID: 78 RVA: 0x00004AFC File Offset: 0x00002CFC
        private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
        {
            MemoryStream input = new MemoryStream(privkey);
            BinaryReader binaryReader = new BinaryReader(input);
            RSACryptoServiceProvider result;
            try
            {
                ushort num = binaryReader.ReadUInt16();
                if (num == 33072)
                {
                    binaryReader.ReadByte();
                }
                else
                {
                    if (num != 33328)
                    {
                        return null;
                    }
                    binaryReader.ReadInt16();
                }
                num = binaryReader.ReadUInt16();
                if (num != 258)
                {
                    result = null;
                }
                else
                {
                    byte b = binaryReader.ReadByte();
                    if (b != 0)
                    {
                        result = null;
                    }
                    else
                    {
                        int integerSize = RSAUtil.GetIntegerSize(binaryReader);
                        byte[] modulus = binaryReader.ReadBytes(integerSize);
                        integerSize = RSAUtil.GetIntegerSize(binaryReader);
                        byte[] exponent = binaryReader.ReadBytes(integerSize);
                        integerSize = RSAUtil.GetIntegerSize(binaryReader);
                        byte[] d = binaryReader.ReadBytes(integerSize);
                        integerSize = RSAUtil.GetIntegerSize(binaryReader);
                        byte[] p = binaryReader.ReadBytes(integerSize);
                        integerSize = RSAUtil.GetIntegerSize(binaryReader);
                        byte[] q = binaryReader.ReadBytes(integerSize);
                        integerSize = RSAUtil.GetIntegerSize(binaryReader);
                        byte[] dp = binaryReader.ReadBytes(integerSize);
                        integerSize = RSAUtil.GetIntegerSize(binaryReader);
                        byte[] dq = binaryReader.ReadBytes(integerSize);
                        integerSize = RSAUtil.GetIntegerSize(binaryReader);
                        byte[] inverseQ = binaryReader.ReadBytes(integerSize);
                        RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider(1024, new CspParameters
                        {
                            Flags = CspProviderFlags.UseMachineKeyStore
                        });
                        rsacryptoServiceProvider.ImportParameters(new RSAParameters
                        {
                            Modulus = modulus,
                            Exponent = exponent,
                            D = d,
                            P = p,
                            Q = q,
                            DP = dp,
                            DQ = dq,
                            InverseQ = inverseQ
                        });
                        result = rsacryptoServiceProvider;
                    }
                }
            }
            catch (Exception ex)
            {
                result = null;
            }
            finally
            {
                binaryReader.Close();
            }
            return result;
        }

        // Token: 0x0600004F RID: 79 RVA: 0x00004D0C File Offset: 0x00002F0C
        private static int GetIntegerSize(BinaryReader binr)
        {
            byte b = binr.ReadByte();
            int result;
            if (b != 2)
            {
                result = 0;
            }
            else
            {
                b = binr.ReadByte();
                int num;
                if (b == 129)
                {
                    num = (int)binr.ReadByte();
                }
                else if (b == 130)
                {
                    byte b2 = binr.ReadByte();
                    byte b3 = binr.ReadByte();
                    byte[] array = new byte[4];
                    array[0] = b3;
                    array[1] = b2;
                    byte[] value = array;
                    num = BitConverter.ToInt32(value, 0);
                }
                else
                {
                    num = (int)b;
                }
                while (binr.ReadByte() == 0)
                {
                    num--;
                }
                binr.BaseStream.Seek(-1L, SeekOrigin.Current);
                result = num;
            }
            return result;
        }

        // Token: 0x06000050 RID: 80 RVA: 0x00004DD0 File Offset: 0x00002FD0
        public static RSACryptoServiceProvider getRSAPFromPubli(string filename)
        {
            byte[] a = new byte[15];
            StreamReader streamReader = File.OpenText(HttpContext.Current.Server.MapPath(filename));
            string value = streamReader.ReadToEnd();
            streamReader.Close();
            StringBuilder stringBuilder = new StringBuilder(value);
            stringBuilder.Replace("-----BEGIN PUBLIC KEY-----", "");
            stringBuilder.Replace("-----END PUBLIC KEY-----", "");
            byte[] array;
            try
            {
                array = Convert.FromBase64String(stringBuilder.ToString());
            }
            catch (FormatException)
            {
                Console.WriteLine("Not a valid  b64 blob; assume binary");
                Stream stream = new FileStream(filename, FileMode.Open);
                int num = (int)stream.Length;
                array = new byte[num];
                stream.Read(array, 0, num);
                stream.Close();
            }
            int num2 = array.Length;
            MemoryStream input = new MemoryStream(array);
            BinaryReader binaryReader = new BinaryReader(input);
            RSACryptoServiceProvider result;
            try
            {
                ushort num3 = binaryReader.ReadUInt16();
                if (num3 == 33072)
                {
                    binaryReader.ReadByte();
                }
                else
                {
                    if (num3 != 33328)
                    {
                        return null;
                    }
                    binaryReader.ReadInt16();
                }
                a = binaryReader.ReadBytes(15);
                if (!RSAUtil.CompareBytearrays(a, RSAUtil.SeqOID))
                {
                    result = null;
                }
                else
                {
                    num3 = binaryReader.ReadUInt16();
                    if (num3 == 33027)
                    {
                        binaryReader.ReadByte();
                    }
                    else
                    {
                        if (num3 != 33283)
                        {
                            return null;
                        }
                        binaryReader.ReadInt16();
                    }
                    byte b = binaryReader.ReadByte();
                    if (b != 0)
                    {
                        result = null;
                    }
                    else
                    {
                        num3 = binaryReader.ReadUInt16();
                        if (num3 == 33072)
                        {
                            binaryReader.ReadByte();
                        }
                        else
                        {
                            if (num3 != 33328)
                            {
                                return null;
                            }
                            binaryReader.ReadInt16();
                        }
                        num3 = binaryReader.ReadUInt16();
                        byte b2 = 0;
                        byte b3;
                        if (num3 == 33026)
                        {
                            b3 = binaryReader.ReadByte();
                        }
                        else
                        {
                            if (num3 != 33282)
                            {
                                return null;
                            }
                            b2 = binaryReader.ReadByte();
                            b3 = binaryReader.ReadByte();
                        }
                        byte[] array2 = new byte[4];
                        array2[0] = b3;
                        array2[1] = b2;
                        byte[] value2 = array2;
                        int num4 = BitConverter.ToInt32(value2, 0);
                        int num5 = binaryReader.PeekChar();
                        if (num5 == 0)
                        {
                            binaryReader.ReadByte();
                            num4--;
                        }
                        byte[] modulus = binaryReader.ReadBytes(num4);
                        if (binaryReader.ReadByte() != 2)
                        {
                            result = null;
                        }
                        else
                        {
                            int count = (int)binaryReader.ReadByte();
                            byte[] exponent = binaryReader.ReadBytes(count);
                            RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
                            rsacryptoServiceProvider.ImportParameters(new RSAParameters
                            {
                                Modulus = modulus,
                                Exponent = exponent
                            });
                            result = rsacryptoServiceProvider;
                        }
                    }
                }
            }
            finally
            {
                binaryReader.Close();
            }
            return result;
        }

        // Token: 0x06000051 RID: 81 RVA: 0x0000510C File Offset: 0x0000330C
        private static bool CompareBytearrays(byte[] a, byte[] b)
        {
            bool result;
            if (a.Length != b.Length)
            {
                result = false;
            }
            else
            {
                int num = 0;
                foreach (byte b2 in a)
                {
                    if (b2 != b[num])
                    {
                        return false;
                    }
                    num++;
                }
                result = true;
            }
            return result;
        }

        // Token: 0x06000052 RID: 82 RVA: 0x00005168 File Offset: 0x00003368
        private static void showBytes(string info, byte[] data)
        {
            Console.WriteLine("{0}  [{1} bytes]", info, data.Length);
            for (int i = 1; i <= data.Length; i++)
            {
                Console.Write("{0:X2}  ", data[i - 1]);
                if (i % 16 == 0)
                {
                    Console.WriteLine();
                }
            }
            Console.WriteLine();
        }

        // Token: 0x06000053 RID: 83 RVA: 0x000051D0 File Offset: 0x000033D0
        private static void PutFileBytes(string outfile, byte[] data, int bytes)
        {
            FileStream fileStream = null;
            if (bytes > data.Length)
            {
                Console.WriteLine("Too many bytes");
            }
            else
            {
                try
                {
                    fileStream = new FileStream(outfile, FileMode.Create);
                    fileStream.Write(data, 0, bytes);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    fileStream.Close();
                }
            }
        }

        // Token: 0x0400000D RID: 13
        private static byte[] SeqOID = new byte[]
        {
            48,
            13,
            6,
            9,
            42,
            134,
            72,
            134,
            247,
            13,
            1,
            1,
            1,
            5,
            0
        };
    }
}

AES类================

using System;
using System.Security.Cryptography;
using System.Text;

namespace AESUtil
{
    // Token: 0x02000014 RID: 20
    public class AESUtil
    {
        // Token: 0x06000078 RID: 120 RVA: 0x00006508 File Offset: 0x00004708
        public static string generateRandomString()
        {
            string text = "";
            for (int i = 0; i < AESUtil.length; i++)
            {
                int index = AESUtil.random.Next(AESUtil.chars.Length);
                text += AESUtil.chars[index];
            }
            return text;
        }

        // Token: 0x06000079 RID: 121 RVA: 0x00006568 File Offset: 0x00004768
        public static string Encrypt(string toEncrypt, string key, string iv)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            byte[] bytes2 = Encoding.UTF8.GetBytes(iv);
            byte[] bytes3 = Encoding.UTF8.GetBytes(toEncrypt);
            ICryptoTransform cryptoTransform = new RijndaelManaged
            {
                Key = bytes,
                IV = bytes2,
                Mode = CipherMode.CBC,
                Padding = PaddingMode.Zeros
            }.CreateEncryptor();
            byte[] array = cryptoTransform.TransformFinalBlock(bytes3, 0, bytes3.Length);
            return Convert.ToBase64String(array, 0, array.Length);
        }

        // Token: 0x0600007A RID: 122 RVA: 0x000065E8 File Offset: 0x000047E8
        public static string Decrypt(string toDecrypt, string key, string iv)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(key);
            byte[] bytes2 = Encoding.UTF8.GetBytes(iv);
            byte[] array = Convert.FromBase64String(toDecrypt);
            ICryptoTransform cryptoTransform = new RijndaelManaged
            {
                Key = bytes,
                IV = bytes2,
                Mode = CipherMode.CBC,
                Padding = PaddingMode.Zeros
            }.CreateDecryptor();
            byte[] bytes3 = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
            string @string = Encoding.UTF8.GetString(bytes3);
            char[] trimChars = new char[1];
            return @string.TrimEnd(trimChars);
        }

        // Token: 0x04000029 RID: 41
        private static int length = 16;

        // Token: 0x0400002A RID: 42
        private static Random random = new Random(DateTime.Now.Millisecond);

        // Token: 0x0400002B RID: 43
        private static string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|";
    }
}

调用方法

public void interpret()
{
    this.digest = "";
    string text = "";
    int num = 0;
    foreach (KeyValuePair<string, string> keyValuePair in this.encrypts)
    {
        num++;
        this.digest += keyValuePair.Value;
        text = text + Uri.EscapeUriString(keyValuePair.Key) + "&" + Uri.EscapeUriString(keyValuePair.Value);
        if (num != this.encrypts.Count)
        {
            text += "&";
        }
    }
    string text2 = AESUtil.generateRandomString();
    string text3 = AESUtil.generateRandomString();
    this.message = RSAUtil.RSAEncrypt(text2, this.publicKeyPath) + RSAUtil.RSAEncrypt(text3, this.publicKeyPath) + AESUtil.Encrypt(text, text2, text3);
    this.signature = RSAUtil.RSASign(this.digest, this.privateKeyPath, this.psw);
public void encryptComfirm()
{
    this.digest = "";
    string str = "";
    int num = 0;
    foreach (KeyValuePair<string, string> keyValuePair in this.encrypts)
    {
        num++;
        this.digest += keyValuePair.Value;
        str = str + Uri.EscapeUriString(keyValuePair.Key) + "&" + Uri.EscapeUriString(keyValuePair.Value);
        if (num != this.encrypts.Count)
        {
            str += "&";
        }
    }
    string text = AESUtil.generateRandomString();
    string text2 = AESUtil.generateRandomString();
    this.message = RSAUtil.RSAEncrypt(text, this.publicKeyPath) + RSAUtil.RSAEncrypt(text2, this.publicKeyPath) + AESUtil.Encrypt(this.digest, text, text2);
}
public void decrypt(string message)
{
    string key = RSAUtil.RSADecrypt(message.Substring(0, 172), this.privateKeyPath);
    string iv = RSAUtil.RSADecrypt(message.Substring(172, 172), this.privateKeyPath);
    string text = AESUtil.Decrypt(message.Substring(344), key, iv);
    this.digest = "";
    this.message = message;
    this.encrypts.Clear();
    string[] array = text.Split(new char[]
    {
        '&'
    });
    for (int i = 0; i < array.Length / 2; i++)
    {
        string key2 = HttpUtility.UrlDecode(array[2 * i]);
        string text2 = HttpUtility.UrlDecode(array[2 * i + 1]);
        this.encrypts.Add(key2, text2);
        this.digest += text2;
    }
}


评论 0

挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论