zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using HH.WMS.Utils;
namespace HH.WMS.Utils
{
    public partial class ZEncypt
    {
        /// <summary>
        /// 32λKeyÖµ£º
        /// </summary>
        private static byte[] DESKey = new byte[] { 0x03, 0x0B, 0x13, 0x1B, 0x23, 0x2B, 0x33, 0x3B, 0x43, 0x4B, 0x9B, 0x93, 0x8B, 0x83, 0x7B, 0x73, 0x6B, 0x63, 0x5B, 0x53, 0xF3, 0xFB, 0xA3, 0xAB, 0xB3, 0xBB, 0xC3, 0xEB, 0xE3, 0xDB, 0xD3, 0xCB };
 
        #region DES¼ÓÃÜ
        /// <summary>
        /// DES¼ÓÃÜ
        /// </summary>
        /// <param name="strSource">´ý¼ÓÃÜ×Ö´®</param>
        /// <returns>¼ÓÃܺóµÄ×Ö·û´®</returns>
        public static string DESEncrypt(string strSource)
        {
            return DESEncrypt(strSource, DESKey);
        }
        /// <summary>
        /// DES¼ÓÃÜ
        /// </summary>
        /// <param name="strSource">´ý¼ÓÃÜ×Ö´®</param>
        /// <param name="key">KeyÖµ</param>
        /// <returns>¼ÓÃܺóµÄ×Ö·û´®</returns>
        public static string DESEncrypt(string strSource, byte[] key)
        {
            SymmetricAlgorithm sa = Rijndael.Create();
            sa.Key = key;
            sa.Mode = CipherMode.ECB;
            sa.Padding = PaddingMode.Zeros;
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
            byte[] byt = Encoding.Unicode.GetBytes(strSource);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Convert.ToBase64String(ms.ToArray());
        }
        #endregion
 
        #region DES½âÃÜ
        /// <summary>
        /// DES½âÃÜ
        /// </summary>
        /// <param name="strSource">´ý½âÃܵÄ×Ö´®</param>
        /// <returns>½âÃܺóµÄ×Ö·û´®</returns>
        public static string DESDecrypt(string strSource)
        {
            return DESDecrypt(strSource, DESKey);
        }
        /// <summary>
        /// DES½âÃÜ
        /// </summary>
        /// <param name="strSource">´ý½âÃܵÄ×Ö´®</param>
        /// <param name="key">32λKeyÖµ</param>
        /// <returns>½âÃܺóµÄ×Ö·û´®</returns>
        public static string DESDecrypt(string strSource, byte[] key)
        {
            SymmetricAlgorithm sa = Rijndael.Create();
            sa.Key = key;
            sa.Mode = CipherMode.ECB;
            sa.Padding = PaddingMode.Zeros;
            ICryptoTransform ct = sa.CreateDecryptor();
            byte[] byt = Convert.FromBase64String(strSource);
            MemoryStream ms = new MemoryStream(byt);
            CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs, Encoding.Unicode);
            return sr.ReadToEnd().Trim('\0');
        }
        #endregion
 
    }
}