///----------------------------------------------------------------------- /// /// 程式代號: EncryptMgr /// 程式名稱: 加解密總管 /// 程式說明: 提供加密解密 /// 起始作者: Hercules /// 起始日期: 2016/06/17 /// 最新修改人: Hercules /// 最新修日期: 2016/06/17 /// ///----------------------------------------------------------------------- #region 程式異動記錄 /// xx.YYYY/MM/DD VER AUTHOR COMMENTS(說明修改的內容) /// 01.2016/06/17 1.0 Hercules CREATE /// 最新修改人: Hercules /// 最新修日期: 2016/06/17 #endregion using OT.COM.Encryption; using OT.COM.LogisticsUtil; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; namespace SoldierData { /// /// 類別名稱:EncryptMgr /// 類別說明:加解密總管 /// 起始作者:Hercules /// 起始日期:2016/06/17 /// 最新修改人: Hercules /// 最新修日期: 2016/06/17 /// public class SecurityUtil { private static Util _ut = new Util(); private static byte[] _Key = System.Text.Encoding.Unicode.GetBytes(_ut.GetSettingString("KEY")); private static byte[] _IV = System.Text.Encoding.Unicode.GetBytes(_ut.GetSettingString("IV")); private static AES _aes = new AES(); /// /// 函式名稱:Encrypt /// 函式說明:加密API /// 起始作者:Hercules /// 起始日期:2016/06/17 /// 最新修改人: Hercules /// 最新修日期: 2016/06/17 /// /// 明碼 /// 暗碼 /// 錯誤訊息,若成功則為Null public static string Encrypt(string i_sPlaintext, out string o_sCiphertext) { string sMsg = null; string sRes = null; try { do { byte[] baRes = _aes.EncryptStringToByte( i_sPlaintext, _Key, _IV); sRes = Convert.ToBase64String(baRes); } while (false); } catch (Exception ex) { sMsg = _ut.GetLastExceptionMsg(ex); } finally { o_sCiphertext = sRes; } return sMsg; } /// /// 函式名稱:Decrypt /// 函式說明:解密API /// 起始作者:Hercules /// 起始日期:2016/06/17 /// 最新修改人: Hercules /// 最新修日期: 2016/06/17 /// /// 暗碼 /// 明碼 /// 錯誤訊息,若成功則為Null public static string Decrypt(string i_sCiphertext, out string o_sPlaintext) { string sMsg = null; string sRes = null; try { do { byte[] baCiphertext = Convert.FromBase64String(i_sCiphertext); sRes = _aes.DecryptByteToString( baCiphertext, _Key, _IV); } while (false); } catch (Exception ex) { sMsg = _ut.GetLastExceptionMsg(ex); } finally { o_sPlaintext = sRes; } return sMsg; } //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /**/ /**/ /**/ /// /// DES加密字符串 /// /// 待加密的字符串 /// 加密密钥,要求为8位 /// 加密成功返回加密后的字符串,失败返回源串 public static string EncryptDES(string encryptString, out string o_sCiphertext) { string sMsg = null; try { byte[] rgbKey = Encoding.UTF8.GetBytes("chipmos_".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(); o_sCiphertext = Convert.ToBase64String(mStream.ToArray()); } catch (Exception ex) { o_sCiphertext = encryptString; sMsg = _ut.GetLastExceptionMsg(ex); } return sMsg; } /**/ /**/ /**/ /// /// DES解密字符串 /// /// 待解密的字符串 /// 解密密钥,要求为8位,和加密密钥相同 /// 解密成功返回解密后的字符串,失败返源串 public static string DecryptDES(string decryptString, out string o_sPlaintext) { string sMsg = null; try { byte[] rgbKey = Encoding.UTF8.GetBytes("chipmos_".Substring(0, 8)); 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(); o_sPlaintext = Encoding.UTF8.GetString(mStream.ToArray()); } catch (Exception ex) { o_sPlaintext = decryptString; sMsg = _ut.GetLastExceptionMsg(ex); } return sMsg; } private static char[] constant = { '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' }; public static string GetRandom(int Length) { System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62); Random rd = new Random(); for (int i = 0; i < Length; i++) { newRandom.Append(constant[rd.Next(62)]); } return newRandom.ToString(); } } }