public void noAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.SetContent(strContent); msg.Encrypt(rgbKey128); }
public void EncryptCompressed() { string msg = "Ths is some content"; EncryptMessage encryptMessage = new EncryptMessage(); encryptMessage.SetContent(msg); JWK encryptionKey = JWK.GenerateKey("A128GCM"); // encryptMessage.AddAttribute(HeaderKeys.EncryptionAlgorithm, CBORObject.FromObject(EncryptionAlgorithm), Attributes.PROTECTED); Recipient recipient = new Recipient(encryptionKey); encryptMessage.AddRecipient(recipient); // recipient.ClearUnprotected(); if (recipient.RecipientType == RecipientType.Direct && encryptionKey.ContainsName("alg")) { encryptMessage.AddAttribute("enc", encryptionKey.AsString("alg"), Attributes.PROTECTED); } else { encryptMessage.AddAttribute("enc", "A128GCM", Attributes.PROTECTED); } msg = encryptMessage.EncodeCompressed(); }
public void TestDecodeUnknown() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); // msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.PROTECTED); msg.SetContent(rgbContent); CBORObject obj = CBORObject.NewMap(); obj.Add("kty", "oct"); obj.Add("k", Encoding.UTF8.GetString(Base64.Encode(rgbKey128))); JWK key = new JWK(obj); Recipient recipient = new Recipient(key, "dir"); msg.AddRecipient(recipient); string rgbMsg = msg.Encode(); JoseException e = Assert.ThrowsException <JoseException>(() => msg = (EncryptMessage)Message.DecodeFromString(rgbMsg)); Assert.AreEqual(e.Message, ("Message was not tagged and no default tagging option given")); }
public void incorrectKeySize() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true); msg.SetContent(strContent); msg.Encrypt(rgbKey256); }
public void unsupportedAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.HMAC_SHA_256, true); msg.SetContent(strContent); msg.Encrypt(rgbKey128); }
public void unknownAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), true); msg.SetContent(strContent); msg.Encrypt(rgbKey128); }
public void nullKey() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.SetContent(strContent); msg.Encrypt(); }
public void incorrectIV() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV128), false); msg.SetContent(strContent); msg.Encrypt(rgbKey128); }
public void testSetContent_byteArr() { byte[] rgbData = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; Message instance = new EncryptMessage(); instance.SetContent(rgbData); byte[] result = instance.GetContent(); Assert.AreEqual(result, (rgbData)); }
public void noAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.SetContent(strContent); Recipient r = new Recipient(key128, AlgorithmValues.Direct); msg.AddRecipient(r); msg.Encrypt(); }
public void unknownAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attributes.PROTECTED); msg.SetContent(strContent); Recipient r = new Recipient(key128, AlgorithmValues.Direct); msg.AddRecipient(r); msg.Encrypt(); }
public void unsupportedAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.HMAC_SHA_256, Attributes.PROTECTED); msg.SetContent(strContent); Recipient r = new Recipient(key128, AlgorithmValues.Direct); msg.AddRecipient(r); msg.Encrypt(); }
public void EncryptNoRecipients() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.SetContent(rgbContent); CoseException e = Assert.ThrowsException <CoseException>(() => msg.Encrypt()); Assert.AreEqual(e.Message, ("No recipients supplied")); }
public void testSetContent_String() { String strData = "12345678"; byte[] rgbData = new byte[] { 49, 50, 51, 52, 53, 54, 55, 56 }; Message instance = new EncryptMessage(); instance.SetContent(strData); byte[] result = instance.GetContent(); Assert.AreEqual(result, (rgbData)); }
public void testHasContent() { Message instance = new EncryptMessage(); Boolean expResult = false; Boolean result = instance.HasContent(); Assert.AreEqual(expResult, (expResult)); instance.SetContent(new byte[10]); result = instance.HasContent(); Assert.AreEqual(result, (true)); }
public void incorrectIV() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV128), Attributes.UNPROTECTED); msg.SetContent(strContent); Recipient r = new Recipient(key128, AlgorithmValues.Direct); msg.AddRecipient(r); msg.Encrypt(); }
public void EncryptNoAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddRecipient(recipient128); msg.SetContent(rgbContent); CoseException e = Assert.ThrowsException <CoseException>(() => msg.Encrypt()); Assert.AreEqual(e.Message, ("No Algorithm Specified")); }
public void EncryptUnknownAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddRecipient(recipient128); msg.AddAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attributes.PROTECTED); msg.SetContent(rgbContent); CoseException e = Assert.ThrowsException <CoseException>(() => msg.Encrypt()); Assert.AreEqual(e.Message, ("Unknown Algorithm Specified")); }
public void EncryptUnsupportedAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddRecipient(recipient128); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.HMAC_SHA_256, Attributes.PROTECTED); msg.SetContent(rgbContent); CoseException e = Assert.ThrowsException <CoseException>(() => msg.Encrypt()); Assert.AreEqual(e.Message, ("Incorrect key size" /*"Unsupported Algorithm Specified"*/)); }
public void encryptNoEmitContent() { EncryptMessage msg = new EncryptMessage(true, false); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false); msg.SetContent(strContent); msg.Encrypt(rgbKey128); CBORObject cn = msg.EncodeToCBORObject(); Assert.IsTrue(cn[2].IsNull); }
public void testDecodeFromBytes_byteArr_MessageTag() { EncryptMessage msg = new EncryptMessage(true, false); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.PROTECTED); msg.SetContent(rgbContent); msg.Encrypt(rgbKey128); byte[] rgbMsg = msg.EncodeToBytes(); msg = (EncryptMessage)Message.DecodeFromBytes(rgbMsg); Assert.AreEqual(false, (msg.HasContent())); }
public void EncryptIncorrectIV() { EncryptMessage msg = new EncryptMessage(); msg.AddRecipient(recipient128); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV128), Attributes.UNPROTECTED); msg.SetContent(rgbContent); CoseException e = Assert.ThrowsException <CoseException>(() => msg.Encrypt()); Assert.AreEqual(e.Message, ("IV size is incorrect.")); }
public void roundTrip() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false); msg.SetContent(strContent); msg.Encrypt(rgbKey128); byte[] rgbMsg = msg.EncodeToBytes(); msg = (EncryptMessage)Message.DecodeFromBytes(rgbMsg); msg.Decrypt(rgbKey128); Assert.AreEqual <string>(msg.GetContentAsString(), strContent); }
public void encryptNoEmitContent() { EncryptMessage msg = new EncryptMessage(true, false); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.UNPROTECTED); msg.SetContent(strContent); Recipient r = new Recipient(key128, AlgorithmValues.Direct); msg.AddRecipient(r); msg.Encrypt(); CBORObject cn = msg.EncodeToCBORObject(); Assert.IsTrue(cn[2].IsNull); }
public void nullKeyForDecrypt() { EncryptMessage msg = new EncryptMessage(true, true); // thrown.expect(CoseException.class); // thrown.expectMessage("No Encrypted Content Specified"); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false); msg.SetContent(strContent); msg.Encrypt(rgbKey128); byte[] rgb = msg.EncodeToBytes(); msg = (EncryptMessage)Message.DecodeFromBytes(rgb); msg.Decrypt(null); }
public void roundTripDetached() { EncryptMessage msg = new EncryptMessage(true, false); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false); msg.SetContent(strContent); msg.Encrypt(rgbKey128); byte[] content = msg.GetEncryptedContent(); byte[] rgb = msg.EncodeToBytes(); msg = (EncryptMessage)Message.DecodeFromBytes(rgb); msg.SetEncryptedContent(content); msg.Decrypt(rgbKey128); }
static void BuildCompact(JSON control, KeySet keys) { // Encrypted or Signed? if (control.ContainsKey("signing")) { JOSE.SignMessage sign = new JOSE.SignMessage(); JOSE.Signer signer = new JOSE.Signer(keys[0]); sign.SetContent(control["input"]["payload"].AsString()); sign.AddSigner(signer); JSON xx = control["signing"]["protected"]; foreach (string key in xx.Keys) { signer.AddProtected(key, xx[key]); } string output = sign.EncodeCompact(); Message msg = Message.DecodeFromString(output); CheckMessage(msg, keys[0], control["input"]); } else if (control.ContainsKey("encrypting_key")) { JOSE.EncryptMessage enc = new EncryptMessage(); JSON xx = control["encrypting_content"]["protected"]; foreach (string key in xx.Keys) { enc.AddProtected(key, xx[key]); } JOSE.Recipient recip = new Recipient(keys[0], control["input"]["alg"].AsString(), enc); enc.AddRecipient(recip); enc.SetContent(control["input"]["plaintext"].AsString()); string output = enc.EncodeCompact(); Message msg = Message.DecodeFromString(output); CheckMessage(msg, keys[0], control["input"]); } }
static void BuildCompact(CBORObject control, JwkSet keys) { // Encrypted or Signed? if (control.ContainsKey("signing")) { SignMessage sign = new SignMessage(); Signer signer = new Signer(keys[0]); sign.SetContent(control["input"]["payload"].AsString()); sign.AddSigner(signer); CBORObject xx = control["signing"]["protected"]; foreach (CBORObject key in xx.Keys) { signer.AddAttribute(key, xx[key], Attributes.PROTECTED); } string output = sign.EncodeCompressed(); Message msg = Message.DecodeFromString(output); CheckMessage(msg, keys[0], control["input"]); } else if (control.ContainsKey("encrypting_key")) { EncryptMessage enc = new EncryptMessage(); CBORObject xx = control["encrypting_content"]["protected"]; foreach (CBORObject key in xx.Keys) { enc.AddAttribute(key, xx[key], Attributes.PROTECTED); } Recipient recip = new Recipient(keys[0], control["input"]["alg"].AsString(), enc); enc.AddRecipient(recip); enc.SetContent(control["input"]["plaintext"].AsString()); string output = enc.EncodeCompressed(); Message msg = Message.DecodeFromString(output); CheckMessage(msg, keys[0], control["input"]); } }
public void roundTrip() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.UNPROTECTED); msg.SetContent(strContent); Recipient r = new Recipient(key128, AlgorithmValues.Direct); msg.AddRecipient(r); msg.Encrypt(); byte[] rgbMsg = msg.EncodeToBytes(); msg = (EncryptMessage)Message.DecodeFromBytes(rgbMsg); r = msg.RecipientList[0]; r.SetKey(key128); msg.Decrypt(r); Assert.AreEqual <string>(msg.GetContentAsString(), strContent); }
public void roundTripDetached() { EncryptMessage msg = new EncryptMessage(true, false); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.UNPROTECTED); msg.SetContent(strContent); Recipient r = new Recipient(key128, AlgorithmValues.Direct); msg.AddRecipient(r); msg.Encrypt(); byte[] content = msg.GetEncryptedContent(); byte[] rgb = msg.EncodeToBytes(); msg = (EncryptMessage)Message.DecodeFromBytes(rgb); msg.SetEncryptedContent(content); r = msg.RecipientList[0]; r.SetKey(key128); msg.Decrypt(r); }