首页上一页 1 下一页尾页 1 条记录 1/1页
我创建了一个类,里面创建了一个方法,达到调用方法时传入一串字符,能返回一个一串加密后的字符
发表在C#图书答疑
2010-07-01
是否精华
是
否
版块置顶:
是
否
[font color=#FF0000]//我创建了一个类,里面创建了一个方法,达到调用方法时传入一串字符,能返回一个一串加密后的字符,但怎么试都有错,请帮我看下是哪里的问题呀,谢谢。[/font]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography; //加密解密用
using System.IO; //MemoryStream文件流用
namespace 从ini文件中读取数据库连接
{
class DBConfig
{
public string Encryp (string what)
{
string encryptKey = "secr";//加密key
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Encoding.Unicode.GetBytes(what.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
return Convert.ToBase64String(MStream.ToArray());
}
catch
{
MessageBox.Show("加密失败!");
}
}
public string test() //有返回值的方法
{
string t = "您好!"; //设定变量
return t; //返回t变量
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography; //加密解密用
using System.IO; //MemoryStream文件流用
namespace 从ini文件中读取数据库连接
{
class DBConfig
{
public string Encryp (string what)
{
string encryptKey = "secr";//加密key
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Encoding.Unicode.GetBytes(what.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
return Convert.ToBase64String(MStream.ToArray());
}
catch
{
MessageBox.Show("加密失败!");
}
}
public string test() //有返回值的方法
{
string t = "您好!"; //设定变量
return t; //返回t变量
}
}
}