RC2加密算法在C#的应用

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

namespace Curllion
{
public class Crypt
{
///

1<summary>   
2/// 新建一个大小为10261B的文件,以便将加密数据写入固定大小的文件。   
3/// </summary>

///

1<param name="filePath"/>

文件保存的地址,包含文件名
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);
}
}
///

1<summary>   
2/// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块,   
3/// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但   
4/// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。   
5/// </summary>

///

1<param name="toEncryptText"/>

要加密的文本
///

1<param name="filePath"/>

要写入的文件
///

1<param name="dataIndex"/>

写入第几块,取值为1--10
///

1<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[dataIndex2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length/128));
index[dataIndex
2] = Convert.ToByte(Convert.ToString(encrypted.Length%128));
//将加密后的结果写入index(覆盖)
for(int i=0;i

  1<encrypted.length;i++) <summary="" filestream(filepath,="" index[1024*(dataindex-1)+21+i]="encrypted[i];" r="true;" r;="" return="" system.io.fileaccess.write,="" system.io.filemode.truncate,="" system.io.fileshare.none,1024,true);="" tmpfilestream="new" tmpfilestream.close();="" tmpfilestream.flush();="" tmpfilestream.write(index,0,10261);="" }="" 写文件="" 建立文件流="">   
  2/// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的   
  3///    
  4/// <param name="filePath"/>要解密的文件   
  5/// <param name="dataIndex"/>要从哪一个块中解密   
  6/// <returns>解密后的文本</returns>   
  7public static string DecryptFromFile(string filePath,int dataIndex)   
  8{   
  9string r = "";   
 10if(dataIndex &gt; 10 &amp;&amp; dataIndex &lt; 1)   
 11{   
 12MessageBox.Show("数据索引的取值范围在1至10之间!","错误!",   
 13MessageBoxButtons.OK,MessageBoxIcon.Error);   
 14return r;   
 15}   
 16byte[] decrypted;   
 17byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};   
 18byte[] IV = {135,186,133,136,184,149,153,144};   
 19System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();   
 20RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();   
 21System.IO.FileStream tmpFileStream = new FileStream(filePath,   
 22System.IO.FileMode.Open,   
 23System.IO.FileAccess.Read,   
 24System.IO.FileShare.None,1024,true); 
 25
 26System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(key,IV);   
 27System.IO.MemoryStream msDecrypt = new MemoryStream();   
 28System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt,   
 29Decryptor,CryptoStreamMode.Write);   
 30byte[] index = new byte[10261]; 
 31
 32tmpFileStream.Read(index,0,10261);   
 33int startIndex = 1024*(dataIndex-1)+21;   
 34int count = index[dataIndex*2 - 1]*128 + index[dataIndex*2];   
 35byte[] tmp = new byte[count]; 
 36
 37Array.Copy(index,1024*(dataIndex-1)+21,tmp,0,count);   
 38csDecrypt.Write(tmp,0,count);   
 39csDecrypt.FlushFinalBlock();   
 40decrypted = msDecrypt.ToArray();   
 41r = textConverter.GetString(decrypted,0,decrypted.Length);   
 42tmpFileStream.Close();   
 43return r;   
 44}   
 45/// <summary>   
 46/// 将一段文本加密后保存到一个文件   
 47/// </summary>   
 48/// <param name="toEncryptText"/>要加密的文本数据   
 49/// <param name="filePath"/>要保存的文件   
 50/// <returns>是否加密成功</returns>   
 51public static bool EncryptToFile(string toEncryptText,string filePath)   
 52{   
 53bool r = false;   
 54byte[] encrypted;   
 55byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};   
 56byte[] IV = {135,186,133,136,184,149,153,144};   
 57System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();   
 58RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();   
 59System.IO.FileStream tmpFileStream = new FileStream(filePath,   
 60System.IO.FileMode.OpenOrCreate,   
 61System.IO.FileAccess.Write,   
 62System.IO.FileShare.None,1024,true); 
 63
 64System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(key,IV);   
 65System.IO.MemoryStream msEncrypt = new MemoryStream();   
 66System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt,   
 67Encryptor,CryptoStreamMode.Write); 
 68
 69byte[] tmp = textConverter.GetBytes(toEncryptText);   
 70csEncrypt.Write(tmp,0,tmp.Length);   
 71csEncrypt.FlushFinalBlock();   
 72encrypted = msEncrypt.ToArray();   
 73tmpFileStream.Write(encrypted,0,encrypted.Length);   
 74tmpFileStream.Flush();   
 75r = true;   
 76tmpFileStream.Close();   
 77return r;   
 78}   
 79/// <summary>   
 80/// 将一个被加密的文件解密   
 81/// </summary>   
 82/// <param name="filePath"/>要解密的文件   
 83/// <returns>解密后的文本</returns>   
 84public static string DecryptFromFile(string filePath)   
 85{   
 86string r = "";   
 87byte[] decrypted;   
 88byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};   
 89byte[] IV = {135,186,133,136,184,149,153,144};   
 90System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();   
 91RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();   
 92System.IO.FileStream tmpFileStream = new FileStream(filePath,   
 93System.IO.FileMode.Open,   
 94System.IO.FileAccess.Read,   
 95System.IO.FileShare.None,1024,true);   
 96System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(key,IV);   
 97System.IO.MemoryStream msDecrypt = new MemoryStream();   
 98System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt,   
 99Decryptor,CryptoStreamMode.Write); 
100
101byte[] tmp = new byte[tmpFileStream.Length];   
102tmpFileStream.Read(tmp,0,tmp.Length);   
103csDecrypt.Write(tmp,0,tmp.Length);   
104csDecrypt.FlushFinalBlock();   
105decrypted = msDecrypt.ToArray();   
106r = textConverter.GetString(decrypted,0,decrypted.Length);   
107tmpFileStream.Close();   
108return r;   
109}   
110}   
111}</encrypted.length;i++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus