Inheritance: KeyData
Ejemplo n.º 1
0
        static void Main(string[] args) {

            var TagDictionary = Constants.MakeDictionary(Constants.JoseTags);

            var ExamplePayloadBytes = UTF8Encoding.UTF8.GetBytes(ExamplePayload);
            var ExampleHeaderBytes = UTF8Encoding.UTF8.GetBytes(ExampleHeader);

            var Key = new Key(new JSONReader(ExampleKey));
            var Header = new Header(new JSONReader(ExampleHeader));

            //var Signed = Sign(ExampleHeaderBytes, ExamplePayloadBytes, Key);

            // Sign and serialize using JSON
            var JWS = new JoseWebSignature(Header, ExamplePayload);

            Sign(JWS, Key, ExampleHeader);
            byte[] JWSinJSON = JWS.ToJson();
            Console.WriteLine("In JSON:   {0} bytes", JWSinJSON.Length);
            Console.WriteLine("    Protected {0} / Payload {1} / Signature {2}",
                JWS.Protected.Length, JWS.Payload.Length, JWS.Signature.Length);



            // Sign and serialize using JSON-B
            SignB(JWS, Key);
            byte[] JWSinJSONB = JWS.ToJsonB();
            Console.WriteLine("In JSON-B: {0} bytes", JWSinJSONB.Length);
            Console.WriteLine("    Protected {0} / Payload {1} / Signature {2}",
                JWS.Protected.Length, JWS.Payload.Length, JWS.Signature.Length);

            // Sign and serialize using JSON-C
            SignC(JWS, Key, TagDictionary);
            byte[] JWSinJSONC = JWS.ToJsonC(TagDictionary);

            Console.WriteLine("In JSON-C: {0} bytes", JWSinJSONC.Length);
            Console.WriteLine("    Protected {0} / Payload {1} / Signature {2}",
                JWS.Protected.Length, JWS.Payload.Length, JWS.Signature.Length);

            Dump("JSON-B", JWSinJSONB);
            Dump("JSON-C", JWSinJSONC);

            }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a new encryption context and encrypt the data under the 
        /// generated content key and IV.
        /// </summary>
        /// <param name="Data">The data to encrypt</param>
        /// <param name="Encryptor">The encryption provider to use.</param>
        public void Encrypt(byte[] Data, CryptoProviderEncryption Encryptor) {
            //Trace.WriteHex("Encryption Key", Encryptor.Key);


            CryptoData = Encryptor.Encrypt(Data);
            var Preheader = new Header(Encryptor);
            Protected = Preheader.GetBytes(false); // get the data bytes untagged
            IV = CryptoData.IV;
            JTag = CryptoData.Integrity;
            CipherText = CryptoData.Data;
            }
Ejemplo n.º 3
0
 /// <summary>
 /// Encrypt to the specified key of the specified profile.
 /// </summary>
 /// <param name="KeyPair">KeyPair for the recipient.</param>
 public Recipient(KeyPair KeyPair) {
     Header = new Header();
     Header.kid = KeyPair.UDF;
     }
Ejemplo n.º 4
0
        /// <summary>
        /// Having read a tag, process the corresponding value data.
        /// </summary>
        /// <param name="JSONReader"></param>
        /// <param name="Tag"></param>
		public override void DeserializeToken (JSONReader JSONReader, string Tag) {
			
			switch (Tag) {
				case "header" : {
					// An untagged structure
					Header = new Header (JSONReader);
 
					break;
					}
				case "payload" : {
					Payload = JSONReader.ReadBinary ();
					break;
					}
				case "signature" : {
					Signature = JSONReader.ReadBinary ();
					break;
					}
				default : {
					break;
					}
				}
			// check up that all the required elements are present
			}
Ejemplo n.º 5
0
		/// <summary>
        /// Construct an instance from the specified tagged JSONReader stream.
        /// </summary>
        public static void Deserialize(JSONReader JSONReader, out JSONObject Out) {
	
			JSONReader.StartObject ();
            if (JSONReader.EOR) {
                Out = null;
                return;
                }

			string token = JSONReader.ReadToken ();
			Out = null;

			switch (token) {

				case "JoseWebSignature" : {
					var Result = new JoseWebSignature ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "JoseWebEncryption" : {
					var Result = new JoseWebEncryption ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "Signed" : {
					var Result = new Signed ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "Encrypted" : {
					var Result = new Encrypted ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "KeyData" : {
					var Result = new KeyData ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "Header" : {
					var Result = new Header ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "Key" : {
					var Result = new Key ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "Recipient" : {
					var Result = new Recipient ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "PublicKeyRSA" : {
					var Result = new PublicKeyRSA ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


				case "PrivateKeyRSA" : {
					var Result = new PrivateKeyRSA ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}

				default : {
					throw new Exception ("Not supported");
					}
				}	
			JSONReader.EndObject ();
            }
Ejemplo n.º 6
0
        /// <summary>
        /// Having read a tag, process the corresponding value data.
        /// </summary>
        /// <param name="JSONReader"></param>
        /// <param name="Tag"></param>
		public override void DeserializeToken (JSONReader JSONReader, string Tag) {
			
			switch (Tag) {
				case "Header" : {
					// An untagged structure
					Header = new Header (JSONReader);
 
					break;
					}
				case "encrypted_key" : {
					EncryptedKey = JSONReader.ReadBinary ();
					break;
					}
				default : {
					break;
					}
				}
			// check up that all the required elements are present
			}
Ejemplo n.º 7
0
        /// <summary>
        /// Deserialize a tagged stream
        /// </summary>
        /// <param name="JSONReader"></param>
        public static new Header  FromTagged (JSONReader JSONReader) {
			Header Out = null;

			JSONReader.StartObject ();
            if (JSONReader.EOR) {
                return null;
                }

			string token = JSONReader.ReadToken ();

			switch (token) {

				case "Header" : {
					var Result = new Header ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}

				default : {
					//Ignore the unknown data
                    //throw new Exception ("Not supported");
                    break;
					}
				}
			JSONReader.EndObject ();

			return Out;
			}