using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class RrsaSignatureString {
public Header header {get; set;}
public string encrypted_key {get; set;}
public Recipients[] recipients {get; set;}
[JsonProperty(PropertyName = "protected")]
public string _protected {get; set;}
public string ciphertext {get; set;}
public string iv {get; set;}
public string tag {get; set;}
public string isPaymentIBFT = "N";
public BankCodeReq body {get; set;}
BankCodeReq obj = new BankCodeReq();
public string appAcct {get; set;}
public string benAcct {get; set;}
public string benAcctName {get; set;}
public string benbankCode {get; set;}
public string benbankName {get; set;}
public string tranType {get; set;}
public string amount {get; set;}
public string curr {get; set;}
public string charge {get; set;}
public string remark {get; set;}
public string effdate {get; set;}
this.appAcct = "1600000848";
this.benAcct = "2612589163";
this.benAcctName = "VSYUPH VJO VJYA MOPJ";
this.benbankCode = "01202020";
this.benbankName = "NHTMCP DAU TU VA PT XN";
this.remark = "NGUYEN VAN A Chuyen Tien";
this.effdate = "20231231";
public static void Main() {
BodyInput obj = new BodyInput();
String payloadRequest = JsonConvert.SerializeObject(obj);
String encryptData = doEncryptJWE(payloadRequest);
String X_JWS_SIGNATURE = doSignatureJWS(encryptData);
private static byte[] hexStringToBytes(String s) {
byte[] ans = new byte[s.Length / 2];
for (int i = 0; i < ans.Length; i++) {
int val = Convert.ToInt32(s.Substring(index, 2),16);
public static String doEncryptJWE(String payload) {
byte[] key = hexStringToBytes("746163353431386f6b6d6e626c6f7071746163353431386f6b6d6e626c6f7033");
string jwe = Jose.JWT.Encode(payload, key, JweAlgorithm.A256KW, JweEncryption.A128GCM);
string[] split = jwe.Split(".");
string protected_ = split[0];
string encryptedKey = split[1];
string ciphertext = split[3];
Recipients recipient = new Recipients();
recipient.header = new Header();
recipient.encrypted_key = encryptedKey;
Recipients[] recipients = new Recipients[1];
recipients[0] = recipient;
JweObj jwePayload = new JweObj();
jwePayload.recipients = recipients;
jwePayload._protected = protected_;
jwePayload.ciphertext = ciphertext;
String payloadRequest = JsonConvert.SerializeObject(jwePayload);
Console.WriteLine(payloadRequest);
public static String doSignatureJWS(String payloadRequest) {
RSA rsaAlg = RSA.Create();
byte[] privateKeyByte = getRsaPrivateKeyEncodedFromPem(loadRsaPrivateKeyPem());
rsaAlg.ImportPkcs8PrivateKey(privateKeyByte, out _out);
string TXT_DATA = Jose.JWT.Encode(payloadRequest, rsaAlg, Jose.JwsAlgorithm.RS256);
char[] delimiterChars = {'.'};
string[] words = TXT_DATA.Split(delimiterChars);
string X_JWS_SIGNATURE = words[0] + ".." + words[2];
Console.WriteLine(X_JWS_SIGNATURE);
static string Base64Encoding(byte[] input) {
return Convert.ToBase64String(input);
static byte[] Base64Decoding(String input) {
return Convert.FromBase64String(input);
private static byte[] getRsaPrivateKeyEncodedFromPem(string rsaPrivateKeyPem) {
string rsaPrivateKeyHeaderPem = "-----BEGIN PRIVATE KEY-----\n";
string rsaPrivateKeyFooterPem = "-----END PRIVATE KEY-----";
string rsaPrivateKeyDataPem = rsaPrivateKeyPem.Replace(rsaPrivateKeyHeaderPem, "").Replace(rsaPrivateKeyFooterPem, "").Replace("\n", "");
return Base64Decoding(rsaPrivateKeyDataPem);
private static string loadRsaPrivateKeyPem() {
return "-----BEGIN PRIVATE KEY-----\n" +
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDqX3WWr2Cte2uC\n" +
"u1uK+i4hZ/G89Ar+XCHYiEgC/MB8xQqCDfMdaA1kKGRoFm4ClhrI1rPy5TFH9Fxg\n" +
"JhMQKYnYmSfG1LTTHNVN2/FHzRGZteRy+EYoy5ldt5/5AdxQVGexspUYazv5oMkT\n" +
"UnbvvA7iwJJg2UrgoWJnVQnIBRc0WJ5PBJS8OzphfvGYQ9OUB/Abf7Uv+WncAfdR\n" +
"D+qz9U/1i7InJ/zbVvs6GdObAJUFjEruUGc62TJC3Ef01klulk9gK5wlGDLNS5Cm\n" +
"bnaswWGe1TzVHT3bo8oSBjIVnazRzlVZ4w8jJ+VQdl0DzjnGHwvwmD3JTi6jy05E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