JWE in C#

WillieBurgerICS
Regular Visitor

JWE in C#

Hi All,

 

I am working on a simple app to use the Visa Token Service Api's. The bit I am really batteling with is building a method were I can pass a string object and encode it as a JWE.

 

From the doc:

Required) BLOB field (URL safe base 64 encoded ) that represents the encrypted payload for payment instrument. (See Encrypted
Payment Instrument .)
JSON Web Encryption using shared secret made available at time of onboarding,
Encrypted Blob of all Payment instrument details.

There is mention of a Java SDK that has a method to create the JWE blob, I need a similar method but in C# to do this. 

 

The .net samples on github for Visa does not include an example of this 😞 

 

Any help on creating/implementing this method would be greatly appreciated. 

 

Thank you so much!

8 REPLIES 8
ricardo_visa
Community Scholar

Re: JWE in C#

Hi WillieBurgerICS, 

 

I am working to get this for you.

 

Thanks,

Ricardo 




Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.
WillieBurgerICS
Regular Visitor

Re: JWE in C#

Hi Ricardo,
Thank you 🙂
Please note that I have gone through all the samples on the GitHub repo including all the C# unit tests for the api's.
None of these API's include the JWE bit.
Kind Regards
Willie
WillieBurgerICS
Regular Visitor

Re: JWE in C#

Hi Riccardo,

 

From the documentation we got this far:

 

public static string JWEEncode(string message,string key, string secret)
{
HMACSHA256 hmacsha256 = new HMACSHA256();

var test = hmacsha256.ComputeHash(new ASCIIEncoding().GetBytes(secret));
var extraHeaders = new Dictionary<string, object>();
extraHeaders.Add("kid", key);

string result = Jose.JWT.Encode(message, test, JweAlgorithm.A256GCMKW, JweEncryption.A256CBC_HS512,extraHeaders: extraHeaders);

return result;
}

 

the Key and the secret are passed from the values in the devloper portal sandbo environment.

 

We are using the JOSE JWT library found here: 

https://github.com/dvsekhvalnov/jose-jwt

 

However we still get a result:

{"errorResponse":{"status":400,"message":"Input is invalid or inconsistent with the profile.","reason":"invalidParameter","details":[{"location":"clientAppID"}]}}

 

The unencrypted message we are passing is:

{"accountNumber": "4411059860000004",  "name": "BillEvans","cvv2":"123","expirationDate": {"month": "10","year": "2015"},"billingAddress": {    "line1": "801MetroCenterBlvd","line2": null,"city": "FosterCity","state": "CA","country": "US","postalCode": "94404"}}

 

The RAW request:

{
"clientAppID": "APP1",
"locale": "en_US",
"panSource": "MOBILEAPP",
"encPaymentInstrument": "eyJhbGciOiJBMjU2R0NNS1ciLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwia2lkIjoiSEc2MUM0UURMV1ZGU002TjFZQkgyMXJkZ1BqNVpzRTNHOTZPUFJ4REpLT1R6ejVJayIsIml2IjoieGI0eHEyR2hvaDlSbnNPVSIsInRhZyI6ImRVeHNBRDV2ZURuWVdhaXF5ZXpCNWcifQ.MaQVGt6rsucf58ma1kLsRSIPOHPwjdQM6YlhF6WYI8NEtajLXYwzzywvPMtvJB7ef5GIl1Jwy2wT4sMbCmKd0g._nWJK9944KY3kFdMT23IrA.yvc_JUX6leJxi91mgIAEpd2pGIuhXDoGNMYTUfhDFCxwqMbZwbSayOvWwk58ftCnB72HCK3lGkXmw2xkk32Iqbu4NpyLBQXA13969KpX4o2sq65anuHyX2Xg5oZ6EXz8hBzY7_XmnYD4-LYGdkU14dYZcRJ7glZohXP-BrNWhUlM-4n_SQgm1FQIgDjvOrNwSX5QlyWWLQXnO9jAhwrSHricgytDXzEnGa7Xn6Dt3hiEk1Ivpu69C9imXhs11QryLln-Z3ngguyQRpYjOlEv_x7Wx6OapfDRhgelloHu3mqHABQnz-VMgyvl11ZHTwQUVnyKZ42b-7giA97Jl-5cZY35H-pm4lmgDUvSnrxcJKQ5OzXESe1XgaYTDE2uysGU.E_R_1bxSep_yGxgfXAht-u4NWVAL0EJ21OEASUXQt5Y"
}

 

Any help will be greatly appreciated.

adamlee
Helper

Re: JWE in C#

can u share the solution for it please
SLi
Visa Developer Support Specialist
Visa Developer Support Specialist

Re: JWE in C#

Hi @adamlee,

 

Can you please share more details on what you are looking for? This post is from a few years ago, I believe we are not uploading code to GitHub anymore. Sample codes can be accessed from your project dashboard once you are logged into your Visa Developer account.    


Best,
Stacey

Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.
Matrixum
Occasional Visitor

Re: JWE in C#

Hello guys.
Here is my working solution in C #
I hope you find it useful.
Good luck.

Here link with formatted code with jwe and x-pay-token: https://pastebin.com/KYgPbkRX
I used this library: https://github.com/dvsekhvalnov/jose-jwt

Raw code: 

  1. /// <summary>
  2. ///github.com/dvsekhvalnov/jose-jwt
  3.         /// </summary>
  4.         /// <param name="jsonData">Raw data for encryption</param>
  5.         /// <param name="sharedSecret">EncryptionSharedSecret VISA</param>
  6.         /// <param name="apiKey">EncryptionApiKey VISA</param>
  7.         /// <returns></returns>
  8.         public static string JwtTokenEncryption(string jsonData, string sharedSecret, string apiKey)
  9.         {
  10.             using (SHA256 sha256Hash = SHA256.Create())
  11.             {
  12.                 var utc0 = new DateTime(1970, 1, 1, 3, 0, 0, 0, DateTimeKind.Utc);
  13.                 var iat = (int)DateTime.Now.Subtract(utc0).TotalSeconds;
  14.  
  15.                 var digest = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(sharedSecret));
  16.  
  17.                 var extraHeaders = new Dictionary<string, object>
  18.                 {
  19.                     {"kid",  apiKey},
  20.                     {"typ", "JOSE"},
  21.                     {"channelSecurityContext", "SHARED_SECRET"},
  22.                     {"iat", iat},
  23.                 };
  24.  
  25.                 string result = Jose.JWT.Encode(jsonData, digest, JweAlgorithm.A256GCMKW, JweEncryption.A256GCM, extraHeaders: extraHeaders);
  26.  
  27.                 return result;
  28.             }
  29.         }



  1. /// <summary>
  2.        ///github.com/dvsekhvalnov/jose-jwt
  3.         /// </summary>
  4.         /// <param name="token">Token from VISA</param>
  5.         /// <param name="sharedSecret">EncryptionSharedSecret VISA</param>
  6.         /// <returns></returns>
  7.         public static string JwtTokenDecryption(string token, string sharedSecret)
  8.         {
  9.             using (SHA256 sha256Hash = SHA256.Create())
  10.             {
  11.                 var digest = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(sharedSecret));
  12.                
  13.                 string result = Jose.JWT.Decode(token, digest, JweAlgorithm.A256GCMKW, JweEncryption.A256GCM);
  14.  
  15.                 return result;
  16.             }
  17.         }



Raveen
Helper

Re: JWE in C#

Hi Recardo,

I am also using same api, but face same issue as it is new for us, can you please post sample code in github.

 

Thank you

Re: JWE in C#

any news ? facing the same issue