/// <summary> /// 采用标准RSA算法 /// </summary> /// <param name="EncryptString"></param> /// <returns></returns> public async static Task <string> RSAEncrypt(string EncryptString) { // var pempath = $"{Environment.CurrentDirectory}/{ConfigHelper.Configuration["WeChatPay:RsaPublicKey"]}"; if (!File.Exists(WxPayConfig.pempath)) { var PublicKey = await WxPayApi.Getpublickey(); if (!string.IsNullOrWhiteSpace(PublicKey)) { File.WriteAllText(WxPayConfig.pempath, PublicKey); } else { return("获取公钥失败!"); } } string R; // 加载公钥 RsaKeyParameters pubkey; using (var sr = new StreamReader(WxPayConfig.pempath)) { var pemReader = new PemReader(sr); pubkey = (RsaKeyParameters)pemReader.ReadObject(); } // 初始化cipher var cipher = (BufferedAsymmetricBlockCipher)CipherUtilities.GetCipher("RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING"); cipher.Init(true, pubkey); // 加密message var message = Encoding.UTF8.GetBytes(EncryptString); var output = EncryptUtil.Encrypt(message, cipher); R = Convert.ToBase64String(output); return(R); }