123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- namespace Ant.Service.Common
- {
-
-
-
- public class DESProvider
- {
- private DESProvider()
- {
- }
-
- private static string key = "netskycn";
-
-
-
- public static string Key
- {
- get
- {
- return key;
- }
- set
- {
- key = value;
- }
- }
- #region 加密
-
-
-
-
-
-
- public static string EncryptString(string encryptString, string key)
- {
-
- if (string.IsNullOrEmpty(encryptString))
- {
- throw new ArgumentNullException("encryptString", "不能为空");
- }
-
- if (string.IsNullOrEmpty(key))
- {
- throw new ArgumentNullException("key", "不能为空");
- }
-
- byte[] keyBytes = Encoding.UTF8.GetBytes(key);
-
- byte[] keyIV = keyBytes;
-
- byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
-
- byte[] resultByteArray = EncryptBytes(inputByteArray, keyBytes, keyIV);
-
- return Convert.ToBase64String(resultByteArray);
- }
-
-
-
-
-
- public static string EncryptString(string encryptString)
- {
- return EncryptString(encryptString, key);
- }
-
-
-
-
-
-
-
- public static byte[] EncryptBytes(byte[] sourceBytes, byte[] keyBytes, byte[] keyIV)
- {
- if (sourceBytes == null || keyBytes == null || keyIV == null)
- {
- throw new ArgumentNullException("sourceBytes和keyBytes", "不能为空。");
- }
- else
- {
-
- keyBytes = CheckByteArrayLength(keyBytes);
-
- keyIV = CheckByteArrayLength(keyIV);
- DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
-
- MemoryStream mStream = new MemoryStream();
-
- CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
- cStream.Write(sourceBytes, 0, sourceBytes.Length);
- cStream.FlushFinalBlock();
-
- byte[] buffer = mStream.ToArray();
- mStream.Close();
- cStream.Close();
- return buffer;
- }
- }
- #endregion
- #region 解密
- public static string DecryptString(string decryptString, string key)
- {
- if (string.IsNullOrEmpty(decryptString))
- {
- throw new ArgumentNullException("decryptString", "不能为空");
- }
- if (string.IsNullOrEmpty(key))
- {
- throw new ArgumentNullException("key", "不能为空");
- }
- byte[] keyBytes = Encoding.UTF8.GetBytes(key);
- byte[] keyIV = keyBytes;
-
- byte[] inputByteArray = Convert.FromBase64String(decryptString);
-
- byte[] resultByteArray = DecryptBytes(inputByteArray, keyBytes, keyIV);
-
- return Encoding.UTF8.GetString(resultByteArray);
- }
-
-
-
-
-
- public static string DecryptString(string decryptString)
- {
- return DecryptString(decryptString, key);
- }
-
-
-
-
-
-
-
- public static byte[] DecryptBytes(byte[] sourceBytes, byte[] keyBytes, byte[] keyIV)
- {
- if (sourceBytes == null || keyBytes == null || keyIV == null)
- {
- throw new ArgumentNullException("soureBytes和keyBytes及keyIV", "不能为空。");
- }
- else
- {
-
- keyBytes = CheckByteArrayLength(keyBytes);
-
- keyIV = CheckByteArrayLength(keyIV);
- DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
- MemoryStream mStream = new MemoryStream();
- CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
- cStream.Write(sourceBytes, 0, sourceBytes.Length);
- cStream.FlushFinalBlock();
-
- byte[] buffer = mStream.ToArray();
- mStream.Close();
- cStream.Close();
- return buffer;
- }
- }
- #endregion
-
-
-
-
-
- private static byte[] CheckByteArrayLength(byte[] byteArray)
- {
- byte[] resultBytes = new byte[8];
-
- if (byteArray.Length < 8)
- {
- return Encoding.UTF8.GetBytes("12345678");
- }
-
- else if (byteArray.Length % 8 != 0 || byteArray.Length > 64)
- {
- Array.Copy(byteArray, 0, resultBytes, 0, 8);
- return resultBytes;
- }
- else
- {
- return byteArray;
- }
- }
- }
- }
|