zoukankan      html  css  js  c++  java
  • 利用RSACryptoServiceProvider进行RSA加密解密

    利用RSACryptoServiceProvider进行RSA加密解密

    加密获取公私钥

     static void Main(string[] args)
            {
                RsaPrivateKey xmlPrivateKeys;
                RsaPublicKey xmlPublicKey;
                RSAKey(out xmlPrivateKeys,out xmlPublicKey);
            }
    
            public static void RSAKey(out RsaPrivateKey xmlPrivateKeys, out RsaPublicKey xmlPublicKey)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                string xmlPrivateKeyStr = rsa.ToXmlString(true);
                string xmlPublicKeyStr = rsa.ToXmlString(false);
                xmlPublicKey = XmlCustomSerialization.XmlDeserialize<RsaPublicKey>(xmlPublicKeyStr);
                xmlPublicKey.PublicKeyStr = xmlPublicKeyStr;
                xmlPrivateKeys = XmlCustomSerialization.XmlDeserialize<RsaPrivateKey>(xmlPrivateKeyStr);
                xmlPrivateKeys.PrivateKeyStr = xmlPrivateKeyStr;
            }

    RsaPrivateKey

    XmlType(AnonymousType = true), XmlRoot(Namespace = "", IsNullable = false, ElementName = "RSAKeyValue")]
        public class RsaPrivateKey
        {
            // Fields
            //[CompilerGenerated, DebuggerBrowsable(DebuggerBrowsableState.Never)]
            //private string <PrivateKeyStr> k__BackingField;
            private string exponentField;
            private string modulusField;
    
            // Methods
            public RsaPrivateKey() { }
    
            // Properties
            public string Exponent { get; set; }
            [XmlElement("Modulus")]
            public string Modulus { get; set; }
            public string PrivateKeyStr { get; set; }
        }
    View Code

    RsaPublicKey

    [XmlType(AnonymousType = true), XmlRoot(Namespace = "", IsNullable = false, ElementName = "RSAKeyValue")]
        public class RsaPublicKey
        {
            // Fields
            //[CompilerGenerated, DebuggerBrowsable(DebuggerBrowsableState.Never)]
            //private string <PublicKeyStr>k__BackingField;
            private string dField;
            private string dpField;
            private string dqField;
            private string exponentField;
            private string inverseQField;
            private string modulusField;
            private string pField;
            private string qField;
    
            // Methods
            public RsaPublicKey() { }
    
            // Properties
            public string D { get; set; }
            public string DP { get; set; }
            public string DQ { get; set; }
            public string Exponent { get; set; }
            public string InverseQ { get; set; }
            public string Modulus { get; set; }
            public string P { get; set; }
            public string PublicKeyStr { get; set; }
            public string Q { get; set; }
        }
    View Code

    XmlCustomSerialization xml序列化帮助类

    public class XmlCustomSerialization
        {
            // Methods
            public static T XmlDeserialize<T>(string item)
            {
                using (StringReader stringReader = new StringReader(item))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return (T)serializer.Deserialize(stringReader);
                }
            }
    
            public static string XmlSerialize(object item, bool removeNamespace = false, bool omitXmlDeclaration = false)
            {
                string serialXML = string.Empty;
                try
                {
                    StringBuilder sb = new StringBuilder();
                    XmlWriterSettings settings1 = new XmlWriterSettings
                    {
                        Indent = true,
                        OmitXmlDeclaration = omitXmlDeclaration,
                        Encoding = Encoding.UTF8
                    };
                    XmlWriterSettings settings = settings1;
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add(string.Empty, string.Empty);
                    XmlSerializer xmlSerializer = new XmlSerializer(item.GetType());
                    using (XmlWriter writer = XmlWriter.Create(sb, settings))
                    {
                        if (removeNamespace)
                        {
                            xmlSerializer.Serialize(writer, item, ns);
                        }
                        else
                        {
                            xmlSerializer.Serialize(writer, item);
                        }
                    }
                    serialXML = sb.ToString();
                }
                catch
                {
                    throw;
                }
                return serialXML;
            }
    
            public static string XmlSerializeToPureXML(object item)
            {
                string serialXML = string.Empty;
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
                {
                    OmitXmlDeclaration = true,
                    Indent = true,
                    CloseOutput = true,
                    Encoding = Encoding.UTF8
                };
                using (StringWriter stringWriter = new StringWriter())
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings))
                    {
                        XmlSerializer xmlSerializer = new XmlSerializer(item.GetType());
                        XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
                        xmlSerializerNamespaces.Add(string.Empty, string.Empty);
                        xmlSerializer.Serialize(xmlWriter, item, xmlSerializerNamespaces);
                        serialXML = stringWriter.ToString();
                        xmlWriter.Flush();
                    }
                    stringWriter.Flush();
                }
                return serialXML;
            }
    
            public static string XmlSerializeWithNoXmlDeclaration(object item)
            {
                string serialXML = string.Empty;
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
                {
                    OmitXmlDeclaration = true,
                    Indent = true,
                    CloseOutput = true,
                    Encoding = Encoding.UTF8
                };
                using (StringWriter stringWriter = new StringWriter())
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings))
                    {
                        new XmlSerializer(item.GetType()).Serialize(xmlWriter, item);
                        serialXML = stringWriter.ToString();
                        xmlWriter.Flush();
                    }
                    stringWriter.Flush();
                }
                return serialXML;
            }
        }
  • 相关阅读:
    iis重写模块实现程序自动二级域名,微软提供的URL重写2.0版本适用IIS以上
    借助微软提供的url重写类库URLRewriter.dll(1.0)实现程序自动二级域名,域名需要泛解析
    AspNetPager控件报错误: Syntax error, unrecognized expression: input#ctl00$ContentPlaceHolder1$Aspnetpager1_input问题解决[摘]
    精准定位才能赢得人心
    IIS7.5打开GZip压缩,同时启用GZip压缩JS/CSS文件的设置方法[bubuko.com]
    c#变量缺少using引用,如何快速加上using,加Using的快捷键[bubuko.com]
    chart.js插件生成折线图时数据普遍较大时Y轴数据不从0开始的解决办法[bubuko.com]
    要用于尝试,广东移动间接实现“流量不清零”[bubuko.com]
    jquery中ajax在firefox浏览器下“object XMLDocument”返回结果的解决办法
    mvc中Url.RouteUrl或者Html.RouteLink实现灵活超链接,使href的值随路由名称或配置的改变而改变[bubuko.com]
  • 原文地址:https://www.cnblogs.com/jameslif/p/7116990.html
Copyright © 2011-2022 走看看