• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
.NET新手入门
Keep MOVEing.......永不止步!!!
博客园    首页    新随笔    联系   管理    订阅  订阅

DES加密算法,仅供自己做笔记用

// 加密函数
    //strText要加密的字符串,strencrKey 密钥, 返回加密后的字符串 密码8位
    public static string Encrypt(string strText, string strEncrKey)
    {
        byte[] byKey = null;//初始化密钥
        byte[] IV = { 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x11, 0x12 };
        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey);//获取密钥
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            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();
            return Convert.ToBase64String(ms.ToArray());
        }
        catch (System.Exception error)
        {
            return "error:" + error.Message + "\r";
        }
    }
posted @ 2009-05-07 10:23  .NET新手入门  阅读(382)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3