欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

加密解密

程序员文章站 2023-11-22 08:31:34
//加密 public static string GDEncode(string data, string Key) { Key = "12345678"; byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(Key); byte[] b ......

 

//加密

public static string gdencode(string data, string key) 
{
key = "12345678";
byte[] bykey = system.text.asciiencoding.ascii.getbytes(key);
byte[] byiv = system.text.asciiencoding.ascii.getbytes(key);
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 gjdecode(string data, string key) 
{
key = "12345678";
byte[] bykey = system.text.asciiencoding.ascii.getbytes(key);
byte[] byiv = system.text.asciiencoding.ascii.getbytes(key);
byte[] byenc;
try
{
byenc = convert.frombase64string(data);
}
catch
{
return data;
}
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);
string str;
str = sr.readtoend();
if (str.trim() == "") str = data;
return str;
}