|
|
@@ -6,9 +6,7 @@ using System.Text;
|
|
|
using System.Windows.Forms;
|
|
|
using UAS_LabelMachine.CustomControl;
|
|
|
using UAS_LabelMachine.CustomControl.GroupBoxWithBorder;
|
|
|
-using System.Security.Cryptography;
|
|
|
using static System.Windows.Forms.Control;
|
|
|
-using System.IO;
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
namespace UAS_LabelMachine
|
|
|
@@ -18,8 +16,6 @@ namespace UAS_LabelMachine
|
|
|
|
|
|
static string SysDisc;
|
|
|
|
|
|
- private string Key = "96878265";
|
|
|
-
|
|
|
public static string SysDisc1
|
|
|
{
|
|
|
get
|
|
|
@@ -33,214 +29,6 @@ namespace UAS_LabelMachine
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static byte[] hexStr2ByteArr(string strIn)
|
|
|
- {
|
|
|
- byte[] arrB = Encoding.UTF8.GetBytes(strIn);
|
|
|
- int iLen = arrB.Length;
|
|
|
- // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
|
|
|
- byte[] arrOut = new byte[iLen / 2];
|
|
|
- for (int i = 0; i < iLen; i = i + 2)
|
|
|
- {
|
|
|
- string strTmp = new string(Encoding.UTF8.GetChars(arrB), i, 2);
|
|
|
- arrOut[i / 2] = (byte)int.Parse(strTmp, System.Globalization.NumberStyles.HexNumber);
|
|
|
- }
|
|
|
- return arrOut;
|
|
|
- }
|
|
|
-
|
|
|
- public byte[] decrypt(byte[] arrB)
|
|
|
- {
|
|
|
- return null;
|
|
|
- //return decryptCipher.doFinal(arrB);
|
|
|
- }
|
|
|
-
|
|
|
- public string decrypt(string strIn)
|
|
|
- {
|
|
|
- return new string(Encoding.ASCII.GetChars(decrypt(hexStr2ByteArr(strIn))));
|
|
|
- }
|
|
|
-
|
|
|
- public static string DESEnCode(string pToEncrypt, string sKey)
|
|
|
- {
|
|
|
- // string pToEncrypt1 = HttpContext.Current.Server.UrlEncode(pToEncrypt);
|
|
|
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
|
|
- byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
|
|
|
-
|
|
|
- //建立加密对象的密钥和偏移量
|
|
|
- //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
|
|
|
- //使得输入密码必须输入英文文本
|
|
|
- des.Key = Encoding.UTF8.GetBytes(sKey);
|
|
|
- des.IV = Encoding.UTF8.GetBytes(sKey);
|
|
|
- MemoryStream ms = new MemoryStream();
|
|
|
- CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
|
|
|
-
|
|
|
- cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
|
- cs.FlushFinalBlock();
|
|
|
-
|
|
|
- StringBuilder ret = new StringBuilder();
|
|
|
- foreach (byte b in ms.ToArray())
|
|
|
- {
|
|
|
- ret.AppendFormat("{0:X2}", b);
|
|
|
- }
|
|
|
- ret.ToString();
|
|
|
- return ret.ToString();
|
|
|
- }
|
|
|
-
|
|
|
- public static string DESDeCode(string pToDecrypt, string sKey)
|
|
|
- {
|
|
|
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
|
|
-
|
|
|
- byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
|
|
|
- for (int x = 0; x < pToDecrypt.Length / 2; x++)
|
|
|
- {
|
|
|
- int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
|
|
|
- inputByteArray[x] = (byte)i;
|
|
|
- }
|
|
|
- des.Key = Encoding.UTF8.GetBytes(sKey);
|
|
|
- des.IV = Encoding.UTF8.GetBytes(sKey);
|
|
|
- MemoryStream ms = new MemoryStream();
|
|
|
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
|
|
|
- cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
|
- cs.FlushFinalBlock();
|
|
|
-
|
|
|
- return Encoding.UTF8.GetString(ms.ToArray());
|
|
|
- }
|
|
|
-
|
|
|
- public static byte[] Des3EncodeCBC(byte[] key, byte[] iv, byte[] data)
|
|
|
- {
|
|
|
- //复制于MSDN
|
|
|
- try
|
|
|
- {
|
|
|
- // Create a MemoryStream.
|
|
|
- MemoryStream mStream = new MemoryStream();
|
|
|
- TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
|
|
|
- tdsp.Mode = CipherMode.CBC; //默认值
|
|
|
- tdsp.Padding = PaddingMode.PKCS7; //默认值
|
|
|
- // Create a CryptoStream using the MemoryStream
|
|
|
- // and the passed key and initialization vector (IV).
|
|
|
- CryptoStream cStream = new CryptoStream(mStream,
|
|
|
- tdsp.CreateEncryptor(key, iv),
|
|
|
- CryptoStreamMode.Write);
|
|
|
- // Write the byte array to the crypto stream and flush it.
|
|
|
- cStream.Write(data, 0, data.Length);
|
|
|
- cStream.FlushFinalBlock();
|
|
|
- // Get an array of bytes from the
|
|
|
- // MemoryStream that holds the
|
|
|
- // encrypted data.
|
|
|
- byte[] ret = mStream.ToArray();
|
|
|
- // Close the streams.
|
|
|
- cStream.Close();
|
|
|
- mStream.Close();
|
|
|
- // Return the encrypted buffer.
|
|
|
- return ret;
|
|
|
- }
|
|
|
- catch (CryptographicException e)
|
|
|
- {
|
|
|
- Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// DES3 CBC模式解密
|
|
|
- /// </summary>
|
|
|
- /// <param name="key">密钥</param>
|
|
|
- /// <param name="iv">IV</param>
|
|
|
- /// <param name="data">密文的byte数组</param>
|
|
|
- /// <returns>明文的byte数组</returns>
|
|
|
- public static byte[] Des3DecodeCBC(byte[] key, byte[] iv, byte[] data)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- // Create a new MemoryStream using the passed
|
|
|
- // array of encrypted data.
|
|
|
- MemoryStream msDecrypt = new MemoryStream(data);
|
|
|
- TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
|
|
|
- tdsp.Mode = CipherMode.CBC;
|
|
|
- tdsp.Padding = PaddingMode.PKCS7;
|
|
|
- // Create a CryptoStream using the MemoryStream
|
|
|
- // and the passed key and initialization vector (IV).
|
|
|
- CryptoStream csDecrypt = new CryptoStream(msDecrypt, tdsp.CreateDecryptor(key, iv), CryptoStreamMode.Read);
|
|
|
- // Create buffer to hold the decrypted data.
|
|
|
- byte[] fromEncrypt = new byte[data.Length];
|
|
|
- // Read the decrypted data out of the crypto stream
|
|
|
- // and place it into the temporary buffer.
|
|
|
- csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
|
|
|
- //Convert the buffer into a string and return it.
|
|
|
- return fromEncrypt;
|
|
|
- }
|
|
|
- catch (CryptographicException e)
|
|
|
- {
|
|
|
- Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// DES3 ECB模式加密
|
|
|
- /// </summary>
|
|
|
- /// <param name="key">密钥</param>
|
|
|
- /// <param name="iv">IV(当模式为ECB时,IV无用)</param>
|
|
|
- /// <param name="str">明文的byte数组</param>
|
|
|
- /// <returns>密文的byte数组</returns>
|
|
|
- public static byte[] Des3EncodeECB(byte[] key, byte[] iv, byte[] data)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- // Create a MemoryStream.
|
|
|
- MemoryStream mStream = new MemoryStream();
|
|
|
- TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
|
|
|
- tdsp.Mode = CipherMode.ECB;
|
|
|
- tdsp.Padding = PaddingMode.PKCS7;
|
|
|
- // Create a CryptoStream using the MemoryStream
|
|
|
- // and the passed key and initialization vector (IV).
|
|
|
- CryptoStream cStream = new CryptoStream(mStream, tdsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
|
|
|
- // Write the byte array to the crypto stream and flush it.
|
|
|
- cStream.Write(data, 0, data.Length);
|
|
|
- cStream.FlushFinalBlock();
|
|
|
- // Get an array of bytes from the
|
|
|
- // MemoryStream that holds the
|
|
|
- // encrypted data.
|
|
|
- byte[] ret = mStream.ToArray();
|
|
|
- // Close the streams.
|
|
|
- cStream.Close();
|
|
|
- mStream.Close();
|
|
|
- // Return the encrypted buffer.
|
|
|
- return ret;
|
|
|
- }
|
|
|
- catch (CryptographicException e)
|
|
|
- {
|
|
|
- Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// DES3 ECB模式解密
|
|
|
- /// </summary>
|
|
|
- /// <param name="key">密钥</param>
|
|
|
- /// <param name="iv">IV(当模式为ECB时,IV无用)</param>
|
|
|
- /// <param name="str">密文的byte数组</param>
|
|
|
- /// <returns>明文的byte数组</returns>
|
|
|
- public static byte[] Des3DecodeECB(byte[] key, byte[] iv, byte[] data)
|
|
|
- {
|
|
|
-
|
|
|
- // Create a new MemoryStream using the passed
|
|
|
- // array of encrypted data.
|
|
|
- MemoryStream msDecrypt = new MemoryStream(data);
|
|
|
- TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
|
|
|
- tdsp.Mode = CipherMode.ECB;
|
|
|
- tdsp.Padding = PaddingMode.PKCS7;
|
|
|
- // Create a CryptoStream using the MemoryStream
|
|
|
- // and the passed key and initialization vector (IV).
|
|
|
- CryptoStream csDecrypt = new CryptoStream(msDecrypt, tdsp.CreateDecryptor(key, iv), CryptoStreamMode.Read);
|
|
|
- // Create buffer to hold the decrypted data.
|
|
|
- byte[] fromEncrypt = new byte[data.Length];
|
|
|
- // Read the decrypted data out of the crypto stream
|
|
|
- // and place it into the temporary buffer.
|
|
|
- csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
|
|
|
- //Convert the buffer into a string and return it.
|
|
|
- return fromEncrypt;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 通过DataTable的ColumnName和Caption来拼接一条语句
|
|
|
/// </summary>
|