· 4 years ago · Apr 08, 2021, 06:42 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Jose;
7
8using System.Buffers.Text;
9using Newtonsoft.Json;
10using Newtonsoft.Json.Linq;
11using Jose.keys;
12using System.Security.Cryptography;
13using System.Xml.Serialization;
14using System.Web.Script.Serialization;
15using WorkBenchC.Models;
16
17namespace WorkBenchC.Tools
18{
19 class CyberSourcev2
20 {
21 private JObject encryptObject = new JObject();
22
23 public string jwetoken = "";
24 public CyberSourcev2(String keyID)
25 {
26 // String keyBody = new String(Base64.DecodeFromUtf8(
27
28 //recape
29 //split the private key (check if its private or public) at the full-stops.
30 string keyIdSplit = keyID.Split('.')[1];
31
32 //convert the privat key from base 64 (encoding)
33 byte[] data = Convert.FromBase64String(keyIdSplit);
34
35 //convert the byte array decoded string to a string
36 string decodedString = Encoding.UTF8.GetString(data);
37
38 //serialise string into a Json (even though its still a string... so what does Serialize actually do?...)
39 var serializeAddressFormatted = JsonConvert.SerializeObject(decodedString);
40
41 //DeSerialise the string into a Jason Object or Jobject.
42 var routes_list2 = (JObject)JsonConvert.DeserializeObject(decodedString);
43
44 //Retrieve the value kid from jason property
45 var kid = routes_list2.Descendants()
46 .OfType<JProperty>()
47 .FirstOrDefault(x => x.Name == "kid")
48 ?.Value.ToString();
49
50 //Retrieve the value jwk from jason property (which is a object itself, a json i think)
51 var jwk = routes_list2.Descendants()
52 .OfType<JProperty>()
53 .FirstOrDefault(x => x.Name == "jwk")
54 ?.Value.ToString();
55
56 //convert from json property to string
57 // string = result2.ToString();
58 // string = JsonConvert.SerializeObject(result3).ToString();
59
60
61 //not sure what needs to go in payload... if naything in my case
62 Card card = new Card();
63 var cardJson = JsonConvert.SerializeObject(card);
64
65 string payload = cardJson.ToString();
66 //headers seems to be kid? (private id? i think)
67 var headers = new Dictionary<string, object> { { "kid", kid }, };
68
69 // byte[] secretKey =< secret key bytes here >;
70
71 //var parameters = GetRSAParameters(jwk);
72 // var rsaKey = RsaKey.New(parameters);
73 var js = new JavaScriptSerializer();
74 var jwk2 = js.Deserialize<IDictionary<string, string>>(jwk);
75 byte[] e = Base64Url.Decode(jwk2["e"]);
76 byte[] n = Base64Url.Decode(jwk2["n"]);
77
78 RSA key = RSA.Create();
79 RSAParameters keyParams = new RSAParameters();
80 keyParams.Exponent = e;
81 keyParams.Modulus = n;
82 keyParams.P = null;
83 keyParams.Q = null;
84
85 key.ImportParameters(keyParams);
86
87// string rsaJwkStr = rsaKey.GetJwk();
88 encryptObject.Add("context", keyID);
89 encryptObject.Add("index", 0);
90 encryptObject.Add("data", cardJson);
91 JWE test = new JWE();
92
93 //payload would be card object which is a json serialised and then cast to string
94 var jweToken = JWT.Encode(encryptObject.ToString(), key, JweAlgorithm.RSA_OAEP, JweEncryption.A256GCM, extraHeaders: headers);
95 jwetoken = jweToken;
96 //when you have created the card object and it appears working...
97 //youll want to setup the one from github in java and pass in the JWK value to see what it returns
98 //compare with your result.
99
100 Console.WriteLine();
101
102
103 Console.WriteLine("");
104 }
105
106 }
107}
108
109
110
111
112//kid = the kid references the public key that was used to encrypt the data
113//JWK = the key is the public key to which the JWE was encrypted