再共享一个安全帮助类-SecurityHelper

  发现自己的博客首页的底部Banner条的位置不对,原因是内容太少了。还没想好应该写什么,所以先把以前的一些小东西先整理出来,装点一下门面。

  SecurityHelper是以前写了用于远程业务通讯安全的一个帮助类,功能也很简单,还有需要改进的地方,但当时觉得能用了就没再管它。大家如果有什么建议将不胜感激。

  对了,请教一个问题,目前这些代码虽然都是我写的,但用在了公司的项目中。这会不会违背了职业道德?我的理解是这些代码都是比较通用的部分,凡是涉及具体业务的内容一律剔除,不涉及公司的商业秘密,也不可能因此对业务系统造成损害。另一方面,这类代码完全可以公开了接受公众检验,提高质量。当然这只是个人看法,如果大部分人认为这样做不合适那我会处理掉。

  1 None.gif using  System;
  2 None.gif using  System.IO;
  3 None.gif using  System.Security.Cryptography;
  4 None.gif using  System.Text;
  5 None.gif using  System.Xml;
  6 None.gif
  7 None.gif namespace  NHTSS.Framework.Common
  8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 10InBlock.gif    /// 为系统提供加密、解密功能的帮助类。
 11ExpandedSubBlockEnd.gif    /// </summary>

 12InBlock.gif    public sealed class SecurityHelper
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 15InBlock.gif        /// 内部构造,不允许实例化该类。
 16ExpandedSubBlockEnd.gif        /// </summary>

 17InBlock.gif        internal SecurityHelper()
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19ExpandedSubBlockEnd.gif        }

 20InBlock.gif
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 22InBlock.gif        /// 加密文本。
 23InBlock.gif        /// </summary>
 24InBlock.gif        /// <remarks>
 25InBlock.gif        /// 使用RSA算法。
 26InBlock.gif        /// </remarks>
 27InBlock.gif        /// <param name="xmlKeyPath">密钥文件(XML)路径。</param>
 28InBlock.gif        /// <param name="strMessage">待加密的文本。</param>
 29ExpandedSubBlockEnd.gif        /// <returns>密文。</returns>

 30InBlock.gif        public static string RSAEncryptString(string xmlKeyPath, string strMessage)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            if (!File.Exists(xmlKeyPath))
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 34InBlock.gif                throw new System.IO.FileNotFoundException("加密密钥文件不存在。");
 35ExpandedSubBlockEnd.gif            }

 36InBlock.gif            try
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 38InBlock.gif                UnicodeEncoding myByteConverter = new UnicodeEncoding();
 39InBlock.gif
 40InBlock.gif                byte[] dataToEncrypt = myByteConverter.GetBytes(strMessage);
 41InBlock.gif                byte[] encryptedData;
 42InBlock.gif
 43InBlock.gif                using (RSACryptoServiceProvider myRSACSP = new RSACryptoServiceProvider())
 44ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 45InBlock.gif                    XmlDocument myDoc = new XmlDocument();
 46InBlock.gif                    myDoc.Load(xmlKeyPath);
 47InBlock.gif                    myRSACSP.FromXmlString(myDoc.OuterXml);
 48InBlock.gif
 49InBlock.gif                    encryptedData = myRSACSP.Encrypt(dataToEncrypt, false);
 50ExpandedSubBlockEnd.gif                }

 51InBlock.gif                return Convert.ToBase64String(encryptedData);
 52ExpandedSubBlockEnd.gif            }

 53InBlock.gif            catch (System.Exception ex)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                throw new ApplicationException("加密失败!\r\n" + ex.Message, ex);
 56ExpandedSubBlockEnd.gif            }

 57ExpandedSubBlockEnd.gif        }

 58InBlock.gif
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 60InBlock.gif        /// 解密文本。
 61InBlock.gif        /// </summary>
 62InBlock.gif        /// <remarks>
 63InBlock.gif        /// 使用RSA算法。
 64InBlock.gif        /// </remarks>
 65InBlock.gif        /// <param name="xmlKeyPath">密钥文件(XML)路径。</param>
 66InBlock.gif        /// <param name="strCryptograph">待解密的文本。</param>
 67ExpandedSubBlockEnd.gif        /// <returns>明文。</returns>

 68InBlock.gif        public static string RSADecryptString(string xmlKeyPath, string strCryptograph)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70InBlock.gif            if (!File.Exists(xmlKeyPath))
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 72InBlock.gif                throw new System.IO.FileNotFoundException("解密密钥文件不存在。");
 73ExpandedSubBlockEnd.gif            }

 74InBlock.gif
 75InBlock.gif            try
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 77InBlock.gif                byte[] dataToDecrypt = Convert.FromBase64String(strCryptograph);
 78InBlock.gif                byte[] decryptedData;
 79InBlock.gif
 80InBlock.gif                using (RSACryptoServiceProvider myRSACSP = new RSACryptoServiceProvider())
 81ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 82InBlock.gif                    XmlDocument myDoc = new XmlDocument();
 83InBlock.gif                    myDoc.Load(xmlKeyPath);
 84InBlock.gif                    myRSACSP.FromXmlString(myDoc.OuterXml);
 85InBlock.gif
 86InBlock.gif                    decryptedData = myRSACSP.Decrypt(dataToDecrypt, false);
 87ExpandedSubBlockEnd.gif                }

 88InBlock.gif                UnicodeEncoding myByteConverter = new UnicodeEncoding();
 89InBlock.gif                return myByteConverter.GetString(decryptedData);
 90ExpandedSubBlockEnd.gif            }

 91InBlock.gif            catch (System.Exception ex)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 93InBlock.gif                throw new ApplicationException("解密失败!\r\n" + ex.Message, ex);
 94ExpandedSubBlockEnd.gif            }

 95ExpandedSubBlockEnd.gif        }

 96InBlock.gif
 97InBlock.gif
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 99InBlock.gif        /// 对文本进行签名。
100InBlock.gif        /// </summary>
101InBlock.gif        /// <param name="xmlPrivateKeyPath">签名密钥(XML)的路径。</param>
102InBlock.gif        /// <param name="strMessage">待签名的文本。</param>
103ExpandedSubBlockEnd.gif        /// <returns>签名。</returns>

104InBlock.gif        public static string RSASignString(string xmlPrivateKeyPath, string strMessage)
105ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
106InBlock.gif            if (!File.Exists(xmlPrivateKeyPath))
107ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
108InBlock.gif                throw new System.IO.FileNotFoundException("签名密钥文件不存在。");
109ExpandedSubBlockEnd.gif            }

110InBlock.gif
111InBlock.gif            try
112ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
113InBlock.gif                UnicodeEncoding myByteConverter = new UnicodeEncoding();
114InBlock.gif
115InBlock.gif                byte[] dataToSign = myByteConverter.GetBytes(strMessage);
116InBlock.gif                byte[] signedData;
117InBlock.gif
118InBlock.gif                using (RSACryptoServiceProvider myRSACSP = new RSACryptoServiceProvider())
119ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
120InBlock.gif                    XmlDocument myDoc = new XmlDocument();
121InBlock.gif                    myDoc.Load(xmlPrivateKeyPath);
122InBlock.gif                    myRSACSP.FromXmlString(myDoc.OuterXml);
123InBlock.gif
124InBlock.gif                    signedData = myRSACSP.SignData(dataToSign, new SHA1CryptoServiceProvider());
125ExpandedSubBlockEnd.gif                }

126InBlock.gif                return myByteConverter.GetString(signedData);
127ExpandedSubBlockEnd.gif            }

128InBlock.gif            catch (System.Exception ex)
129ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
130InBlock.gif                throw new ApplicationException("签名失败!\r\n" + ex.Message, ex);
131ExpandedSubBlockEnd.gif            }

132ExpandedSubBlockEnd.gif        }

133InBlock.gif
134ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
135InBlock.gif        /// 验证签名。
136InBlock.gif        /// </summary>
137InBlock.gif        /// <param name="xmlPublicKeyPath">验证签名的公钥文件(XML)路径。</param>
138InBlock.gif        /// <param name="strMessage">消息原文。</param>
139InBlock.gif        /// <param name="strSignature">签名数据。</param>
140ExpandedSubBlockEnd.gif        /// <returns>验证结果。</returns>

141InBlock.gif        public static bool RSAVerifySignString(string xmlPublicKeyPath, string strMessage, string strSignature)
142ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
143InBlock.gif            if (!File.Exists(xmlPublicKeyPath))
144ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
145InBlock.gif                throw new System.IO.FileNotFoundException("验证密钥文件不存在。");
146ExpandedSubBlockEnd.gif            }

147InBlock.gif
148InBlock.gif            try
149ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
150InBlock.gif                UnicodeEncoding myByteConverter = new UnicodeEncoding();
151InBlock.gif
152InBlock.gif                byte[] dataToSign = myByteConverter.GetBytes(strMessage);
153InBlock.gif                byte[] signedData = myByteConverter.GetBytes(strSignature);
154InBlock.gif
155InBlock.gif                bool bolResult = false;
156InBlock.gif                using (RSACryptoServiceProvider myRSACSP = new RSACryptoServiceProvider())
157ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
158InBlock.gif                    XmlDocument myDoc = new XmlDocument();
159InBlock.gif                    myDoc.Load(xmlPublicKeyPath);
160InBlock.gif                    myRSACSP.FromXmlString(myDoc.OuterXml);
161InBlock.gif
162InBlock.gif                    bolResult = myRSACSP.VerifyData(dataToSign, new SHA1CryptoServiceProvider(), signedData);
163ExpandedSubBlockEnd.gif                }

164InBlock.gif                return bolResult;
165ExpandedSubBlockEnd.gif            }

166InBlock.gif            catch (System.Exception ex)
167ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
168InBlock.gif                throw new ApplicationException("验证签名失败!\r\n" + ex.Message, ex);
169ExpandedSubBlockEnd.gif            }

170ExpandedSubBlockEnd.gif        }

171ExpandedSubBlockEnd.gif    }

172InBlock.gif
173ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
174InBlock.gif    /// 用来生成密钥对。
175InBlock.gif    /// </summary>
176InBlock.gif    /// <remarks>
177InBlock.gif    /// 采用RSA算法。XML文件表示。
178ExpandedSubBlockEnd.gif    /// </remarks>

179InBlock.gif    public class KeyGenerator
180ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
181InBlock.gif        private XmlDocument _privateKey;
182InBlock.gif        private XmlDocument _publicKey;
183InBlock.gif
184ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
185InBlock.gif        /// 缺省构造,生成密钥对。
186ExpandedSubBlockEnd.gif        /// </summary>

187InBlock.gif        public KeyGenerator()
188ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
189InBlock.gif            //Use RSA Provider to generate public / private key pair
190InBlock.gif            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
191ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
192InBlock.gif                //  create the xml-docs
193InBlock.gif                _privateKey = new XmlDocument();
194InBlock.gif                _publicKey = new XmlDocument();
195InBlock.gif
196InBlock.gif                //  use RSA convenience methods to stuff public and private keys into dom's
197InBlock.gif                _privateKey.LoadXml(RSA.ToXmlString(true));
198InBlock.gif                _publicKey.LoadXml(RSA.ToXmlString(false));
199ExpandedSubBlockEnd.gif            }

200ExpandedSubBlockEnd.gif        }

201InBlock.gif
202ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
203InBlock.gif        /// 私钥。
204ExpandedSubBlockEnd.gif        /// </summary>

205InBlock.gif        public XmlDocument PrivateKey
206ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
207ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _privateKey; }
208ExpandedSubBlockEnd.gif        }

209InBlock.gif
210ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
211InBlock.gif        /// 公钥。
212ExpandedSubBlockEnd.gif        /// </summary>

213InBlock.gif        public XmlDocument PublicKey
214ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
215ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _publicKey; }
216ExpandedSubBlockEnd.gif        }

217InBlock.gif
218ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
219InBlock.gif        /// 生成加密型强随机非零值序列作为会话令牌。
220InBlock.gif        /// </summary>
221InBlock.gif        /// <remarks>
222InBlock.gif        /// 使用弱加密,16位。
223InBlock.gif        /// </remarks>
224ExpandedSubBlockEnd.gif        /// <returns>随机的令牌。</returns>

225InBlock.gif        public static string GenToken()
226ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
227InBlock.gif            byte[] random = new Byte[16];
228InBlock.gif            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
229InBlock.gif            rng.GetNonZeroBytes(random);
230InBlock.gif            UnicodeEncoding myByteConverter = new UnicodeEncoding();
231InBlock.gif            return myByteConverter.GetString(random);
232ExpandedSubBlockEnd.gif        }

233ExpandedSubBlockEnd.gif    }

234ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/bengxia/archive/2006/03/21/355235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值