服务器之家

服务器之家 > 正文

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

时间:2021-12-03 15:22     来源/作者:C#教程网

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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#字符串加密和解密

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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");//解密字符串

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

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部