Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
尚未分类 LINQ教程 Enterprise技术 性能优化/调试 水晶报表与打印安全与加密 图形图像 文件处理 基础教程 Web Services 内置对象 控件示例 正则表达式\采集 ADO.NET 缓存\泛型\线程 XML技术 Url重写\静态页 vs2008综合教程
当前位置: 主页 > ASP.NET教程 > 安全与加密 >

RSA公钥加密技术

时间:2009-11-15 20:45来源:未知 作者:admin 点击:

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

namespace Web
{
    public class RSA
    {
        int keySize;
        string publicKey;
        string privateKey;
        string publicKeyFileName;
        string privateKeyFileName;

        public RSA()
        {
            keySize = 1024;
            publicKeyFileName = "public.pk";
            privateKeyFileName = "private.pk";
            publicKey = ReadFile(publicKeyFileName);
            privateKey = ReadFile(privateKeyFileName);
            if (String.IsNullOrEmpty(publicKey) || String.IsNullOrEmpty(privateKey))
            {
                GenerateKey();
            }
        }

        private void GenerateKey()
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(keySize);

            publicKey = RSA.ToXmlString(true);
            privateKey = RSA.ToXmlString(false);

            if (SaveFile(privateKeyFileName, publicKey))
            {
                SaveFile(publicKeyFileName, privateKey);
            }
        }

        public string EncryptString(string text)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(keySize);
            RSA.FromXmlString(publicKey);
            byte[] bytes = Encoding.UTF32.GetBytes(text);
            int maxLength = (keySize / 8) - 42;
            int dataLength = bytes.Length;
            int iterations = dataLength / maxLength;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i <= iterations; i++)
            {
                byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];
                Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length);
                byte[] encryptedBytes = RSA.Encrypt(tempBytes, true);
                Array.Reverse(encryptedBytes);
                stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
            }
            return stringBuilder.ToString();
        }

        public string DecryptString(string text)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(keySize);
            RSA.FromXmlString(privateKey);
            int base64BlockSize = ((keySize / 8) % 3 != 0) ? (((keySize / 8) / 3) * 4) + 4 : ((keySize / 8) / 3) * 4;
            int iterations = text.Length / base64BlockSize;
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < iterations; i++)
            {
                byte[] encryptedBytes = Convert.FromBase64String(text.Substring(base64BlockSize * i, base64BlockSize));
                Array.Reverse(encryptedBytes);
                arrayList.AddRange(RSA.Decrypt(encryptedBytes, true));
            }
            return Encoding.UTF32.GetString(arrayList.ToArray(Type.GetType("System.Byte")) as byte[]);
        }

        private bool SaveFile(string fileName, string text)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath("/" + fileName);
                StreamWriter streamWriter = new StreamWriter(path, false);
                if (text != null) streamWriter.Write(text);
                streamWriter.Close();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private string ReadFile(string fileName)
        {
            string text = null;
            string path = HttpContext.Current.Server.MapPath("/" + fileName);
            if (File.Exists(path))
            {
                StreamReader streamReader = new StreamReader(path, true);
                text = streamReader.ReadToEnd();
                streamReader.Close();
            }
            return text;
        }
    }
}

(责任编辑:admin)
Tags:RSA公钥加密
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价
  • ASP.NET中散列加密密码

    private void LoginButton_Click(object sender,System.EventArgs e) { String sql=St...

  • ASP.NET程序如何防止被注入(整站通用)

    防止sql注入,通常一个一个文件修改不仅麻烦而且还有漏掉的危险,下面我说一上如何从整个系统防止注入。 做到以下三步,相信的程序将会比较安全了,而且对整个网站的维...

  • asp.net实现邮箱验证功能,通过邮箱链接激活该帐号

    当一个用户进行注册的时候同时发送一个邮件到注册人的邮箱,通过发送的邮箱链接来激活该帐号,具体用asp.net方法的实现代码,贴在这了,有需要的可以参考! pub...

  • ASP.NET中使用MD5和SHA1算法加密

    你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在ASP.NET中实现加密非常容易...

  • ASP.Net中的RSA加密

    在我们实际运用中,加密是保证数据安全的重要手段。以前使用ASP时,对数据加密可以使用MD5和SHA1算法,这两种算法虽然快捷有效,但是无法对通过它们加密的密文进...

  • asp.net中用md5加密简单例子

    在实际应用中,经常需要将虚拟路径转换为实际路径.Request对象的MapPath方法可以解决该问题.下面的例子显示MapPath方法的使用. script l...