对数据进行加密、解密

nlling 2005-06-15 11:49:54
请问,谁有用C#写的加密、解密函数,如果谁有,请尽快回复,小妹急用。谢了。
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
angeltimes 2005-06-16
  • 打赏
  • 举报
回复
包含两种加密,一个对文件加密,一个是对字符串加密
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
{

}


}
}
lovebanyi 2005-06-16
  • 打赏
  • 举报
回复
学习
孟子E章 2005-06-16
  • 打赏
  • 举报
回复
http://sz.luohuedu.net/xml/ShowDetail.asp?id=7AE7D20A-A5DA-4303-AC2D-32046BE4D086

加密,可解密
xjp0000 2005-06-16
  • 打赏
  • 举报
回复
/// 用户安全
/// </summary>
public class Security
{
/// <summary>
/// 字符串加密
/// </summary>
/// <param name="cleanString"></param>
/// <returns></returns>
public static string Encrypt(string cleanString)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString);
Byte[] hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

return BitConverter.ToString(hashedBytes);
}
}
}
fanruinet 2005-06-15
  • 打赏
  • 举报
回复
System.Security.Cryptography 命名空间都是加解密的类,几乎常见的加解密算法都有

参考
http://www.microsoft.com/china/msdn/Archives/msdnmag/issues/02/06/crypto/crypto.asp

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧