初来乍到

希望在学习C#的过程中,能和大家一起分享经验和快乐

用户操作
[即时聊天] [发私信] [加为好友]
张建平ID:curllion
21132次访问,排名5941,好友1人,关注者2人。
你好
curllion的文章
原创 21 篇
翻译 0 篇
转载 2 篇
评论 18 篇
curllion的公告
Curllion
QQ:35277427
MSN:curllion@hotmail.com
EMAIL:curllion@gmail.com

给我留言

我的新blog
最近评论
basil:写的不错,不过感觉权限的验证写的不是很清楚啊,有点晕
卷毛:第一、二行就是VS2003中的水晶报表的发布时的注册码

第三行就是VS2005中的KeyCode,在“CrystalReport10_rdc_license.msm”的“KeyCode”属性中输入就可以了
吴下阿蒙:最后的Crystal Reports for Visual Studio 2005怎么没有注册码?
sdd:呵呵!还不错!
多多的写些让我们交流!
panqy82:你好,之前我看书迷迷糊糊的就是不太明白,看了你的帖子有种豁然开朗的感觉,不错。
在ColorChange(this)上面加这条语句会更好点,如下:
if(ColorChange!=null)
文章分类
收藏
    相册
    抵制日货(二)
    抵制日货(一)
    链接用图
    博客链接交换
    小馬过桥(RSS)
    高手BLOG
    Krystalware
    小虫的窝
    汤建军
    程序人生(RSS)
    朋友与同学的BLOG
    尹礼国
    我的新blog(RSS)
    网站链接
    Delphi盒子
    Delphi盒子论坛
    公司主页
    存档
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 RC2加密算法在CSharp的应用收藏

    新一篇: RC2加密算法在CSharp的应用----完善版 | 旧一篇: 让人喷血的经典广告

    using System;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;

    namespace Curllion
    {
     public class Crypt
     {
      /// <summary>
      /// 新建一个大小为10261B的文件,以便将加密数据写入固定大小的文件。
      /// </summary>
      /// <param name="filePath">文件保存的地址,包含文件名</param>
      public static void InitBinFile(string filePath)
      {
       byte[] tmp = new byte[10261];
       try  //创建文件流,将其内容全部写入0
       {
        System.IO.FileStream writeFileStream = new FileStream(filePath,
         System.IO.FileMode.Create,
         System.IO.FileAccess.Write,
         System.IO.FileShare.None,512,false);

        for(int i = 0 ;i< 10261;i++)
         tmp[i] = 0; 
        writeFileStream.Write(tmp,0,10261);
        writeFileStream.Flush();
        writeFileStream.Close();
       }
       catch(System.IO.IOException)
       {
        MessageBox.Show("文件操作错误!","错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);
       }
      }
      /// <summary>
      /// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块,
      /// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但
      /// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。
      /// </summary>
      /// <param name="toEncryptText">要加密的文本</param>
      /// <param name="filePath">要写入的文件</param>
      /// <param name="dataIndex">写入第几块,取值为1--10</param>
      /// <returns>是否操作成功</returns>
      public static bool EncryptToFile(string toEncryptText,string filePath,int dataIndex)
      {
       bool r = false;
       if(dataIndex > 10 && dataIndex < 1)
       {
        MessageBox.Show("数据索引的取值范围在1至10之间!","错误!",
         MessageBoxButtons.OK,MessageBoxIcon.Error);
        return r;
       }
       byte[] encrypted;  
       //初始化向量
       byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};   
       //密匙
       byte[] IV = {135,186,133,136,184,149,153,144};
       //创建UTF-16 编码,用来在byte[]和string之间转换
       System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();
       //创建RC2服务
          RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
       //打开要写入的文件,主要是为了保持原文件的内容不丢失
       System.IO.FileStream tmpFileStream= new FileStream(filePath,
        System.IO.FileMode.Open,
        System.IO.FileAccess.Read,
        System.IO.FileShare.None,1024,true);

       byte[] index = new byte[10261];
       //将读取的内容写到byte数组
       tmpFileStream.Read(index,0,10261);
       tmpFileStream.Close();
       //定义基本的加密转换运算
       System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(key,IV);
       System.IO.MemoryStream msEncrypt = new MemoryStream();
       //在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。
       System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt,
        Encryptor,CryptoStreamMode.Write);
       //将要加密的文本转换成UTF-16 编码,保存在tmp数组。
       byte[] tmp = textConverter.GetBytes(toEncryptText);
       //将tmp输入csEncrypt,将通过Encryptor来加密。
       csEncrypt.Write(tmp,0,tmp.Length);
       //输出到msEnctypt
       csEncrypt.FlushFinalBlock();
       //将流转成byte[]
       encrypted = msEncrypt.ToArray();
       if(encrypted.Length>1024)
       {
        MessageBox.Show("加密后,数据长度大于1KB,无法保存");
        return false;
       }
       //得到加密后数据的大小,将结果存在指定的位置。
       index[dataIndex*2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length/128));
       index[dataIndex*2]     = Convert.ToByte(Convert.ToString(encrypted.Length%128));
       //将加密后的结果写入index(覆盖)
       for(int i=0;i<encrypted.Length;i++)
        index[1024*(dataIndex-1)+21+i]=encrypted[i];
       //建立文件流
       tmpFileStream = new FileStream(filePath,
        System.IO.FileMode.Truncate,
        System.IO.FileAccess.Write,
        System.IO.FileShare.None,1024,true);
       //写文件
       tmpFileStream.Write(index,0,10261);
       tmpFileStream.Flush();
       r = true;
       tmpFileStream.Close();
       return r;
      }
      /// <summary>
      /// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
      /// </summary>
      /// <param name="filePath">要解密的文件</param>
      /// <param name="dataIndex">要从哪一个块中解密</param>
      /// <returns>解密后的文本</returns>
      public static string DecryptFromFile(string filePath,int dataIndex)
      {
       string r = "";
       if(dataIndex > 10 && dataIndex < 1)
       {
        MessageBox.Show("数据索引的取值范围在1至10之间!","错误!",
         MessageBoxButtons.OK,MessageBoxIcon.Error);
        return r;
       }
       byte[] decrypted;
       byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};
       byte[] IV = {135,186,133,136,184,149,153,144};
       System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();
       RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
       System.IO.FileStream tmpFileStream = new FileStream(filePath,
        System.IO.FileMode.Open,
        System.IO.FileAccess.Read,
        System.IO.FileShare.None,1024,true);

       System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(key,IV);
       System.IO.MemoryStream msDecrypt = new MemoryStream();
       System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt,
        Decryptor,CryptoStreamMode.Write);
       byte[] index = new byte[10261];

       tmpFileStream.Read(index,0,10261);
       int startIndex = 1024*(dataIndex-1)+21;
       int count      = index[dataIndex*2 - 1]*128 + index[dataIndex*2];
       byte[] tmp     = new byte[count];

       Array.Copy(index,1024*(dataIndex-1)+21,tmp,0,count);
       csDecrypt.Write(tmp,0,count);
       csDecrypt.FlushFinalBlock();
       decrypted = msDecrypt.ToArray();
       r = textConverter.GetString(decrypted,0,decrypted.Length);
       tmpFileStream.Close();
       return r;
      }
      /// <summary>
      /// 将一段文本加密后保存到一个文件
      /// </summary>
      /// <param name="toEncryptText">要加密的文本数据</param>
      /// <param name="filePath">要保存的文件</param>
      /// <returns>是否加密成功</returns>
      public static bool EncryptToFile(string toEncryptText,string filePath)
      {
       bool r = false;
       byte[] encrypted;
       byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};
       byte[] IV = {135,186,133,136,184,149,153,144};
       System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();
       RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
       System.IO.FileStream tmpFileStream = new FileStream(filePath,
        System.IO.FileMode.OpenOrCreate,
        System.IO.FileAccess.Write,
        System.IO.FileShare.None,1024,true);

       System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(key,IV);
       System.IO.MemoryStream msEncrypt = new MemoryStream();
       System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt,
        Encryptor,CryptoStreamMode.Write);

       byte[] tmp = textConverter.GetBytes(toEncryptText);
       csEncrypt.Write(tmp,0,tmp.Length);
       csEncrypt.FlushFinalBlock();   
       encrypted = msEncrypt.ToArray();
       tmpFileStream.Write(encrypted,0,encrypted.Length);
       tmpFileStream.Flush();
       r = true;
       tmpFileStream.Close();
       return r;
      }
      /// <summary>
      /// 将一个被加密的文件解密
      /// </summary>
      /// <param name="filePath">要解密的文件</param>
      /// <returns>解密后的文本</returns>
      public static string DecryptFromFile(string filePath)
      {
       string r = "";
       byte[] decrypted;
       byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};
       byte[] IV = {135,186,133,136,184,149,153,144};
       System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();
       RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
       System.IO.FileStream tmpFileStream = new FileStream(filePath,
        System.IO.FileMode.Open,
        System.IO.FileAccess.Read,
        System.IO.FileShare.None,1024,true);
       System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(key,IV);
       System.IO.MemoryStream msDecrypt = new MemoryStream();
       System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt,
        Decryptor,CryptoStreamMode.Write);

       byte[] tmp = new byte[tmpFileStream.Length];
       tmpFileStream.Read(tmp,0,tmp.Length);
       csDecrypt.Write(tmp,0,tmp.Length);
       csDecrypt.FlushFinalBlock();
       decrypted = msDecrypt.ToArray();    
       r = textConverter.GetString(decrypted,0,decrypted.Length);
       tmpFileStream.Close();
       return r;
      }
     }
    }


    请参阅:RC2加密算法在C#的应用----完善版

    发表于 @ 2005年03月15日 10:37:00|评论(loading...)|编辑

    新一篇: RC2加密算法在CSharp的应用----完善版 | 旧一篇: 让人喷血的经典广告

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © curllion