欢迎来到 IT实训基地-南通科迅教育
咨询电话:0513-81107100
C# 使用DES加密,解密字符串
2017/3/6
南通科迅教育
328
南通Java培训靠谱么?从来没有一篇文章讲得如此详细

1 DES类

?
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
 
namespace NoticeBoard
{
    /// <summary>
    /// DES加密解密算法
    /// </summary>
    publicclassDES
    {
        /// <summary>
        /// DES加密算法
        /// sKey为8位或16位
        /// </summary>
        /// 需要加密的字符串
        /// 密钥
        /// <returns></returns>
        publicstring DesEncrypt(string pToEncrypt, string sKey)
        {
            StringBuilder ret =newStringBuilder();
 
            try
            {
                DESCryptoServiceProvider des =newDESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms =newMemoryStream();
                CryptoStream cs =newCryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray,0, inputByteArray.Length);
                cs.FlushFinalBlock();
                 
                foreach (byteb in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
            }
            catch
            {
 
            }
            returnret.ToString();
            //return a;
        }
        /// <summary>
        /// DES解密算法
        /// sKey为8位或16位
        /// </summary>
        /// 需要解密的字符串
        /// 密钥
        /// <returns></returns>
        publicstring DesDecrypt(string pToDecrypt, string sKey)
        { 
            MemoryStream ms =newMemoryStream();
 
            try
            {
                DESCryptoServiceProvider des =newDESCryptoServiceProvider();
                byte[] inputByteArray =newbyte[pToDecrypt.Length /2];
                for(intx =0; x < pToDecrypt.Length /2; x++)
                {
                    inti = (Convert.ToInt32(pToDecrypt.Substring(x *2,2),16));
                    inputByteArray[x] = (byte)i;
                }
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
               
                CryptoStream cs =newCryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray,0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret =newStringBuilder();
                
            }
            catch
            {
 
            }
             
            returnSystem.Text.Encoding.Default.GetString(ms.ToArray());
        }
    }
 
    /// <summary>  
    /// 对称加密解密算法类  
    /// </summary>  
    publicclassRijndael
    {
        privateSymmetricAlgorithm mobjCryptoService;
        privatestring Key;
        /// <summary>  
        /// 对称加密类的构造函数  
        /// </summary>  
        publicvoidSymmetricMethod()
        {
            mobjCryptoService =newRijndaelManaged();
            Key ="Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
        }
        /// <summary>  
        /// 获得密钥  
        /// </summary>  
        /// <returns>密钥</returns>  
        privatebyte[] GetLegalKey()
        {
            string sTemp = Key;
            mobjCryptoService.GenerateKey();
            byte[] bytTemp = mobjCryptoService.Key;
            intKeyLength = bytTemp.Length;
            if(sTemp.Length > KeyLength)
                sTemp = sTemp.Substring(0, KeyLength);
            elseif(sTemp.Length < KeyLength)
                sTemp = sTemp.PadRight(KeyLength,' ');
            returnASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /// <summary>  
        /// 获得初始向量IV  
        /// </summary>  
        /// <returns>初试向量IV</returns>  
        privatebyte[] GetLegalIV()
        {
            string sTemp ="aclejaspwejgjdjfasrweojksdf$%#@!^!@&#*ajke^!@#jka";     //key 随便写。。
            mobjCryptoService.GenerateIV();
            byte[] bytTemp = mobjCryptoService.IV;
            intIVLength = bytTemp.Length;
            if(sTemp.Length > IVLength)
                sTemp = sTemp.Substring(0, IVLength);
            elseif(sTemp.Length < IVLength)
                sTemp = sTemp.PadRight(IVLength,' ');
            returnASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /// <summary>  
        /// 加密方法  
        /// </summary>  
        /// 待加密的串  
        /// <returns>经过加密的串</returns>  
        publicstring Encrypto(string Source)
        {
            byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
            MemoryStream ms =newMemoryStream();
            mobjCryptoService.Key = GetLegalKey();
            mobjCryptoService.IV = GetLegalIV();
            ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
            CryptoStream cs =newCryptoStream(ms, encrypto, CryptoStreamMode.Write);
            cs.Write(bytIn,0, bytIn.Length);
            cs.FlushFinalBlock();
            ms.Close();
            byte[] bytOut = ms.ToArray();
            returnConvert.ToBase64String(bytOut);
        }
        /// <summary>  
        /// 解密方法  
        /// </summary>  
        /// 待解密的串  
        /// <returns>经过解密的串</returns>  
        publicstring Decrypto(string Source)
        {
            byte[] bytIn = Convert.FromBase64String(Source);
            MemoryStream ms =newMemoryStream(bytIn,0, bytIn.Length);
            mobjCryptoService.Key = GetLegalKey();
            mobjCryptoService.IV = GetLegalIV();
            ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
            CryptoStream cs =newCryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader sr =newStreamReader(cs);
            returnsr.ReadToEnd();
        }
    }
 
 
}

2 DES加解密方法静态封装

 

 

?
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml;
 
 
namespace NoticeBoard
{
    publicclassStaticClass
    {
 
        #region 加密解密
 
        publicstaticDES dd =newDES();
 
        publicstaticstring StringKey ="A123456.";// 加密密钥
 
        /// <summary>
        /// 解密
        /// </summary>
        ///<param name="str">
        /// <returns></returns>
        publicstaticstring StringDecder(string str)
        {
            returndd.DesDecrypt(str, StringKey);
        }
 
        /// <summary>
        /// 加密
        /// </summary>
        ///<param name="str">
        /// <returns></returns>
        publicstaticstring StringEncoder(string str)
        {
            returndd.DesEncrypt(str, StringKey);
        }
 
        #endregion
 
    }
}

3 方法调用:

 

 

直接调用 StringDecder 和 StringEncoder 方法就行了
77
关闭
先学习,后交费申请表
每期5位名额
在线咨询
免费电话
QQ联系
先学习,后交费
TOP
您好,您想咨询哪门课程呢?
关于我们
机构简介
官方资讯
地理位置
联系我们
0513-91107100
周一至周六     8:30-21:00
微信扫我送教程
手机端访问
南通科迅教育信息咨询有限公司     苏ICP备15009282号     联系地址:江苏省南通市人民中路23-6号新亚大厦三楼             法律顾问:江苏瑞慈律师事务所     Copyright 2008-