常用编码工具类,支持base64,md5,des,crc32

简介:

常用编码工具类,支持base64,md5,des,crc32
支持 从文件 到文件, 从字符串 到字符串的方式操作
以下是源码
None.gif using System;
None.gif using System.IO;
None.gif using System.Security;
None.gif using System.Security.Cryptography;
None.gif using System.Runtime.InteropServices;
None.gif using System.Text;
None.gif
ExpandedBlockStart.gif ContractedBlock.gif namespace YNEC.Services.Encrypt dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// CRC 效验
InBlock.gif
/// 快速检测算法
ExpandedSubBlockEnd.gif
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic class CRC32dot.gif{
InBlock.gif
InBlock.gifprotected ulong[] crc32Table;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 构造:初始化效验表
ExpandedSubBlockEnd.gif
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic CRC32() dot.gif{
InBlock.gifconst ulong ulPolynomial = 0xEDB88320;
InBlock.gifulong dwCrc;
InBlock.gif crc32Table = new ulong[256];
InBlock.gifint i,j;
ExpandedSubBlockStart.gifContractedSubBlock.giffor(i = 0; i < 256; i++) dot.gif{
InBlock.gif dwCrc = (ulong)i;
ExpandedSubBlockStart.gifContractedSubBlock.giffor(j = 8; j > 0; j--) dot.gif{
InBlock.gifif((dwCrc & 1)==1)
InBlock.gif dwCrc = (dwCrc >> 1) ^ ulPolynomial;
InBlock.gifelse
InBlock.gif dwCrc >>= 1;
ExpandedSubBlockEnd.gif }

InBlock.gif crc32Table[i] = dwCrc;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 字节数组效验
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="buffer">ref 字节数组</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic ulong ByteCRC(ref byte[] buffer) dot.gif{
InBlock.gifulong ulCRC = 0xffffffff;
InBlock.gifulong len;
InBlock.gif len = (ulong)buffer.Length;
ExpandedSubBlockStart.gifContractedSubBlock.giffor (ulong buffptr=0; buffptr < len; buffptr++) dot.gif{
InBlock.gifulong tabPtr = ulCRC & 0xFF;
InBlock.gif tabPtr = tabPtr ^ buffer[buffptr];
InBlock.gif ulCRC = ulCRC >> 8;
InBlock.gif ulCRC = ulCRC ^ crc32Table[tabPtr];
ExpandedSubBlockEnd.gif }

InBlock.gifreturn ulCRC ^ 0xffffffff;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 字符串效验
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputString">字符串</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic ulong StringCRC(string sInputString)dot.gif{
InBlock.gifbyte[] buffer = Encoding.Default.GetBytes(sInputString);
InBlock.gifreturn ByteCRC(ref buffer);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 文件效验
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputFilename">输入文件</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic ulong FileCRC(string sInputFilename)dot.gif{
InBlock.gif FileStream inFile = new System.IO.FileStream(sInputFilename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
InBlock.gifbyte[] bInput = new byte[inFile.Length];
InBlock.gif inFile.Read(bInput,0,bInput.Length);
InBlock.gif inFile.Close();
InBlock.gif
InBlock.gifreturn ByteCRC(ref bInput);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// MD5 无逆向编码
InBlock.gif
/// 获取唯一特征串,可用于密码加密
InBlock.gif
/// (无法还原)
ExpandedSubBlockEnd.gif
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic class MD5 dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic MD5()dot.gif{
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 获取字符串的特征串
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputString">输入文本</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic string HashString(string sInputString)dot.gif{
InBlock.gif System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
InBlock.gifstring encoded = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(sInputString))).Replace("-","");
InBlock.gifreturn encoded;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 获取文件的特征串
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputFilename">输入文件</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic string HashFile(string sInputFilename)dot.gif{
InBlock.gif System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
InBlock.gif FileStream inFile = new System.IO.FileStream(sInputFilename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
InBlock.gifbyte[] bInput = new byte[inFile.Length];
InBlock.gif inFile.Read(bInput,0,bInput.Length);
InBlock.gif inFile.Close();
InBlock.gif
InBlock.gifstring encoded = BitConverter.ToString(md5.ComputeHash(bInput)).Replace("-","");
InBlock.gifreturn encoded;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// Base64 UUEncoded 编码
InBlock.gif
/// 将二进制编码为ASCII文本,用于网络传输
InBlock.gif
/// (可还原)
ExpandedSubBlockEnd.gif
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic class BASE64dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic BASE64()dot.gif{
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 解码字符串
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputString">输入文本</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic string DecryptString(string sInputString)dot.gif{
InBlock.gifchar[] sInput = sInputString.ToCharArray();
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gifbyte[] bOutput = System.Convert.FromBase64String(sInputString);
InBlock.gifreturn Encoding.Default.GetString(bOutput);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch ( System.ArgumentNullException ) dot.gif{
InBlock.gif//base 64 字符数组为null
InBlock.gif
return "";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch ( System.FormatException ) dot.gif{
InBlock.gif//长度错误,无法整除4
InBlock.gif
return "";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 编码字符串
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputString">输入文本</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic string EncryptString(string sInputString)dot.gif{
InBlock.gifbyte[] bInput = Encoding.Default.GetBytes(sInputString);
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gifreturn System.Convert.ToBase64String(bInput,0,bInput.Length);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (System.ArgumentNullException) dot.gif{
InBlock.gif//二进制数组为NULL.
InBlock.gif
return "";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (System.ArgumentOutOfRangeException) dot.gif{
InBlock.gif//长度不够
InBlock.gif
return "";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 解码文件
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputFilename">输入文件</param>
ExpandedSubBlockEnd.gif
/// <param name="sOutputFilename">输出文件</param>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void DecryptFile(string sInputFilename,string sOutputFilename) dot.gif{
InBlock.gif System.IO.StreamReader inFile;
InBlock.gifchar[] base64CharArray;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif inFile = new System.IO.StreamReader(sInputFilename,
InBlock.gif System.Text.Encoding.ASCII);
InBlock.gif base64CharArray = new char[inFile.BaseStream.Length];
InBlock.gif inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
InBlock.gif inFile.Close();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch dot.gif{//(System.Exception exp) {
InBlock.gif
return;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif// 转换Base64 UUEncoded为二进制输出
InBlock.gif
byte[] binaryData;
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif binaryData =
InBlock.gif System.Convert.FromBase64CharArray(base64CharArray,
InBlock.gif 0,
InBlock.gif base64CharArray.Length);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch ( System.ArgumentNullException ) dot.gif{
InBlock.gif//base 64 字符数组为null
InBlock.gif
return;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch ( System.FormatException ) dot.gif{
InBlock.gif//长度错误,无法整除4
InBlock.gif
return;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif// 写输出数据
InBlock.gif
System.IO.FileStream outFile;
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif outFile = new System.IO.FileStream(sOutputFilename,
InBlock.gif System.IO.FileMode.Create,
InBlock.gif System.IO.FileAccess.Write);
InBlock.gif outFile.Write(binaryData, 0, binaryData.Length);
InBlock.gif outFile.Close();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatchdot.gif{// (System.Exception exp) {
InBlock.gif
//流错误
ExpandedSubBlockEnd.gif
}

InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 编码文件
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputFilename">输入文件</param>
ExpandedSubBlockEnd.gif
/// <param name="sOutputFilename">输出文件</param>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void EncryptFile(string sInputFilename,string sOutputFilename)dot.gif{
InBlock.gif
InBlock.gif System.IO.FileStream inFile;
InBlock.gifbyte[] binaryData;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif inFile = new System.IO.FileStream(sInputFilename,
InBlock.gif System.IO.FileMode.Open,
InBlock.gif System.IO.FileAccess.Read);
InBlock.gif binaryData = new Byte[inFile.Length];
InBlock.giflong bytesRead = inFile.Read(binaryData, 0,
InBlock.gif (int) inFile.Length);
InBlock.gif inFile.Close();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch dot.gif{ //(System.Exception exp) {
InBlock.gif
return;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif// 转换二进制输入为Base64 UUEncoded输出
InBlock.gif
// 每3个字节在源数据里作为4个字节
InBlock.gif
long arrayLength = (long) ((4.0d/3.0d) * binaryData.Length);
InBlock.gif
InBlock.gif// 如果无法整除4
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (arrayLength % 4 != 0) dot.gif{
InBlock.gif arrayLength += 4 - arrayLength % 4;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifchar[] base64CharArray = new char[arrayLength];
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif System.Convert.ToBase64CharArray(binaryData,
InBlock.gif 0,
InBlock.gif binaryData.Length,
InBlock.gif base64CharArray,
InBlock.gif 0);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (System.ArgumentNullException) dot.gif{
InBlock.gif//二进制数组为NULL.
InBlock.gif
return;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatch (System.ArgumentOutOfRangeException) dot.gif{
InBlock.gif//长度不够
InBlock.gif
return;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif// 写UUEncoded数据到文件内
InBlock.gif
System.IO.StreamWriter outFile;
ExpandedSubBlockStart.gifContractedSubBlock.giftry dot.gif{
InBlock.gif outFile = new System.IO.StreamWriter(sOutputFilename,
InBlock.giffalse,
InBlock.gif System.Text.Encoding.ASCII);
InBlock.gif outFile.Write(base64CharArray);
InBlock.gif outFile.Close();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gifcatchdot.gif{// (System.Exception exp) {
InBlock.gif
//文件流出错
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// DES 加密
InBlock.gif
/// 支持Key(钥匙)加密变化
InBlock.gif
/// 支持还原
InBlock.gif
///
InBlock.gif
/// 演示操作:
InBlock.gif
/// // 64位,8个字节
InBlock.gif
/// string sSecretKey;
InBlock.gif
///
InBlock.gif
/// // 获取Key
InBlock.gif
/// sSecretKey = GenerateKey();
InBlock.gif
///
InBlock.gif
/// // 托管
InBlock.gif
/// GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );
InBlock.gif
///
InBlock.gif
/// // 加密文件
InBlock.gif
/// EncryptFile(@"C:\MyData.txt",
InBlock.gif
/// @"C:\Encrypted.txt",
InBlock.gif
/// sSecretKey);
InBlock.gif
///
InBlock.gif
/// // 解密文件
InBlock.gif
/// DecryptFile(@"C:\Encrypted.txt",
InBlock.gif
/// @"C:\Decrypted.txt",
InBlock.gif
/// sSecretKey);
InBlock.gif
///
InBlock.gif
/// // 释放托管内容
InBlock.gif
/// ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
InBlock.gif
/// gch.Free();
ExpandedSubBlockEnd.gif
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic class DES dot.gif{
InBlock.gif [DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
InBlock.gifpublic static extern bool ZeroMemory(IntPtr Destination, int Length);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic DES() dot.gif{
InBlock.gif//
InBlock.gif
// TODO: 在此处添加构造函数逻辑
InBlock.gif
//
ExpandedSubBlockEnd.gif
}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 创建Key
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns></returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic string GenerateKey() dot.gif{
InBlock.gif// 创建一个DES 算法的实例。自动产生Key
InBlock.gif
DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
InBlock.gif
InBlock.gif// 返回自动创建的Key 用于加密
InBlock.gif
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 加密字符串
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputString">输入字符</param>
InBlock.gif
/// <param name="sKey">Key</param>
ExpandedSubBlockEnd.gif
/// <returns>加密结果</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic string EncryptString(string sInputString,string sKey)dot.gif{
InBlock.gifbyte[] data = Encoding.Default.GetBytes(sInputString);
InBlock.gifbyte[] result;
InBlock.gif DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
InBlock.gif DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
InBlock.gif DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
InBlock.gif ICryptoTransform desencrypt = DES.CreateEncryptor();
InBlock.gif result = desencrypt.TransformFinalBlock(data,0,data.Length);
InBlock.gif
InBlock.gifstring desString = "";
ExpandedSubBlockStart.gifContractedSubBlock.giffor(int i=0;i<result.Length;i++)dot.gif{
InBlock.gif desString += result[i].ToString() + "-";
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif//return desString.TrimEnd('-');
InBlock.gif
return BitConverter.ToString(result);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 解密字符串
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputString">输入字符</param>
InBlock.gif
/// <param name="sKey">Key</param>
ExpandedSubBlockEnd.gif
/// <returns>解密结果</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic string DecryptString(string sInputString,string sKey)dot.gif{
InBlock.gifstring[] sInput = sInputString.Split("-".ToCharArray());
InBlock.gifbyte[] data = new byte[sInput.Length];
InBlock.gifbyte[] result;
InBlock.giffor(int i=0;i<sInput.Length;i++)
InBlock.gif data[i] = byte.Parse(sInput[i],System.Globalization.NumberStyles.HexNumber);
InBlock.gif
InBlock.gif DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
InBlock.gif DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
InBlock.gif DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
InBlock.gif ICryptoTransform desencrypt = DES.CreateDecryptor();
InBlock.gif result = desencrypt.TransformFinalBlock(data,0,data.Length);
InBlock.gifreturn Encoding.Default.GetString(result);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 加密文件
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputFilename">输入文件</param>
InBlock.gif
/// <param name="sOutputFilename">输出文件</param>
ExpandedSubBlockEnd.gif
/// <param name="sKey">Key</param>

InBlock.gifpublic void EncryptFile(string sInputFilename,
InBlock.gifstring sOutputFilename,
ExpandedSubBlockStart.gifContractedSubBlock.gifstring sKey) dot.gif{
InBlock.gif FileStream fsInput = new FileStream(sInputFilename,
InBlock.gif FileMode.Open,
InBlock.gif FileAccess.Read);
InBlock.gif
InBlock.gif FileStream fsEncrypted = new FileStream(sOutputFilename,
InBlock.gif FileMode.Create,
InBlock.gif FileAccess.Write);
InBlock.gif DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
InBlock.gif DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
InBlock.gif DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
InBlock.gif ICryptoTransform desencrypt = DES.CreateEncryptor();
InBlock.gif CryptoStream cryptostream = new CryptoStream(fsEncrypted,
InBlock.gif desencrypt,
InBlock.gif CryptoStreamMode.Write);
InBlock.gif
InBlock.gifbyte[] bytearrayinput = new byte[fsInput.Length];
InBlock.gif fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
InBlock.gif cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
InBlock.gif cryptostream.Close();
InBlock.gif fsInput.Close();
InBlock.gif fsEncrypted.Close();
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 解密文件
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sInputFilename">输入文件</param>
InBlock.gif
/// <param name="sOutputFilename">输出文件</param>
ExpandedSubBlockEnd.gif
/// <param name="sKey">Key</param>

InBlock.gifpublic void DecryptFile(string sInputFilename,
InBlock.gifstring sOutputFilename,
ExpandedSubBlockStart.gifContractedSubBlock.gifstring sKey) dot.gif{
InBlock.gif DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
InBlock.gif DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
InBlock.gif DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
InBlock.gif
InBlock.gif FileStream fsread = new FileStream(sInputFilename,
InBlock.gif FileMode.Open,
InBlock.gif FileAccess.Read);
InBlock.gif ICryptoTransform desdecrypt = DES.CreateDecryptor();
InBlock.gif CryptoStream cryptostreamDecr = new CryptoStream(fsread,
InBlock.gif desdecrypt,
InBlock.gif CryptoStreamMode.Read);
InBlock.gif StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
InBlock.gif fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
InBlock.gif fsDecrypted.Flush();
InBlock.gif fsDecrypted.Close();
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}



本文转自suifei博客园博客,原文链接http://www.cnblogs.com/Chinasf/archive/2005/05/18/157910.html,如需转载请自行联系原作者
相关文章
|
23天前
|
Java 数据安全/隐私保护
des加密+base64编码,base64解码+des解密
des加密+base64编码,base64解码+des解密
22 0
|
6月前
|
存储 算法 安全
【MD5】什么是MD5?md5的简要描述
【MD5】什么是MD5?md5的简要描述
105 0
|
9月前
|
算法 网络安全 数据库
MD5 Encryption Of String ( UTF-8 ) / UE4 MD5 加密
MD5 Encryption Of String ( UTF-8 ) / UE4 MD5 加密
193 0
|
算法 Java 网络安全
MD5只是用于加密吗?可听过文件MD5?
MD5只是用于加密吗?可听过文件MD5?
MD5只是用于加密吗?可听过文件MD5?
|
存储 编解码 算法
加解密技术基本使用指南(Base64、Hex、AES、SM4、RSA 算法)
加解密技术基本使用指南(Base64、Hex、AES、SM4、RSA 算法)
1886 0
加解密技术基本使用指南(Base64、Hex、AES、SM4、RSA 算法)
|
Java 数据安全/隐私保护
jmeter HMAC_SHA1加密并输出hex,base64的值
记录下jmeter HMAC_SHA1加密,并打包成jar文件
|
存储 算法 安全
MD5、SHA1、CRC32值是干什么的?
Hash,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值。 简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。HASH主要用于信息安全领域中加密算法,他把一些不同长度的信息转化成杂乱的128位的编码里,叫做HASH值。也可以说,hash就是找到一种数据内容和数据存放地址之间的映射关系。 MD5和SHA1可以说是目前应用最广泛的Hash算法,而它们都是以MD4为基础设计的。
290 0
|
Java MySQL 关系型数据库