简单.net字符串加密解密函数


    private static string Key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

    //加密函数
    /// <summary>
    /// 使用DES加密指定字符串
    /// </summary>
    /// <param name="encryptStr">待加密的字符串 </param>
    /// <param name="key">密钥(最大长度8) </param>
    /// <param name="IV">初始化向量(最大长度8) </param>
    /// <returns>加密后的字符串 </returns>
    public static string DESEncrypt(string encryptStr, string key, string IV)
    {
        //将key和IV处理成8个字符
        key += "12345678";
        IV += "12345678";
        key = key.Substring(0, 8);
        IV = IV.Substring(0, 8);

        SymmetricAlgorithm sa;
        ICryptoTransform ict;
        MemoryStream ms;
        CryptoStream cs;
        byte[] byt;

        sa = new DESCryptoServiceProvider();
        sa.Key = Encoding.UTF8.GetBytes(key);
        sa.IV = Encoding.UTF8.GetBytes(IV);
        ict = sa.CreateEncryptor();

        byt = Encoding.UTF8.GetBytes(encryptStr);

        ms = new MemoryStream();
        cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
        cs.Write(byt, 0, byt.Length);
        cs.FlushFinalBlock();

        cs.Close();

        //加上一些干扰字符
        string retVal = Convert.ToBase64String(ms.ToArray());
        System.Random ra = new Random();

        for (int i = 0; i < 8; i++)
        {
            int radNum = ra.Next(36);
            char radChr = Convert.ToChar(radNum + 65);//生成一个随机字符

            retVal = retVal.Substring(0, 2 * i + 1) + radChr.ToString() + retVal.Substring(2 * i + 1);
        }
        return retVal.Replace("+", "%2B");
    }


    //解密函数:
    /// <summary>
    /// 使用DES解密指定字符串
    /// </summary>
    /// <param name="encryptedValue">待解密的字符串 </param>
    /// <param name="key">密钥(最大长度8) </param>
    /// <param name="IV">初始化向量(最大长度8) </param>
    /// <returns>解密后的字符串 </returns>
    public static string DESDecrypt(string encryptedValue, string key, string IV)
    {
        //去掉干扰字符
        string tmp = encryptedValue.Replace("%2B", "+");
        if (tmp.Length < 16)
        {
            return "";
        }

        for (int i = 0; i < 8; i++)
        {
            tmp = tmp.Substring(0, i + 1) + tmp.Substring(i + 2);
        }
        encryptedValue = tmp;

        //将key和IV处理成8个字符
        key += "12345678";
        IV += "12345678";
        key = key.Substring(0, 8);
        IV = IV.Substring(0, 8);

        SymmetricAlgorithm sa;
        ICryptoTransform ict;
        MemoryStream ms;
        CryptoStream cs;
        byte[] byt;

        try
        {
            sa = new DESCryptoServiceProvider();
            sa.Key = Encoding.UTF8.GetBytes(key);
            sa.IV = Encoding.UTF8.GetBytes(IV);
            ict = sa.CreateDecryptor();

            byt = Convert.FromBase64String(encryptedValue);

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();

            cs.Close();

            return Encoding.UTF8.GetString(ms.ToArray());
        }
        catch (System.Exception e)
        {

            return null;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cactusjoy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值