包含两种加密,一个对文件加密,一个是对字符串加密
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace CommonFunction
{
/// <summary>
///数据加密、解密,文件的加密解密
/// </summary>
public class Secret
{
public Secret()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string DesEncrypt(string strText, string strEncrKey)
{
//密钥
byte[] byKey=null;
//初始化向量,即私钥
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
//将密钥转换成为UTF8编码的字符数组
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,strEncrKey.Length));
//建立对象
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//将要加密的数据转换成为UTF8格式的数组
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
//创建存储区为内存的流
MemoryStream ms = new MemoryStream();
//以写模式把数据流和加密的数据流建立连接`
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
//把数据加密写到转换流中
cs.Write(inputByteArray, 0, inputByteArray.Length);
//把加密过的数据写到存储区为内存的数据流中,并清空缓冲区
cs.FlushFinalBlock();
//将8位无符号整数数组子集的值转换为等效的System.String表现形式
return Convert.ToBase64String(ms.ToArray());
}
catch(System.Exception error)
{
return "error:" +error.Message+"/r";
}
}
public string DesDecrypt(string strText,string sDecrKey)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)
{
return "error:"+error.Message+"/r";
}
}
public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey)
{
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
//设置写文件流的长度为0
fout.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
//取得指定文件的长度
long totlen = fin.Length;
int len;
DES des = new DESCryptoServiceProvider();
//以定模式把文件的数据流和加密的文件流建立连接
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
catch
{
}
}
public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len;
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
catch
{
}
}
}
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace CommonFunction
{
/// <summary>
///数据加密、解密,文件的加密解密
/// </summary>
public class Secret
{
public Secret()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string DesEncrypt(string strText, string strEncrKey)
{
//密钥
byte[] byKey=null;
//初始化向量,即私钥
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
//将密钥转换成为UTF8编码的字符数组
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,strEncrKey.Length));
//建立对象
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//将要加密的数据转换成为UTF8格式的数组
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
//创建存储区为内存的流
MemoryStream ms = new MemoryStream();
//以写模式把数据流和加密的数据流建立连接`
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
//把数据加密写到转换流中
cs.Write(inputByteArray, 0, inputByteArray.Length);
//把加密过的数据写到存储区为内存的数据流中,并清空缓冲区
cs.FlushFinalBlock();
//将8位无符号整数数组子集的值转换为等效的System.String表现形式
return Convert.ToBase64String(ms.ToArray());
}
catch(System.Exception error)
{
return "error:" +error.Message+"/r";
}
}
public string DesDecrypt(string strText,string sDecrKey)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)
{
return "error:"+error.Message+"/r";
}
}
public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey)
{
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
//设置写文件流的长度为0
fout.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
//取得指定文件的长度
long totlen = fin.Length;
int len;
DES des = new DESCryptoServiceProvider();
//以定模式把文件的数据流和加密的文件流建立连接
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
catch
{
}
}
public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len;
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
catch
{
}
}
}