public bool Validate(string name, string password) { using (var client = new HttpClient()) { var content = Encoder.Encrypt(string.Format("{0}:{1}", name, password)); return(client.FetchValueByPost <bool>(content, ValidateURL)); } }
public void Test_RsaHelper_Encrypt_CertNotFound() { byte[] bb = Guid.NewGuid().ToByteArray(); string error = ExceptionHelper.ExecuteActionReturnErrorMessage(() => { RsaHelper.Encrypt(bb, "abc"); }); Assert.AreEqual($"加密证书abc不存在。", error); }
public void TestRsaEncryptAndDecrypt() { string _data = "I am union guy." , _dataEncrypted = "" , _dataDecrypted = ""; RsaKeyPair _rsaKeyPair = new RsaKeyPair(); RsaKeyFormat _rsaKeyFormat = RsaKeyFormat.PEM; _dataEncrypted = RsaHelper.Encrypt(_rsaKeyFormat, _rsaKeyPair.PublicKey_PEM, _data); _dataDecrypted = RsaHelper.Decrypt(_rsaKeyFormat, _rsaKeyPair.PrivateKey_PEM, _dataEncrypted); Assert.AreEqual(_data, _dataDecrypted, string.Format("Data = {0}, DataEncrypted = {1}, DataDecrypted = {2}", _data, _dataEncrypted, _dataDecrypted)); }
public void Test_RsaHelper_Encrypt_Decrypt() { string s = @"已评估过类似方案,上海和融25版本做过类似改动,但需求有差异。 和融做此需求时改动量比较大,且与252版本跨系列了,参考价值降低。"; byte[] bb = Encoding.UTF8.GetBytes(s); byte[] b1 = RsaHelper.Encrypt(bb, s_certName); byte[] b2 = RsaHelper.Decrypt(b1, s_certName); string s2 = Encoding.UTF8.GetString(b2); Assert.AreEqual(s, s2); }
private Decrypt GetDecryptResult(string requestData, object Data) { var decrypt = Utils.GetDecrypt(requestData); RsaHelper rsa = new RsaHelper(null, Utils.RsaPubKey(decrypt.isdefault)); var aesKv = Utils.AesKV(); AESHelper aes = new AESHelper($"{aesKv}{aesKv}", aesKv); return(new Decrypt { secret = rsa.Encrypt(aesKv), encryption = aes.Encrypt(JsonHelper.Serialize(Data)), isdefault = decrypt.isdefault }); }
/// <summary> /// 加密要发送的数据,包含签名,AES加密,RSA加密AES密钥等步骤 /// </summary> /// <param name="data">要加密的正文明文数据</param> /// <returns>已加密待发送的密文</returns> public string EncryptData(string data) { data.CheckNotNull("data"); string separator = GetSeparator(); //获取正文摘要 string signData = RsaHelper.SignData(data, _ownPrivateKey); data = new[] { data, signData }.ExpandAndToString(separator); //使用AES加密 正文+摘要 AesHelper aes = new AesHelper(true); data = aes.Encrypt(data); //RSA加密AES密钥 byte[] keyBytes = aes.Key.ToBytes(); string enDesKey = Convert.ToBase64String(RsaHelper.Encrypt(keyBytes, _facePublicKey)); return(new[] { enDesKey, data }.ExpandAndToString(separator)); }
/// <summary> /// Rsa 加密 /// </summary> /// <param name="value">明文</param> /// <param name="publicKey">公钥</param> /// <param name="type">加密类型</param> /// <returns></returns> private static string RsaEncrypt(string value, string publicKey, RSAType type) { var rsa = new RsaHelper(type, Encoding.UTF8, publicKey: publicKey); return(rsa.Encrypt(value)); }
/// <summary> /// 重写Controler中的Json方法 /// </summary> /// <param name="context"></param> public override void ExecuteResult(System.Web.Mvc.ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } var requestData = (context.Controller as NetSSLController).RequestData; if (JsonRequestBehavior == Mvc.JsonRequestBehavior.DenyGet && String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("指定的操作不允许Get的Ajax请求方式访问"); } HttpResponseBase response = context.HttpContext.Response; if (!string.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Data != null) { if (requestData.Contains("secret") && requestData.Contains("encryption")) { var decrypt = Utils.GetDecrypt(requestData); RsaHelper rsa = new RsaHelper(null, Utils.RsaPubKey(decrypt.isdefault)); string secret = string.Empty; string encryption = string.Empty; if (decrypt.sectype == SecType.Des.GetHashCode()) { var desKv = Utils.DesKV(); secret = rsa.Encrypt(desKv); encryption = DesHelper.Encrypt(JsonHelper.Serialize(Data), desKv); } else { var aesKv = Utils.AesKV(); AESHelper aes = new AESHelper($"{aesKv}{aesKv}", aesKv); secret = rsa.Encrypt(aesKv); encryption = aes.Encrypt(JsonHelper.Serialize(Data)); } try { response.Write(JsonHelper.Serialize(new Decrypt { secret = secret, encryption = encryption, isdefault = decrypt.isdefault, sectype = decrypt.sectype })); } catch (Exception ex) { response.Write(JsonHelper.Serialize(JResult.Error($"加密失败,异常:{ex.Message}"))); } } else { response.Write(JsonHelper.Serialize(Data)); } } }