?? rc2.cs
字號:
?using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace ZYBEncrypt
{
/// <summary>
/// RC2
/// </summary>
public class RC2_
{
private RC2 rc;
public string Key;
public string IV;
/// <summary>
/// 對稱加密類的構造函數
/// </summary>
public RC2_(string key)
{
rc = new RC2CryptoServiceProvider();
Key = key;
IV = "#$^%&&*Yisifhsfjsljfslhgosdshf26382837sdfjskhf97(*&(*";
}
/// <summary>
/// 對稱加密類的構造函數
/// </summary>
public RC2_(string key, string iv)
{
rc = new RC2CryptoServiceProvider();
Key = key;
IV = iv;
}
/// <summary>
/// 獲得密鑰
/// </summary>
/// <returns>密鑰</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
rc.GenerateKey();
byte[] bytTemp = rc.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 獲得初始向量IV
/// </summary>
/// <returns>初試向量IV</returns>
private byte[] GetLegalIV()
{
string sTemp = IV;
rc.GenerateIV();
byte[] bytTemp = rc.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name="Source">待加密的串</param>
/// <returns>經過加密的串</returns>
public string Encrypt(string Source)
{
try
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
rc.Key = GetLegalKey();
rc.IV = GetLegalIV();
ICryptoTransform encrypto = rc.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
catch (Exception ex)
{
throw new Exception("在文件加密的時候出現錯誤!錯誤提示: \n" + ex.Message);
}
}
/// <summary>
/// 解密方法
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>經過解密的串</returns>
public string Decrypt(string Source)
{
try
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
rc.Key = GetLegalKey();
rc.IV = GetLegalIV();
ICryptoTransform encrypto = rc.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("在文件解密的時候出現錯誤!錯誤提示: \n" + ex.Message);
}
}
/// <summary>
/// 加密方法byte[] to byte[]
/// </summary>
/// <param name="Source">待加密的byte數組</param>
/// <returns>經過加密的byte數組</returns>
public byte[] Encrypt(byte[] Source)
{
try
{
byte[] bytIn = Source;
MemoryStream ms = new MemoryStream();
rc.Key = GetLegalKey();
rc.IV = GetLegalIV();
ICryptoTransform encrypto = rc.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return bytOut;
}
catch (Exception ex)
{
throw new Exception("在文件加密的時候出現錯誤!錯誤提示: \n" + ex.Message);
}
}
/// <summary>
/// 解密方法byte[] to byte[]
/// </summary>
/// <param name="Source">待解密的byte數組</param>
/// <returns>經過解密的byte數組</returns>
public byte[] Decrypt(byte[] Source)
{
try
{
byte[] bytIn = Source;
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
rc.Key = GetLegalKey();
rc.IV = GetLegalIV();
ICryptoTransform encrypto = rc.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return UTF8Encoding.UTF8.GetBytes(sr.ReadToEnd());
}
catch (Exception ex)
{
throw new Exception("在文件解密的時候出現錯誤!錯誤提示: \n" + ex.Message);
}
}
/// <summary>
/// 加密方法File to File
/// </summary>
/// <param name="inFileName">待加密文件的路徑</param>
/// <param name="outFileName">待加密后文件的輸出路徑</param>
public void Encrypt(string inFileName, string outFileName)
{
try
{
FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
rc.Key = GetLegalKey();
rc.IV = GetLegalIV();
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len;
ICryptoTransform encrypto = rc.CreateEncryptor();
CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
cs.Write(bin, 0, len);
rdlen = rdlen + len;
}
cs.Close();
fout.Close();
fin.Close();
}
catch (Exception ex)
{
throw new Exception("在文件加密的時候出現錯誤!錯誤提示: \n" + ex.Message);
}
}
/// <summary>
/// 解密方法File to File
/// </summary>
/// <param name="inFileName">待解密文件的路徑</param>
/// <param name="outFileName">待解密后文件的輸出路徑</param>
public void Decrypt(string inFileName, string outFileName)
{
try
{
FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len;
rc.Key = GetLegalKey();
rc.IV = GetLegalIV();
ICryptoTransform encrypto = rc.CreateDecryptor();
CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
cs.Write(bin, 0, len);
rdlen = rdlen + len;
}
cs.Close();
fout.Close();
fin.Close();
}
catch (Exception ex)
{
throw new Exception("在文件解密的時候出現錯誤!錯誤提示: \n" + ex.Message);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -