• 数据包加密解密


    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//密钥
    private static string key = "caijinhao";//密匙
    #region DES加密字符串
    ///<summary>
    ///DES加密字符串
    ///</summary>
    ///<param name="str">待加密的字符串</param>
    ///<param name="key">加密密钥,要求为8位</param>
    ///<returns>加密成功返回加密后的字符串,失败返回源字符串</returns>
    public static string EncryptDES(string str)
    {
    try
    {
    byte[] rgbKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));
    byte[] rgbIV = Keys;
    byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
    DESCryptoServiceProvider myDES = new DESCryptoServiceProvider();
    MemoryStream MStream = new MemoryStream();
    CryptoStream CStream = new CryptoStream(MStream, myDES.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
    CStream.Write(inputByteArray, 0, inputByteArray.Length);
    CStream.FlushFinalBlock();
    return Convert.ToBase64String(MStream.ToArray());
    }
    catch
    {
    return str;
    }
    }
    #endregion

    #region DES解密字符串

    ///<summary>
    ///DES解密字符串
    ///</summary>
    ///<param name="str">待解密的字符串</param>
    ///<param name="key">解密密钥,要求为8位,和加密密钥相同</param>
    ///<returns>解密成功返回解密后的字符串,失败返源字符串</returns>
    public static string DecryptDES(string str)
    {
    try
    {
    byte[] rgbKey = Encoding.UTF8.GetBytes(key);
    byte[] rgbIV = Keys;
    byte[] inputByteArray = Convert.FromBase64String(str);
    DESCryptoServiceProvider myDES = new DESCryptoServiceProvider();
    MemoryStream MStream = new MemoryStream();
    CryptoStream CStream = new CryptoStream(MStream, myDES.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
    CStream.Write(inputByteArray, 0, inputByteArray.Length);
    CStream.FlushFinalBlock();
    return Encoding.UTF8.GetString(MStream.ToArray());
    }
    catch
    {
    return str;
    }
    }
    #endregion

  • 相关阅读:
    leetcode1046
    leetcode1041
    leetcode1042
    leetcode399
    【经验分享】数据科学与机器学习面试指南
    计算机视觉计算任务有哪些,怎么分类 ?
    超详细!上线一个机器学习项目你需要哪些准备?
    人工智能和机器学习的前世今生
    机器学习从业人员到底做什么?
    两年AI研究经验(教训)总结,进来看看吧!
  • 原文地址:https://www.cnblogs.com/jinhaoObject/p/4581001.html
Copyright © 2020-2023  润新知