首页 > 开发 > 综合 > 正文

用C#实现Des加密和解密

2024-07-21 02:17:59
字体:
来源:转载
供稿:网友
using system;
using system.io;
using system.security.cryptography;

namespace vavic
{
/// <summary>
/// security 的摘要说明。
/// </summary>
public class security
{
const string key_64 = "vavicapp";
const string iv_64 = "vavicapp"; //注意了,是8个字符,64位

public security()
{
//
// todo: 在此处添加构造函数逻辑
//
}

public static string encode(string data)
{
byte[] bykey = system.text.asciiencoding.ascii.getbytes(key_64);
byte[] byiv = system.text.asciiencoding.ascii.getbytes(iv_64);

descryptoserviceprovider cryptoprovider = new descryptoserviceprovider();
int i = cryptoprovider.keysize;
memorystream ms = new memorystream();
cryptostream cst = new cryptostream(ms,cryptoprovider.createencryptor(bykey,byiv),cryptostreammode.write);

streamwriter sw = new streamwriter(cst);
sw.write(data);
sw.flush();
cst.flushfinalblock();
sw.flush();
return convert.tobase64string(ms.getbuffer(),0,(int)ms.length);

}

public static string decode(string data)
{
byte[] bykey = system.text.asciiencoding.ascii.getbytes(key_64);
byte[] byiv = system.text.asciiencoding.ascii.getbytes(iv_64);

byte[] byenc;
try
{
byenc = convert.frombase64string(data);
}
catch
{
return null;
}

descryptoserviceprovider cryptoprovider = new descryptoserviceprovider();
memorystream ms = new memorystream(byenc);
cryptostream cst = new cryptostream(ms,cryptoprovider.createdecryptor(bykey,byiv),cryptostreammode.read);
streamreader sr = new streamreader(cst);
return sr.readtoend();
}
}
}


最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表