C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#字符串加密解密

C#字符串使用密钥进行加解密

投稿:lijiao

这篇文章主要为大家详细介绍了C#字符串使用密钥进行加解密的代码,C#字符串加密和解密实现代码,感兴趣的小伙伴们可以参考一下

第一个为大家分享的是C#字符串使用密钥进行加解密代码,具体内容如下

public class DesEncrypt
  {
    /// <summary>
    /// 算法偏移量
    /// </summary>
    const string m_IV = "12345678";
 
    /// <summary>
    /// 功能描述:根据输入的密钥生成8位密钥
    /// 作  者: 爱给模板网 2gei.cn
    /// 创建日期:2015-07-20 17:25:26
    /// </summary>
    /// <param name="strkey">strkey</param>
    /// <returns>8位密钥</returns>
    private static string GetKey(string strkey)
    {
      if (string.IsNullOrEmpty(strkey))
      {
        strkey = "InfoColl";
      }
      if (strkey.Length % 8 == 0)
      {
        return strkey;
      }
      else
      {
        return GetKey(strkey + "0");
      }
    }
 
    /// <summary>
    /// 功能描述:加密字符串
    /// 作  者: 爱给模板网 2gei.cn
    /// 创建日期:2015-07-20 17:18:31
    /// 任务编号:
    /// </summary>
    /// <param name="strSourceString">原字符串</param>
    /// <param name="strKey">密钥</param>
    /// <returns>加密后的字符串</returns>
    public static string Encrypt(string strSourceString, string strKey)
    {
      strKey = GetKey(strKey);
      byte[] btKey = Encoding.UTF8.GetBytes(strKey);
 
      byte[] btIV = Encoding.UTF8.GetBytes(m_IV);
 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 
      using (MemoryStream ms = new MemoryStream())
      {
        try
        {
          byte[] inData = Encoding.UTF8.GetBytes(strSourceString);
          using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
          {
            cs.Write(inData, 0, inData.Length);
 
            cs.FlushFinalBlock();
          }
 
          return Convert.ToBase64String(ms.ToArray());
        }
        catch
        {
          return strSourceString;
        }
      }
    }
 
    /// <summary>
    /// 功能描述:解密字符串
    /// 作  者: 爱给模板网 2gei.cn
    /// 创建日期:2015-07-20 17:18:49
    /// 任务编号:
    /// </summary>
    /// <param name="strEncryptedString">原字符串</param>
    /// <param name="strKey">密钥</param>
    /// <returns>解密后的字符串</returns>
    public static string Decrypt(string strEncryptedString, string strKey)
    {
      strKey = GetKey(strKey);
      byte[] btKey = Encoding.UTF8.GetBytes(strKey);
 
      byte[] btIV = Encoding.UTF8.GetBytes(m_IV);
 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 
      using (MemoryStream ms = new MemoryStream())
      {
        try
        {
          byte[] inData = Convert.FromBase64String(strEncryptedString);
          using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
          {
            cs.Write(inData, 0, inData.Length);
 
            cs.FlushFinalBlock();
          }
 
          return Encoding.UTF8.GetString(ms.ToArray());
        }
        catch
        {
          return strEncryptedString;
        }
      }
    }
  }

C#字符串加密和解密

using System.Security.Cryptography;
using System.IO;
//默认密钥向量
    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

    /// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="encryptString">待加密的字符串</param>
    /// <param name="encryptKey">加密密钥,要求为8位</param>
    /// <returns>加密成功返回加密后的字符串,失败返回源串 </returns>
    public static string EncryptDES(string encryptString, string encryptKey)
    {
      try
      {
        byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));//转换为字节
        byte[] rgbIV = Keys;
        byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
        DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();//实例化数据加密标准
        MemoryStream mStream = new MemoryStream();//实例化内存流
        //将数据流链接到加密转换的流
        CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
        cStream.Write(inputByteArray, 0, inputByteArray.Length);
        cStream.FlushFinalBlock();
        return Convert.ToBase64String(mStream.ToArray());
      }
      catch
      {
        return encryptString;
      }
    }

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



      string EncryptStr = EncryptDESString.EncryptDES("aaaaaaaaaa", "ssssssss"); //返回加密后的字符串
      string DecryptStr = EncryptDESString.DecryptDES(EncryptStr, "ssssssss");//解密字符串

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文