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

本文介绍了一个使用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";
        }
    }

转载于:https://www.cnblogs.com/zhangyu1986love/archive/2009/05/07/1451351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值