feat(tenpayv3): 支持多种 RSA 填充方式及算法

This commit is contained in:
Fu Diwei 2022-02-24 14:46:59 +08:00
parent 9a8a00f05a
commit ffcc010caf
29 changed files with 118 additions and 82 deletions

View File

@ -4,6 +4,8 @@
{ {
public const string AEAD_AES_256_GCM = "AEAD_AES_256_GCM"; public const string AEAD_AES_256_GCM = "AEAD_AES_256_GCM";
public const string RSA_2048_PKCS8_ECB = "RSA_2048_PKCS8_ECB"; public const string RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1 = "RSA_2048_ECB_PKCS8_OAEPWithSHA-1AndMGF1";
public const string RSA_2048_ECB_PKCS1 = "RSA_2048_ECB_PKCS1";
} }
} }

View File

@ -33,7 +33,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
if (attr == null) if (attr == null)
return (false, oldValue); return (false, oldValue);
if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm)) if (Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1.Equals(attr.Algorithm) ||
Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS1.Equals(attr.Algorithm))
{ {
if (client.PlatformCertificateManager == null) if (client.PlatformCertificateManager == null)
throw new Exceptions.WechatTenpayRequestEncryptionException("Encrypt request failed, because there is no platform certificate in the manager."); throw new Exceptions.WechatTenpayRequestEncryptionException("Encrypt request failed, because there is no platform certificate in the manager.");
@ -65,10 +66,23 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
request.WechatpayCertificateSerialNumber = cert.SerialNumber; request.WechatpayCertificateSerialNumber = cert.SerialNumber;
} }
string newValue = Utilities.RSAUtility.EncryptWithECBByCertificate( string newValue = oldValue;
certificate: certificate, if (Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1.Equals(attr.Algorithm))
plainText: oldValue {
); newValue = Utilities.RSAUtility.EncryptWithECBByCertificate(
certificate: certificate,
plainText: oldValue
);
}
else if(Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS1.Equals(attr.Algorithm))
{
newValue = Utilities.RSAUtility.EncryptWithECBByCertificate(
certificate: certificate,
plainText: oldValue,
paddingAlgorithm: "PKCS1PADDING"
);
}
return (true, newValue); return (true, newValue);
} }
else else

View File

@ -65,12 +65,26 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
if (attr == null) if (attr == null)
return (false, oldValue); return (false, oldValue);
if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm)) if (Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1.Equals(attr.Algorithm) ||
Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS1.Equals(attr.Algorithm))
{ {
string newValue = Utilities.RSAUtility.DecryptWithECB( string newValue = oldValue;
privateKey: client.Credentials.MerchantCertificatePrivateKey, if (Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1.Equals(attr.Algorithm))
cipherText: oldValue {
); newValue = Utilities.RSAUtility.DecryptWithECB(
privateKey: client.Credentials.MerchantCertificatePrivateKey,
cipherText: oldValue
);
}
else if (Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS1.Equals(attr.Algorithm))
{
newValue = Utilities.RSAUtility.DecryptWithECB(
privateKey: client.Credentials.MerchantCertificatePrivateKey,
cipherText: oldValue,
paddingAlgorithm: "PKCS1PADDING"
);
}
return (true, newValue); return (true, newValue);
} }
else else

View File

@ -18,7 +18,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("contact_name")] [Newtonsoft.Json.JsonProperty("contact_name")]
[System.Text.Json.Serialization.JsonPropertyName("contact_name")] [System.Text.Json.Serialization.JsonPropertyName("contact_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -26,7 +26,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("mobile_phone")] [Newtonsoft.Json.JsonProperty("mobile_phone")]
[System.Text.Json.Serialization.JsonPropertyName("mobile_phone")] [System.Text.Json.Serialization.JsonPropertyName("mobile_phone")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string Mobile { get; set; } = string.Empty; public string Mobile { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -34,7 +34,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("contact_id_number")] [Newtonsoft.Json.JsonProperty("contact_id_number")]
[System.Text.Json.Serialization.JsonPropertyName("contact_id_number")] [System.Text.Json.Serialization.JsonPropertyName("contact_id_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? IdCardNumber { get; set; } public string? IdCardNumber { get; set; }
/// <summary> /// <summary>
@ -49,7 +49,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("contact_email")] [Newtonsoft.Json.JsonProperty("contact_email")]
[System.Text.Json.Serialization.JsonPropertyName("contact_email")] [System.Text.Json.Serialization.JsonPropertyName("contact_email")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string Email { get; set; } = string.Empty; public string Email { get; set; } = string.Empty;
} }
@ -203,7 +203,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_card_name")] [Newtonsoft.Json.JsonProperty("id_card_name")]
[System.Text.Json.Serialization.JsonPropertyName("id_card_name")] [System.Text.Json.Serialization.JsonPropertyName("id_card_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdCardName { get; set; } = string.Empty; public string IdCardName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -211,7 +211,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_card_number")] [Newtonsoft.Json.JsonProperty("id_card_number")]
[System.Text.Json.Serialization.JsonPropertyName("id_card_number")] [System.Text.Json.Serialization.JsonPropertyName("id_card_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdCardNumber { get; set; } = string.Empty; public string IdCardNumber { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -243,7 +243,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_doc_name")] [Newtonsoft.Json.JsonProperty("id_doc_name")]
[System.Text.Json.Serialization.JsonPropertyName("id_doc_name")] [System.Text.Json.Serialization.JsonPropertyName("id_doc_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdDocumentName { get; set; } = string.Empty; public string IdDocumentName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -251,7 +251,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_doc_number")] [Newtonsoft.Json.JsonProperty("id_doc_number")]
[System.Text.Json.Serialization.JsonPropertyName("id_doc_number")] [System.Text.Json.Serialization.JsonPropertyName("id_doc_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdDocumentNumber { get; set; } = string.Empty; public string IdDocumentNumber { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -334,7 +334,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -342,7 +342,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_number")] [Newtonsoft.Json.JsonProperty("id_number")]
[System.Text.Json.Serialization.JsonPropertyName("id_number")] [System.Text.Json.Serialization.JsonPropertyName("id_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdNumber { get; set; } = string.Empty; public string IdNumber { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -719,7 +719,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("account_name")] [Newtonsoft.Json.JsonProperty("account_name")]
[System.Text.Json.Serialization.JsonPropertyName("account_name")] [System.Text.Json.Serialization.JsonPropertyName("account_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string AccountName { get; set; } = string.Empty; public string AccountName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -727,7 +727,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("account_number")] [Newtonsoft.Json.JsonProperty("account_number")]
[System.Text.Json.Serialization.JsonPropertyName("account_number")] [System.Text.Json.Serialization.JsonPropertyName("account_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string AccountNumber { get; set; } = string.Empty; public string AccountNumber { get; set; } = string.Empty;
/// <summary> /// <summary>

View File

@ -25,7 +25,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("account_number")] [Newtonsoft.Json.JsonProperty("account_number")]
[System.Text.Json.Serialization.JsonPropertyName("account_number")] [System.Text.Json.Serialization.JsonPropertyName("account_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string AccountNumber { get; set; } = string.Empty; public string AccountNumber { get; set; } = string.Empty;
/// <summary> /// <summary>

View File

@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -25,7 +25,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("mobile")] [Newtonsoft.Json.JsonProperty("mobile")]
[System.Text.Json.Serialization.JsonPropertyName("mobile")] [System.Text.Json.Serialization.JsonPropertyName("mobile")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string Mobile { get; set; } = string.Empty; public string Mobile { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -33,7 +33,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_card_number")] [Newtonsoft.Json.JsonProperty("id_card_number")]
[System.Text.Json.Serialization.JsonPropertyName("id_card_number")] [System.Text.Json.Serialization.JsonPropertyName("id_card_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdCardNumber { get; set; } = string.Empty; public string IdCardNumber { get; set; } = string.Empty;
} }
@ -258,7 +258,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("identification_name")] [Newtonsoft.Json.JsonProperty("identification_name")]
[System.Text.Json.Serialization.JsonPropertyName("identification_name")] [System.Text.Json.Serialization.JsonPropertyName("identification_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdentificationName { get; set; } = string.Empty; public string IdentificationName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -266,7 +266,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("identification_number")] [Newtonsoft.Json.JsonProperty("identification_number")]
[System.Text.Json.Serialization.JsonPropertyName("identification_number")] [System.Text.Json.Serialization.JsonPropertyName("identification_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdentificationNumber { get; set; } = string.Empty; public string IdentificationNumber { get; set; } = string.Empty;
/// <summary> /// <summary>

View File

@ -43,7 +43,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("encrypt_key")] [Newtonsoft.Json.JsonProperty("encrypt_key")]
[System.Text.Json.Serialization.JsonPropertyName("encrypt_key")] [System.Text.Json.Serialization.JsonPropertyName("encrypt_key")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string EncryptKey { get; set; } = default!; public string EncryptKey { get; set; } = default!;
/// <summary> /// <summary>

View File

@ -31,7 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? Name { get; set; } public string? Name { get; set; }
/// <summary> /// <summary>

View File

@ -11,7 +11,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonIgnore] [Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore] [System.Text.Json.Serialization.JsonIgnore]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string AccountNumber { get; set; } = string.Empty; public string AccountNumber { get; set; } = string.Empty;
} }
} }

View File

@ -100,7 +100,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_card_name")] [Newtonsoft.Json.JsonProperty("id_card_name")]
[System.Text.Json.Serialization.JsonPropertyName("id_card_name")] [System.Text.Json.Serialization.JsonPropertyName("id_card_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdCardName { get; set; } = string.Empty; public string IdCardName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -108,7 +108,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_card_number")] [Newtonsoft.Json.JsonProperty("id_card_number")]
[System.Text.Json.Serialization.JsonPropertyName("id_card_number")] [System.Text.Json.Serialization.JsonPropertyName("id_card_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdCardNumber { get; set; } = string.Empty; public string IdCardNumber { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -133,7 +133,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_doc_name")] [Newtonsoft.Json.JsonProperty("id_doc_name")]
[System.Text.Json.Serialization.JsonPropertyName("id_doc_name")] [System.Text.Json.Serialization.JsonPropertyName("id_doc_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdDocumentName { get; set; } = string.Empty; public string IdDocumentName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -141,7 +141,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("id_doc_number")] [Newtonsoft.Json.JsonProperty("id_doc_number")]
[System.Text.Json.Serialization.JsonPropertyName("id_doc_number")] [System.Text.Json.Serialization.JsonPropertyName("id_doc_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string IdDocumentNumber { get; set; } = string.Empty; public string IdDocumentNumber { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -166,7 +166,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("contact_name")] [Newtonsoft.Json.JsonProperty("contact_name")]
[System.Text.Json.Serialization.JsonPropertyName("contact_name")] [System.Text.Json.Serialization.JsonPropertyName("contact_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string ContactName { get; set; } = string.Empty; public string ContactName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -174,7 +174,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("mobile_phone")] [Newtonsoft.Json.JsonProperty("mobile_phone")]
[System.Text.Json.Serialization.JsonPropertyName("mobile_phone")] [System.Text.Json.Serialization.JsonPropertyName("mobile_phone")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string ContactMobile { get; set; } = string.Empty; public string ContactMobile { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -182,7 +182,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("contact_id_card_number")] [Newtonsoft.Json.JsonProperty("contact_id_card_number")]
[System.Text.Json.Serialization.JsonPropertyName("contact_id_card_number")] [System.Text.Json.Serialization.JsonPropertyName("contact_id_card_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string ContactIdCardNumber { get; set; } = string.Empty; public string ContactIdCardNumber { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -190,7 +190,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("contact_email")] [Newtonsoft.Json.JsonProperty("contact_email")]
[System.Text.Json.Serialization.JsonPropertyName("contact_email")] [System.Text.Json.Serialization.JsonPropertyName("contact_email")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string ContactEmail { get; set; } = string.Empty; public string ContactEmail { get; set; } = string.Empty;
} }
@ -208,7 +208,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("account_name")] [Newtonsoft.Json.JsonProperty("account_name")]
[System.Text.Json.Serialization.JsonPropertyName("account_name")] [System.Text.Json.Serialization.JsonPropertyName("account_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string AccountName { get; set; } = string.Empty; public string AccountName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -216,7 +216,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("account_number")] [Newtonsoft.Json.JsonProperty("account_number")]
[System.Text.Json.Serialization.JsonPropertyName("account_number")] [System.Text.Json.Serialization.JsonPropertyName("account_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string AccountNumber { get; set; } = string.Empty; public string AccountNumber { get; set; } = string.Empty;
/// <summary> /// <summary>

View File

@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("account_name")] [Newtonsoft.Json.JsonProperty("account_name")]
[System.Text.Json.Serialization.JsonPropertyName("account_name")] [System.Text.Json.Serialization.JsonPropertyName("account_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string AccountName { get; set; } = default!; public string AccountName { get; set; } = default!;
/// <summary> /// <summary>
@ -25,7 +25,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("account_no")] [Newtonsoft.Json.JsonProperty("account_no")]
[System.Text.Json.Serialization.JsonPropertyName("account_no")] [System.Text.Json.Serialization.JsonPropertyName("account_no")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? AccountNumber { get; set; } public string? AccountNumber { get; set; }
/// <summary> /// <summary>

View File

@ -43,7 +43,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("encrypt_key")] [Newtonsoft.Json.JsonProperty("encrypt_key")]
[System.Text.Json.Serialization.JsonPropertyName("encrypt_key")] [System.Text.Json.Serialization.JsonPropertyName("encrypt_key")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string EncryptKey { get; set; } = default!; public string EncryptKey { get; set; } = default!;
/// <summary> /// <summary>

View File

@ -31,7 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("receiver_name")] [Newtonsoft.Json.JsonProperty("receiver_name")]
[System.Text.Json.Serialization.JsonPropertyName("receiver_name")] [System.Text.Json.Serialization.JsonPropertyName("receiver_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? Name { get; set; } public string? Name { get; set; }
/// <summary> /// <summary>

View File

@ -39,7 +39,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("encrypted_name")] [Newtonsoft.Json.JsonProperty("encrypted_name")]
[System.Text.Json.Serialization.JsonPropertyName("encrypted_name")] [System.Text.Json.Serialization.JsonPropertyName("encrypted_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? EncryptedName { get; set; } public string? EncryptedName { get; set; }
/// <summary> /// <summary>

View File

@ -17,7 +17,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("encrypted_phone_number")] [Newtonsoft.Json.JsonProperty("encrypted_phone_number")]
[System.Text.Json.Serialization.JsonPropertyName("encrypted_phone_number")] [System.Text.Json.Serialization.JsonPropertyName("encrypted_phone_number")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string EncryptedPhoneNumber { get; set; } = string.Empty; public string EncryptedPhoneNumber { get; set; } = string.Empty;
/// <summary> /// <summary>

View File

@ -94,7 +94,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("payer_phone")] [Newtonsoft.Json.JsonProperty("payer_phone")]
[System.Text.Json.Serialization.JsonPropertyName("payer_phone")] [System.Text.Json.Serialization.JsonPropertyName("payer_phone")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? PayerPhone { get; set; } public string? PayerPhone { get; set; }
/// <summary> /// <summary>

View File

@ -58,7 +58,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("payer_phone")] [Newtonsoft.Json.JsonProperty("payer_phone")]
[System.Text.Json.Serialization.JsonPropertyName("payer_phone")] [System.Text.Json.Serialization.JsonPropertyName("payer_phone")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? PayerPhone { get; set; } public string? PayerPhone { get; set; }
/// <summary> /// <summary>

View File

@ -18,7 +18,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("username")] [Newtonsoft.Json.JsonProperty("username")]
[System.Text.Json.Serialization.JsonPropertyName("username")] [System.Text.Json.Serialization.JsonPropertyName("username")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public override string UserName { get; set; } = default!; public override string UserName { get; set; } = default!;
} }
} }

View File

@ -43,7 +43,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("encrypt_key")] [Newtonsoft.Json.JsonProperty("encrypt_key")]
[System.Text.Json.Serialization.JsonPropertyName("encrypt_key")] [System.Text.Json.Serialization.JsonPropertyName("encrypt_key")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string EncryptKey { get; set; } = default!; public string EncryptKey { get; set; } = default!;
/// <summary> /// <summary>

View File

@ -31,7 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? Name { get; set; } public string? Name { get; set; }
/// <summary> /// <summary>

View File

@ -46,7 +46,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? Name { get; set; } public string? Name { get; set; }
/// <summary> /// <summary>

View File

@ -32,7 +32,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? Name { get; set; } public string? Name { get; set; }
/// <summary> /// <summary>

View File

@ -39,7 +39,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string UserName { get; set; } = string.Empty; public string UserName { get; set; } = string.Empty;
/// <summary> /// <summary>
@ -47,7 +47,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("mobile")] [Newtonsoft.Json.JsonProperty("mobile")]
[System.Text.Json.Serialization.JsonPropertyName("mobile")] [System.Text.Json.Serialization.JsonPropertyName("mobile")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string UserMobile { get; set; } = string.Empty; public string UserMobile { get; set; } = string.Empty;
/// <summary> /// <summary>

View File

@ -32,7 +32,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonIgnore] [Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore] [System.Text.Json.Serialization.JsonIgnore]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? UserMobile { get; set; } public string? UserMobile { get; set; }
/// <summary> /// <summary>

View File

@ -36,7 +36,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string UserName { get; set; } = default!; public string UserName { get; set; } = default!;
/// <summary> /// <summary>
@ -44,7 +44,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("mobile")] [Newtonsoft.Json.JsonProperty("mobile")]
[System.Text.Json.Serialization.JsonPropertyName("mobile")] [System.Text.Json.Serialization.JsonPropertyName("mobile")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string UserMobile { get; set; } = default!; public string UserMobile { get; set; } = default!;
/// <summary> /// <summary>

View File

@ -25,7 +25,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("name")] [Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")] [System.Text.Json.Serialization.JsonPropertyName("name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? UserName { get; set; } public string? UserName { get; set; }
/// <summary> /// <summary>
@ -33,7 +33,7 @@
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("mobile")] [Newtonsoft.Json.JsonProperty("mobile")]
[System.Text.Json.Serialization.JsonPropertyName("mobile")] [System.Text.Json.Serialization.JsonPropertyName("mobile")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? UserMobile { get; set; } public string? UserMobile { get; set; }
/// <summary> /// <summary>

View File

@ -45,7 +45,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("user_name")] [Newtonsoft.Json.JsonProperty("user_name")]
[System.Text.Json.Serialization.JsonPropertyName("user_name")] [System.Text.Json.Serialization.JsonPropertyName("user_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public string? UserName { get; set; } public string? UserName { get; set; }
} }
} }

View File

@ -83,7 +83,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
/// </summary> /// </summary>
[Newtonsoft.Json.JsonProperty("user_name")] [Newtonsoft.Json.JsonProperty("user_name")]
[System.Text.Json.Serialization.JsonPropertyName("user_name")] [System.Text.Json.Serialization.JsonPropertyName("user_name")]
[WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB)] [WechatTenpaySensitiveProperty(algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
public virtual string UserName { get; set; } = default!; public virtual string UserName { get; set; } = default!;
/// <summary> /// <summary>

View File

@ -16,9 +16,10 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
public static class RSAUtility public static class RSAUtility
{ {
// REF: https://github.com/bcgit/bc-csharp/blob/master/crypto/src/security/CipherUtilities.cs // REF: https://github.com/bcgit/bc-csharp/blob/master/crypto/src/security/CipherUtilities.cs
private const string RSA_CIPHER_ALG = "RSA/ECB/OAEPWITHSHA1ANDMGF1PADDING"; private const string RSA_CIPHER_ALGORITHM_ECB = "RSA/ECB";
private const string RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1 = "OAEPWITHSHA1ANDMGF1PADDING";
// REF: https://github.com/bcgit/bc-csharp/blob/master/crypto/src/security/SignerUtilities.cs // REF: https://github.com/bcgit/bc-csharp/blob/master/crypto/src/security/SignerUtilities.cs
private const string RSA_SIGNER_ALG = "SHA-256withRSA"; private const string RSA_SIGNER_ALGORITHM_SHA256 = "SHA-256withRSA";
/// <summary> /// <summary>
/// 使用私钥基于 SHA-256 算法生成签名。 /// 使用私钥基于 SHA-256 算法生成签名。
@ -112,14 +113,15 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
/// </summary> /// </summary>
/// <param name="privateKeyBytes">PKCS#8 私钥字节数据。</param> /// <param name="privateKeyBytes">PKCS#8 私钥字节数据。</param>
/// <param name="cipherBytes">待解密的数据字节数据。</param> /// <param name="cipherBytes">待解密的数据字节数据。</param>
/// <param name="paddingAlgorithm">填充算法。(默认值:<see cref="RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1"/></param>
/// <returns>解密后的数据字节数组。</returns> /// <returns>解密后的数据字节数组。</returns>
public static byte[] DecryptWithECB(byte[] privateKeyBytes, byte[] cipherBytes) public static byte[] DecryptWithECB(byte[] privateKeyBytes, byte[] cipherBytes, string paddingAlgorithm = RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1)
{ {
if (privateKeyBytes == null) throw new ArgumentNullException(nameof(privateKeyBytes)); if (privateKeyBytes == null) throw new ArgumentNullException(nameof(privateKeyBytes));
if (cipherBytes == null) throw new ArgumentNullException(nameof(cipherBytes)); if (cipherBytes == null) throw new ArgumentNullException(nameof(cipherBytes));
RsaKeyParameters rsaKeyParams = (RsaKeyParameters)PrivateKeyFactory.CreateKey(privateKeyBytes); RsaKeyParameters rsaKeyParams = (RsaKeyParameters)PrivateKeyFactory.CreateKey(privateKeyBytes);
return DecryptWithECB(rsaKeyParams, cipherBytes); return DecryptWithECB(rsaKeyParams, cipherBytes, paddingAlgorithm);
} }
/// <summary> /// <summary>
@ -127,15 +129,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
/// </summary> /// </summary>
/// <param name="privateKey">PKCS#8 私钥PEM 格式)。</param> /// <param name="privateKey">PKCS#8 私钥PEM 格式)。</param>
/// <param name="cipherText">经 Base64 编码的待解密数据。</param> /// <param name="cipherText">经 Base64 编码的待解密数据。</param>
/// <param name="paddingAlgorithm">填充算法。(默认值:<see cref="RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1"/></param>
/// <returns>解密后的文本数据。</returns> /// <returns>解密后的文本数据。</returns>
public static string DecryptWithECB(string privateKey, string cipherText) public static string DecryptWithECB(string privateKey, string cipherText, string paddingAlgorithm = RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1)
{ {
if (privateKey == null) throw new ArgumentNullException(nameof(privateKey)); if (privateKey == null) throw new ArgumentNullException(nameof(privateKey));
if (cipherText == null) throw new ArgumentNullException(nameof(cipherText)); if (cipherText == null) throw new ArgumentNullException(nameof(cipherText));
byte[] privateKeyBytes = ConvertPkcs8PrivateKeyToByteArray(privateKey); byte[] privateKeyBytes = ConvertPkcs8PrivateKeyToByteArray(privateKey);
byte[] cipherBytes = Convert.FromBase64String(cipherText); byte[] cipherBytes = Convert.FromBase64String(cipherText);
byte[] plainBytes = DecryptWithECB(privateKeyBytes, cipherBytes); byte[] plainBytes = DecryptWithECB(privateKeyBytes, cipherBytes, paddingAlgorithm);
return Encoding.UTF8.GetString(plainBytes); return Encoding.UTF8.GetString(plainBytes);
} }
@ -144,14 +147,15 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
/// </summary> /// </summary>
/// <param name="publicKeyBytes">PKCS#8 公钥字节数据。</param> /// <param name="publicKeyBytes">PKCS#8 公钥字节数据。</param>
/// <param name="plainBytes">待加密的数据字节数据。</param> /// <param name="plainBytes">待加密的数据字节数据。</param>
/// <param name="paddingAlgorithm">填充算法。(默认值:<see cref="RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1"/></param>
/// <returns>加密后的数据字节数组。</returns> /// <returns>加密后的数据字节数组。</returns>
public static byte[] EncryptWithECB(byte[] publicKeyBytes, byte[] plainBytes) public static byte[] EncryptWithECB(byte[] publicKeyBytes, byte[] plainBytes, string paddingAlgorithm = RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1)
{ {
if (publicKeyBytes == null) throw new ArgumentNullException(nameof(publicKeyBytes)); if (publicKeyBytes == null) throw new ArgumentNullException(nameof(publicKeyBytes));
if (plainBytes == null) throw new ArgumentNullException(nameof(plainBytes)); if (plainBytes == null) throw new ArgumentNullException(nameof(plainBytes));
RsaKeyParameters rsaKeyParams = (RsaKeyParameters)PublicKeyFactory.CreateKey(publicKeyBytes); RsaKeyParameters rsaKeyParams = (RsaKeyParameters)PublicKeyFactory.CreateKey(publicKeyBytes);
return EncryptWithECB(rsaKeyParams, plainBytes); return EncryptWithECB(rsaKeyParams, plainBytes, paddingAlgorithm);
} }
/// <summary> /// <summary>
@ -159,15 +163,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
/// </summary> /// </summary>
/// <param name="publicKey">PKCS#8 公钥PEM 格式)。</param> /// <param name="publicKey">PKCS#8 公钥PEM 格式)。</param>
/// <param name="plainText">待加密的文本数据。</param> /// <param name="plainText">待加密的文本数据。</param>
/// <param name="paddingAlgorithm">填充算法。(默认值:<see cref="RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1"/></param>
/// <returns>经 Base64 编码的加密数据。</returns> /// <returns>经 Base64 编码的加密数据。</returns>
public static string EncryptWithECB(string publicKey, string plainText) public static string EncryptWithECB(string publicKey, string plainText, string paddingAlgorithm = RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1)
{ {
if (publicKey == null) throw new ArgumentNullException(nameof(publicKey)); if (publicKey == null) throw new ArgumentNullException(nameof(publicKey));
if (plainText == null) throw new ArgumentNullException(nameof(plainText)); if (plainText == null) throw new ArgumentNullException(nameof(plainText));
byte[] publicKeyBytes = ConvertPkcs8PublicKeyToByteArray(publicKey); byte[] publicKeyBytes = ConvertPkcs8PublicKeyToByteArray(publicKey);
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
byte[] cipherBytes = EncryptWithECB(publicKeyBytes, plainBytes); byte[] cipherBytes = EncryptWithECB(publicKeyBytes, plainBytes, paddingAlgorithm);
return Convert.ToBase64String(cipherBytes); return Convert.ToBase64String(cipherBytes);
} }
@ -176,15 +181,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
/// </summary> /// </summary>
/// <param name="certificate">证书PEM 格式)。</param> /// <param name="certificate">证书PEM 格式)。</param>
/// <param name="plainText">待加密的文本数据。</param> /// <param name="plainText">待加密的文本数据。</param>
/// <param name="paddingAlgorithm">填充算法。(默认值:<see cref="RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1"/></param>
/// <returns>经 Base64 编码的加密数据。</returns> /// <returns>经 Base64 编码的加密数据。</returns>
public static string EncryptWithECBByCertificate(string certificate, string plainText) public static string EncryptWithECBByCertificate(string certificate, string plainText, string paddingAlgorithm = RSA_CIPHER_PADDING_OAEP_WITH_SHA1_AND_MGF1)
{ {
if (certificate == null) throw new ArgumentNullException(nameof(certificate)); if (certificate == null) throw new ArgumentNullException(nameof(certificate));
if (plainText == null) throw new ArgumentNullException(nameof(plainText)); if (plainText == null) throw new ArgumentNullException(nameof(plainText));
RsaKeyParameters rsaKeyParams = ConvertCertificateToPublicKeyParams(certificate); RsaKeyParameters rsaKeyParams = ConvertCertificateToPublicKeyParams(certificate);
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
byte[] cipherBytes = EncryptWithECB(rsaKeyParams, plainBytes); byte[] cipherBytes = EncryptWithECB(rsaKeyParams, plainBytes, paddingAlgorithm);
return Convert.ToBase64String(cipherBytes); return Convert.ToBase64String(cipherBytes);
} }
@ -285,7 +291,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
private static byte[] SignWithSHA256(RsaKeyParameters rsaKeyParams, byte[] plainBytes) private static byte[] SignWithSHA256(RsaKeyParameters rsaKeyParams, byte[] plainBytes)
{ {
ISigner signer = SignerUtilities.GetSigner(RSA_SIGNER_ALG); ISigner signer = SignerUtilities.GetSigner(RSA_SIGNER_ALGORITHM_SHA256);
signer.Init(true, rsaKeyParams); signer.Init(true, rsaKeyParams);
signer.BlockUpdate(plainBytes, 0, plainBytes.Length); signer.BlockUpdate(plainBytes, 0, plainBytes.Length);
return signer.GenerateSignature(); return signer.GenerateSignature();
@ -293,22 +299,22 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
private static bool VerifyWithSHA256(RsaKeyParameters rsaKeyParams, byte[] plainBytes, byte[] signBytes) private static bool VerifyWithSHA256(RsaKeyParameters rsaKeyParams, byte[] plainBytes, byte[] signBytes)
{ {
ISigner signer = SignerUtilities.GetSigner(RSA_SIGNER_ALG); ISigner signer = SignerUtilities.GetSigner(RSA_SIGNER_ALGORITHM_SHA256);
signer.Init(false, rsaKeyParams); signer.Init(false, rsaKeyParams);
signer.BlockUpdate(plainBytes, 0, plainBytes.Length); signer.BlockUpdate(plainBytes, 0, plainBytes.Length);
return signer.VerifySignature(signBytes); return signer.VerifySignature(signBytes);
} }
private static byte[] EncryptWithECB(RsaKeyParameters rsaKeyParams, byte[] plainBytes) private static byte[] EncryptWithECB(RsaKeyParameters rsaKeyParams, byte[] plainBytes, string paddingAlgorithm)
{ {
IBufferedCipher cipher = CipherUtilities.GetCipher(RSA_CIPHER_ALG); IBufferedCipher cipher = CipherUtilities.GetCipher($"{RSA_CIPHER_ALGORITHM_ECB}/{paddingAlgorithm}");
cipher.Init(true, rsaKeyParams); cipher.Init(true, rsaKeyParams);
return cipher.DoFinal(plainBytes); return cipher.DoFinal(plainBytes);
} }
private static byte[] DecryptWithECB(RsaKeyParameters rsaKeyParams, byte[] cipherBytes) private static byte[] DecryptWithECB(RsaKeyParameters rsaKeyParams, byte[] cipherBytes, string paddingAlgorithm)
{ {
IBufferedCipher cipher = CipherUtilities.GetCipher(RSA_CIPHER_ALG); IBufferedCipher cipher = CipherUtilities.GetCipher($"{RSA_CIPHER_ALGORITHM_ECB}/{paddingAlgorithm}");
cipher.Init(false, rsaKeyParams); cipher.Init(false, rsaKeyParams);
return cipher.DoFinal(cipherBytes); return cipher.DoFinal(cipherBytes);
} }