已有56人关注
我新建了一个类文件DBConfig 但在其他窗体代码中用DBconfig.Encryp( 加密内容 ) 调用不到,是什么原因呀?
发表在C#图书答疑 2010-07-03
是否精华
版块置顶:
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("加密失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return string.Empty;

            }
        }
        public string Descryp(string what)            //有返回值的方法
        {
            string encryptKey = "secr";//解密key

            try
            {
                DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
                byte[] key = Encoding.Unicode.GetBytes(encryptKey);
                byte[] data = Convert.FromBase64String(what.Trim());
                MemoryStream MStream = new MemoryStream();
                CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
                CStream.Write(data, 0, data.Length);
                CStream.FlushFinalBlock();
                return Encoding.Unicode.GetString(MStream.ToArray());

            }
            catch 
            {
                MessageBox.Show("解密失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return string.Empty;
            }
           
        }
         
    }
    
}

[font color=#FF0000]
[strong]//在其他窗体代码中用DBconfig.Encryp("加密内容") 调用不到,是什么原因呀?
[/strong][/font]
分享到:
精彩评论 2
wufayou
学分:0 LV1
TA的每日心情
第五天
2023-03-18 21:24:17
2010-07-03
沙发

我刚才自己想出来了,搞懂了,不用解答了,谢谢。
小科_mrkj
学分:43 LV2
2010-07-05
板凳
读者朋友:
    您好,希望你继续关注我们的图书和C#编程词典产品。
首页上一页 1 下一页尾页 2 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照