public static byte[] EncryptDecryptBytes(byte[] byteSource, string code, UMPKeyAndIVType type) { string strResult = AnasysVerificationCodeAndKeyIV(code, type); if (!string.IsNullOrEmpty(strResult)) { throw new AuthenticationException("Vetification code invalid"); } byte[] byteKey = Encoding.ASCII.GetBytes(IStrEncryptDecryptKey); byte[] byteIV = Encoding.ASCII.GetBytes(IStrEncryptDecryptIV); var rijindeal = new RijndaelManaged(); rijindeal.Key = byteKey; rijindeal.IV = byteIV; rijindeal.Mode = CipherMode.CBC; rijindeal.Padding = PaddingMode.None; ICryptoTransform transform; if ((int)type > 100) { //解密 transform = rijindeal.CreateDecryptor(); } else { //加密 transform = rijindeal.CreateEncryptor(); } var byteResult = transform.TransformFinalBlock(byteSource, 0, byteSource.Length); return(byteResult); }
/// <summary> /// 字符串加密(SHA512)。加密后的密码不可逆。 /// </summary> /// <param name="AStrSource">待加密字符串</param> /// <param name="AStrVerificationCode">验证码</param> /// <param name="AIntEDType">加密方法,如果 = 0:直接SHA512后返回,> 0 : SHA512后根据该参数进行AES256加密</param> /// <returns>如果返回值与AStrSource,表示加密失败 /// Error01:加解密方法错误; /// Error02:验证码错误; /// </returns> public static string EncryptStringSHA512(string AStrSource, string AStrVerificationCode, UMPKeyAndIVType AIntEDType) { string LStrReturn = string.Empty; string LStrSource = string.Empty; try { LStrSource = AStrSource; if (!string.IsNullOrEmpty(LStrReturn)) { return(LStrReturn); } byte[] LByteTemp; SHA512 LSHA512Class = new SHA512Managed(); LByteTemp = LSHA512Class.ComputeHash(GetKeyByteArray(AStrSource)); LSHA512Class.Clear(); if (AIntEDType > 0) { if ((int)AIntEDType > 100) { return("Error01"); } LStrReturn = AnasysVerificationCodeAndKeyIV(AStrVerificationCode, AIntEDType); if (!string.IsNullOrEmpty(LStrReturn)) { return(LStrReturn); } LStrReturn = EncryptByte(LByteTemp); } else { LStrReturn = ConvertByteToHexStr(LByteTemp); } } catch { LStrReturn = AStrSource; } return(LStrReturn); }
private static string AnasysVerificationCodeAndKeyIV(string AStrVerificationCode, UMPKeyAndIVType AIntEDType) { string LStrReturn = string.Empty; string LStrVerificationCode = string.Empty; string LStrVCodeData = string.Empty; int LIntVCTIndex = 0, LIntUMPIndex = 0, LIntIVKeyIndex = 0; string LStrVCT = string.Empty, LStrUMP = string.Empty, LStrIVKey = string.Empty; try { LStrVerificationCode = DecryptString(AStrVerificationCode); if (LStrVerificationCode == AStrVerificationCode) { return("Error02"); } LIntVCTIndex = int.Parse(LStrVerificationCode.Substring(0, 2)); LIntUMPIndex = int.Parse(LStrVerificationCode.Substring(2, 2)); LIntIVKeyIndex = int.Parse(LStrVerificationCode.Substring(4, 2)); LStrVCodeData = LStrVerificationCode.Substring(6); LStrIVKey = LStrVCodeData.Substring(LIntIVKeyIndex, 3); LStrVCodeData = LStrVCodeData.Substring(0, LIntIVKeyIndex) + LStrVCodeData.Substring(LIntIVKeyIndex + 3); LStrUMP = LStrVCodeData.Substring(LIntUMPIndex, 3); LStrVCodeData = LStrVCodeData.Substring(0, LIntUMPIndex) + LStrVCodeData.Substring(LIntUMPIndex + 3); LStrVCT = LStrVCodeData.Substring(LIntVCTIndex, 3); if (int.Parse(LStrIVKey) != (int)AIntEDType) { LStrReturn = "Error02"; } IStrEncryptDecryptIV = string.Empty; IStrEncryptDecryptKey = string.Empty; if ((int)AIntEDType % 100 == 1) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_LOW1; } if ((int)AIntEDType % 100 == 4) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_NORMAL1; } } catch { LStrReturn = "Error02"; } return(LStrReturn); }
/// <summary> /// 字符串加密或解密 /// </summary> /// <param name="AStrSource">待加密或解密字符串</param> /// <param name="AStrVerificationCode">验证码</param> /// <param name="AIntEDType">加密或解密方法 (1、101):IVEC_LOW,KEY256_LOW1;(4、104):IVEC_LOW,KEY256_NORMAL1</param> /// <returns> /// Error01:加解密方法错误; /// Error02:验证码错误; /// Error03:待加密字符串长度错误,最大只能支持128 /// 如果返回值与AStrSource,表示加密或解密失败 /// </returns> public static string EncryptDecryptString(string AStrSource, string AStrVerificationCode, UMPKeyAndIVType AIntEDType) { string LStrReturn = string.Empty; string LStrSource = string.Empty; int LIntMaxLength = 0; try { LStrReturn = AnasysVerificationCodeAndKeyIV(AStrVerificationCode, AIntEDType); if (!string.IsNullOrEmpty(LStrReturn)) { return(LStrReturn); } if (string.IsNullOrEmpty(IStrEncryptDecryptIV) || string.IsNullOrEmpty(IStrEncryptDecryptKey)) { return("Error01"); } byte[] LByteArrayKey = Encoding.ASCII.GetBytes(IStrEncryptDecryptKey); byte[] LByteArrayAexIv = Encoding.ASCII.GetBytes(IStrEncryptDecryptIV); RijndaelManaged LRijndaelManaged = new RijndaelManaged(); LRijndaelManaged.Key = LByteArrayKey; LRijndaelManaged.Mode = CipherMode.CBC; LRijndaelManaged.Padding = PaddingMode.None; LRijndaelManaged.IV = LByteArrayAexIv; if ((int)AIntEDType < 100) { //字符串加密 LStrSource = AStrSource; LIntMaxLength = (int)Math.Ceiling(((double)LStrSource.Length / (double)8)) * 8; if (LIntMaxLength > 128) { return("Error03"); } while (LStrSource.Length < LIntMaxLength) { LStrSource += "\0"; } byte[] LByteArrayEncrypt = Encoding.Unicode.GetBytes(LStrSource); ICryptoTransform LICryptoTransform = LRijndaelManaged.CreateEncryptor(); byte[] LByteArrayResult = LICryptoTransform.TransformFinalBlock(LByteArrayEncrypt, 0, LByteArrayEncrypt.Length); LStrReturn = ConvertByteToHexStr(LByteArrayResult); } else { //字符串解密 LStrSource = ConvertHexToString(AStrSource); char[] LCharTemp = LStrSource.ToCharArray(); byte[] LByteEncrypt = new byte[LCharTemp.Length]; for (int LIntLoop = 0; LIntLoop < LCharTemp.Length; LIntLoop++) { LByteEncrypt[LIntLoop] = Convert.ToByte(LCharTemp[LIntLoop]); } ICryptoTransform LICryptoTransform = LRijndaelManaged.CreateDecryptor(); byte[] LByteArrayResult = LICryptoTransform.TransformFinalBlock(LByteEncrypt, 0, LByteEncrypt.Length); LStrReturn = Encoding.Unicode.GetString(LByteArrayResult); if (LStrReturn.IndexOf("\0") > 0) { LStrReturn = LStrReturn.Substring(0, LStrReturn.IndexOf("\0")); } } } catch { LStrReturn = AStrSource; } return(LStrReturn); }
private static string AnasysVerificationCodeAndKeyIV(string AStrVerificationCode, UMPKeyAndIVType AIntEDType) { string LStrReturn = string.Empty; string LStrVerificationCode = string.Empty; string LStrVCodeData = string.Empty; int LIntVCTIndex = 0, LIntUMPIndex = 0, LIntIVKeyIndex = 0; string LStrVCT = string.Empty, LStrUMP = string.Empty, LStrIVKey = string.Empty; try { LStrVerificationCode = DecryptString(AStrVerificationCode); if (LStrVerificationCode == AStrVerificationCode) { return("Error02"); } LIntVCTIndex = int.Parse(LStrVerificationCode.Substring(0, 2)); LIntUMPIndex = int.Parse(LStrVerificationCode.Substring(2, 2)); LIntIVKeyIndex = int.Parse(LStrVerificationCode.Substring(4, 2)); LStrVCodeData = LStrVerificationCode.Substring(6); LStrIVKey = LStrVCodeData.Substring(LIntIVKeyIndex, 3); LStrVCodeData = LStrVCodeData.Substring(0, LIntIVKeyIndex) + LStrVCodeData.Substring(LIntIVKeyIndex + 3); LStrUMP = LStrVCodeData.Substring(LIntUMPIndex, 3); LStrVCodeData = LStrVCodeData.Substring(0, LIntUMPIndex) + LStrVCodeData.Substring(LIntUMPIndex + 3); LStrVCT = LStrVCodeData.Substring(LIntVCTIndex, 3); if (int.Parse(LStrIVKey) != (int)AIntEDType) { LStrReturn = "Error02"; } IStrEncryptDecryptIV = string.Empty; IStrEncryptDecryptKey = string.Empty; #region IVEC_LOW,KEY256_LOW if ((int)AIntEDType % 100 == 1) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_LOW1; } if ((int)AIntEDType % 100 == 2) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_LOW2; } if ((int)AIntEDType % 100 == 3) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_LOW3; } #endregion #region IVEC_LOW,KEY256_NORMAL if ((int)AIntEDType % 100 == 4) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_NORMAL1; } if ((int)AIntEDType % 100 == 5) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_NORMAL2; } if ((int)AIntEDType % 100 == 6) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_NORMAL3; } #endregion #region IVEC_LOW,KEY256_HIGH if ((int)AIntEDType % 100 == 7) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_HIGH1; } if ((int)AIntEDType % 100 == 8) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_HIGH2; } if ((int)AIntEDType % 100 == 9) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_LOW; IStrEncryptDecryptKey = VCT_KEY256_HIGH3; } #endregion #region IVEC_NORMAL,KEY256_LOW if ((int)AIntEDType % 100 == 10) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_LOW1; } if ((int)AIntEDType % 100 == 11) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_LOW2; } if ((int)AIntEDType % 100 == 12) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_LOW3; } #endregion #region IVEC_NORMAL,KEY256_NORMAL if ((int)AIntEDType % 100 == 13) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_NORMAL1; } if ((int)AIntEDType % 100 == 14) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_NORMAL2; } if ((int)AIntEDType % 100 == 15) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_NORMAL3; } #endregion #region IVEC_NORMAL,KEY256_HIGH if ((int)AIntEDType % 100 == 16) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_HIGH1; } if ((int)AIntEDType % 100 == 17) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_HIGH2; } if ((int)AIntEDType % 100 == 18) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_NORMAL; IStrEncryptDecryptKey = VCT_KEY256_HIGH3; } #endregion #region IVEC_HIGH,KEY256_LOW if ((int)AIntEDType % 100 == 19) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_LOW1; } if ((int)AIntEDType % 100 == 20) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_LOW2; } if ((int)AIntEDType % 100 == 21) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_LOW3; } #endregion #region IVEC_HIGH,KEY256_NORMAL if ((int)AIntEDType % 100 == 22) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_NORMAL1; } if ((int)AIntEDType % 100 == 23) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_NORMAL2; } if ((int)AIntEDType % 100 == 24) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_NORMAL3; } #endregion #region IVEC_HIGH,KEY256_HIGH if ((int)AIntEDType % 100 == 25) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_HIGH1; } if ((int)AIntEDType % 100 == 26) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_HIGH2; } if ((int)AIntEDType % 100 == 27) { IStrEncryptDecryptIV = VCT_ENCRYPTION_AES_IVEC_HIGH; IStrEncryptDecryptKey = VCT_KEY256_HIGH3; } #endregion } catch { LStrReturn = "Error02"; } return(LStrReturn); }