資源簡(jiǎn)介
// 加載公鑰
RsaKeyParameters pubkey;
using (var sr = new StreamReader(ConfigurationManager.AppSettings["PubKey"]))
{
var pemReader = new Org.BouncyCastle.OpenSsl.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);
代碼片段和文件信息
using?Org.BouncyCastle.Crypto;
using?Org.BouncyCastle.Crypto.Parameters;
using?Org.BouncyCastle.Security;
using?System;
using?System.Collections.Generic;
using?System.IO;
using?System.Text;
public?class?Account
{
????///?
????///?訂單確認(rèn)消費(fèi)之后平臺(tái)打錢到商家銀行卡
????///?
????///?
????public?void?OrderConfirm(int?ID)
????{
????????PayToUser?pay?=?PayOrderUtil.GetPayToUser();
????????try
????????{
????????????/*加密銀行卡號(hào)和用戶姓名*/
????????????pay.enc_bank_no?=?RSAEncrypt(pay.enc_bank_no);
????????????pay.enc_true_name?=?RSAEncrypt(pay.enc_true_name);
????????????PayToUser(pay);
????????}
????????catch?(Exception?ex)
????????{
????????????//Log4Helper.LogInfo(“PayLogger“?ex.Message?ex);
????????}
????}
????public?string?GetPublicKey()
????{
????????SortedDictionaryject>?di?=?new?SortedDictionaryject>();
????????di.Add(“mch_id“?Config.merchant_no);
????????di.Add(“nonce_str“?WeChatUtil.getNoncestr());
????????di.Add(“sign_type“?“MD5“);
????????di.Add(“sign“?WeChatUtil.CreateSign(di?Config.pay_secret));
????????var?data?=?WeChatUtil.WeChatSignxml(di);
????????string?result?=?WeChatUtil.WxCerHttpPost(“https://fraud.mch.weixin.qq.com/risk/getpublickey“?data?Config.WeChatCre?Config.merchant_no);
????????if?(!string.IsNullOrWhiteSpace(result))
????????{
????????????//Log4Helper.LogInfo(“PayLogger“?result);
????????????var?returnxml?=?xmlUtil.xmlToObect(result);
????????????if?(returnxml.return_code?==?“SUCCESS“)
????????????{
????????????????if?(returnxml.result_code?==?“SUCCESS“)
????????????????{
????????????????????return?returnxml.pub_key;
????????????????}
????????????}
????????}
????????return?““;
????}
????public?string?RSAEncrypt(string?EncryptString)
????{
????????if?(!File.Exists(Config.PubKey))
????????{
????????????var?PublicKey?=?GetPublicKey();
????????????if?(!string.IsNullOrWhiteSpace(PublicKey))
????????????{
????????????????File.WriteAllText(Config.PubKey?PublicKey);
????????????}
????????????else
????????????????return?“獲取公鑰失敗!“;
????????}
????????string?R;
????????//?加載公鑰
????????RsaKeyParameters?pubkey;
????????using?(var?sr?=?new?StreamReader(Config.PubKey))
????????{
????????????var?pemReader?=?new?Org.BouncyCastle.OpenSsl.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;
????}
????private?static?void?PayToUser(PayToUser?pay)
????{
????????/*1.生成簽名*/
????????SortedDictionaryject>?dics?=?new?SortedDictionaryject>();
????????dics.Add(“mch_id
評(píng)論
共有 條評(píng)論