/// <summary></summary> public static string Authenticate(string securityToken) { Hashtable table = JsonHelper.ToHashtable(securityToken); string applicationId = table["applicationId"].ToString(); applicationId = StringHelper.ToGuid(new Guid(applicationId)); string applicationSecretSignal = table["applicationSecretSignal"].ToString(); if (string.IsNullOrEmpty(applicationId)) { throw new ArgumentNullException("securityToken.applicationId"); } else { ApplicationInfo param = AppsContext.Instance.ApplicationService.FindOne(applicationId); if (param == null) { throw new NullReferenceException("securityToken.applicationId"); } else { if (param.ApplicationSecret == Encrypter.DecryptAES(applicationSecretSignal, param.ApplicationKey, param.ApplicationKey)) { return(applicationId); } } } return("Unkwon SecurityToken"); }
public void TestDecryptAES() { byte[] key = UTF8Encoding.UTF8.GetBytes("1234567812345678"); byte[] iv = UTF8Encoding.UTF8.GetBytes("1234567812345678"); string result = Encrypter.DecryptAES("2eDiseYiSX62qk/WS/ZDmg==", key, iv, CiphertextFormat.Base64String); Assert.AreEqual("123456", result); result = Encrypter.DecryptAES("d9e0e2b1e622497eb6aa4fd64bf6439a", key, iv, CiphertextFormat.HexStringWhitoutHyphen); Assert.AreEqual("123456", result); }
public ActionResult AES(object args) { // 方法 加密|解密 var methodName = Request["methodName"]; // 原文 var originalText = ViewBag.OriginalText = Request["originalText"]; // 密文 var ciphertext = ViewBag.Ciphertext = Request["ciphertext"]; // 密钥 var cryptoKey = string.IsNullOrEmpty(Request["cryptoKey"]) ? SecurityConfigurationView.Instance.AESCryptoKey : Request["cryptoKey"]; // 密钥偏移量 var cryptoIV = string.IsNullOrEmpty(Request["cryptoIV"]) ? SecurityConfigurationView.Instance.AESCryptoIV : Request["cryptoIV"]; // 加密结果编码方式 var cryptoMode = string.IsNullOrEmpty(Request["cryptoMode"]) ? SecurityConfigurationView.Instance.AESCryptoMode : Request["cryptoMode"]; // 加密结果编码方式 var cryptoPadding = string.IsNullOrEmpty(Request["cryptoPadding"]) ? SecurityConfigurationView.Instance.AESCryptoPadding : Request["cryptoPadding"]; // 加密结果编码方式 var cryptoCiphertextFormat = string.IsNullOrEmpty(Request["cryptoCiphertextFormat"]) ? SecurityConfigurationView.Instance.AESCryptoCiphertextFormat : Request["cryptoCiphertextFormat"]; if (methodName == "encrypt" && !string.IsNullOrEmpty(originalText)) { ViewBag.Ciphertext = Encrypter.EncryptAES(originalText, cryptoKey, cryptoIV, (CipherMode)Enum.Parse(typeof(CipherMode), cryptoMode), (PaddingMode)Enum.Parse(typeof(PaddingMode), cryptoPadding), (CiphertextFormat)Enum.Parse(typeof(CiphertextFormat), cryptoCiphertextFormat)); } else if (methodName == "decrypt" && !string.IsNullOrEmpty(ciphertext)) { ViewBag.OriginalText = Encrypter.DecryptAES(ciphertext, cryptoKey, cryptoIV, (CipherMode)Enum.Parse(typeof(CipherMode), cryptoMode), (PaddingMode)Enum.Parse(typeof(PaddingMode), cryptoPadding), (CiphertextFormat)Enum.Parse(typeof(CiphertextFormat), cryptoCiphertextFormat)); } return(View("/views/main/sdk/aes.cshtml")); }
/// <summary>解密密码</summary> /// <param name="encryptedPassword">加密的密码</param> /// <returns>密码</returns> public string Decrypt(string encryptedPassword) { this.LoadKey(); return(Encrypter.DecryptAES(encryptedPassword, key, iv, CiphertextFormat.HexString)); }