|
|
using System;
|
|
|
using System.IO;
|
|
|
using System.IO.Compression;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
namespace BWP.ABCClient
|
|
|
{
|
|
|
[TestFixture]
|
|
|
public class Test
|
|
|
{
|
|
|
[Test]
|
|
|
public void B2BMockWebServer()
|
|
|
{
|
|
|
new MockWebServer("http://localhost:5000/", Encoding.UTF8, Encoding.UTF8, false);
|
|
|
Thread.Sleep(300000);
|
|
|
}
|
|
|
|
|
|
[Test]
|
|
|
public void RSASendReveive()
|
|
|
{
|
|
|
const string original = "message that will be sent";
|
|
|
|
|
|
Sender mySender = new Sender();
|
|
|
Receiver myReceiver = new Receiver();
|
|
|
|
|
|
byte[] toEncrypt = Encoding.Default.GetBytes(original);
|
|
|
|
|
|
byte[] encrypted = mySender.EncryptData(myReceiver.PublicKey, toEncrypt);
|
|
|
|
|
|
byte[] signature = mySender.HashAndSign(encrypted);
|
|
|
|
|
|
Console.WriteLine("Original: {0}", original);
|
|
|
|
|
|
if (myReceiver.VerifyHash(mySender.PublicKey, encrypted, signature)) {
|
|
|
myReceiver.DecryptData(encrypted);
|
|
|
} else {
|
|
|
Console.WriteLine("Invalid signature");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[Test]
|
|
|
public void TryReadPfx()
|
|
|
{
|
|
|
try {
|
|
|
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
|
|
|
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
|
|
|
X509Certificate2Collection collection = store.Certificates;
|
|
|
X509Certificate2Collection fcollection = collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
|
|
|
X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select", "Select a certificate from the following list to get information on that certificate", X509SelectionFlag.MultiSelection);
|
|
|
Console.WriteLine("Number of certificates: {0}{1}", scollection.Count, Environment.NewLine);
|
|
|
foreach (X509Certificate2 x509 in scollection) {
|
|
|
byte[] rawdata = x509.RawData;
|
|
|
Console.WriteLine("Content Type: {0}{1}", X509Certificate2.GetCertContentType(rawdata), Environment.NewLine);
|
|
|
Console.WriteLine("Friendly Name: {0}{1}", x509.FriendlyName, Environment.NewLine);
|
|
|
Console.WriteLine("Certificate Verified?: {0}{1}", x509.Verify(), Environment.NewLine);
|
|
|
Console.WriteLine("颁发给: {0}{1}", x509.Subject, Environment.NewLine);
|
|
|
Console.WriteLine("Simple Name: {0}{1}", x509.GetNameInfo(X509NameType.SimpleName, true), Environment.NewLine);
|
|
|
Console.WriteLine("Signature Algorithm: {0}{1}", x509.SignatureAlgorithm.FriendlyName, Environment.NewLine);
|
|
|
Console.WriteLine("Private Key: {0}{1}", x509.PrivateKey.ToXmlString(false), Environment.NewLine);
|
|
|
Console.WriteLine("Public Key: {0}{1}", x509.PublicKey.Key.ToXmlString(false), Environment.NewLine);
|
|
|
Console.WriteLine("Certificate Archived?: {0}{1}", x509.Archived, Environment.NewLine);
|
|
|
Console.WriteLine("Length of Raw Data: {0}{1}", x509.RawData.Length, Environment.NewLine);
|
|
|
X509Certificate2UI.DisplayCertificate(x509);
|
|
|
x509.Reset();
|
|
|
}
|
|
|
store.Close();
|
|
|
} catch (CryptographicException) {
|
|
|
Console.WriteLine("Information could not be written out for this certificate.");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[Test]
|
|
|
public void VerifySignedByABC()
|
|
|
{
|
|
|
string returnMsg = "<Merchant><ECMerchantType>B2C</ECMerchantType><MerchantID>103452083980409</MerchantID></Merchant><TrxResponse><ReturnCode>0000</ReturnCode><ErrorMessage>交易成功</ErrorMessage><TrxType>PayReq</TrxType><OrderNo>634333823922218745</OrderNo><PaymentURL>https://easyabc.95599.cn/b2c/NotCheckStatus/PaymentModeAct.ebf?TOKEN=12977568750460430844</PaymentURL><OrderAmount>254011</OrderAmount></TrxResponse>";
|
|
|
string ABCSignature = "3+0sE/7PTfYfHYqHUwWoFzUVvr0h2HUaXpq5pr+r3+DhyvufhVUPq1We9a0E+DzqzwnW3ZD5EYLhA204o4oRiLauEzM1cj8ddXZJGAtk5ftv1OGVCV+bvts/Ei9FQp8ws5b7pNqAvIyoSbIToartR7AJ42RJsT5DxXBtFy0Y2oQ=";
|
|
|
var cert = new X509Certificate2("Certs/TrustPay.cer");
|
|
|
var publicKey = (RSACryptoServiceProvider)cert.PublicKey.Key;
|
|
|
var singature = Convert.FromBase64String(ABCSignature);
|
|
|
var msgHash = new SHA1Managed().ComputeHash(Encoding.Default.GetBytes(returnMsg));
|
|
|
Assert.IsTrue(publicKey.VerifyHash(msgHash, cert.SignatureAlgorithm.Value, singature));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
internal class Sender
|
|
|
{
|
|
|
private readonly RSACryptoServiceProvider _privateKey;
|
|
|
private readonly RSACryptoServiceProvider _publicKey;
|
|
|
|
|
|
public RSACryptoServiceProvider PublicKey
|
|
|
{
|
|
|
get { return _publicKey; }
|
|
|
}
|
|
|
|
|
|
public Sender()
|
|
|
{
|
|
|
var cert = new X509Certificate2("Certs/asB.pfx", "14814622");
|
|
|
|
|
|
_privateKey = (RSACryptoServiceProvider)cert.PrivateKey;
|
|
|
_publicKey = (RSACryptoServiceProvider)cert.PublicKey.Key;
|
|
|
}
|
|
|
|
|
|
public byte[] HashAndSign(byte[] encryptedData)
|
|
|
{
|
|
|
byte[] hashedData = new SHA1Managed().ComputeHash(encryptedData);
|
|
|
return _privateKey.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA1"));
|
|
|
}
|
|
|
|
|
|
public byte[] EncryptData(RSACryptoServiceProvider receiverPublicKey, byte[] toEncrypt)
|
|
|
{
|
|
|
return receiverPublicKey.Encrypt(toEncrypt, false);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
internal class Receiver
|
|
|
{
|
|
|
private readonly RSACryptoServiceProvider _privateKey;
|
|
|
private readonly RSACryptoServiceProvider _publicKey;
|
|
|
|
|
|
public RSACryptoServiceProvider PublicKey
|
|
|
{
|
|
|
get { return _publicKey; }
|
|
|
}
|
|
|
|
|
|
public Receiver()
|
|
|
{
|
|
|
var cert = new X509Certificate2("Certs/asC.pfx", "14814622");
|
|
|
|
|
|
_privateKey = (RSACryptoServiceProvider)cert.PrivateKey;
|
|
|
_publicKey = (RSACryptoServiceProvider)cert.PublicKey.Key;
|
|
|
}
|
|
|
|
|
|
//Manually performs hash and then verifies hashed value.
|
|
|
public bool VerifyHash(RSACryptoServiceProvider senderPublicKey, byte[] encryptedData, byte[] signature)
|
|
|
{
|
|
|
byte[] hashedData = new SHA1Managed().ComputeHash(encryptedData);
|
|
|
return senderPublicKey.VerifyHash(hashedData, CryptoConfig.MapNameToOID("SHA1"), signature);
|
|
|
}
|
|
|
|
|
|
//Decrypt using the private key data.
|
|
|
public void DecryptData(byte[] encryptedData)
|
|
|
{
|
|
|
byte[] fromEncrypt = _privateKey.Decrypt(encryptedData, false);
|
|
|
string roundTrip = Encoding.Default.GetString(fromEncrypt);
|
|
|
|
|
|
Console.WriteLine("RoundTrip: {0}", roundTrip);
|
|
|
}
|
|
|
}
|
|
|
}
|