欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-08-26 23:41:28
第一个为大家分享的是c#字符串使用密钥进行加解密代码,具体内容如下 public class desencrypt { ///

第一个为大家分享的是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");//解密字符串

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