Пример #1
0
        /// <summary>
        /// Generate new Keys from seed
        /// </summary>
        /// <param name="seed"></param>
        /// <returns></returns>
        public static Keys Generate(byte[] seed)
        {
            if (seed == null)
            {
                seed = PrivateBox.RandomBytes(32);
            }

            Ed25519 ed25519 = new Ed25519();

            ed25519.FromSeed(seed);
            var secretKey = ed25519.GetPrivateKey();
            var publicKey = ed25519.GetPublicKey();

            var _public  = Convert.ToBase64String(publicKey) + ".ed25519";
            var _private = Convert.ToBase64String(secretKey) + ".ed25519";

            var keys = new Keys
            {
                Curve   = "ed25519",
                Public  = _public,
                Private = _private,
                ID      = "@" + _public,
            };

            return(keys);
        }
Пример #2
0
        public void TestSeededKeys()
        {
            var k1 = Keys.Generate(PrivateBox.RandomBytes(32));
            var k2 = Keys.Generate(PrivateBox.RandomBytes(32));

            Assert.IsTrue(k1 != k2);
        }
Пример #3
0
        public void Bench(int max, int N)
        {
            var ctxt = Create(max, max, alice.PublicKey);

            System.Diagnostics.Trace.WriteLine("max: " + max);             //number of recipients
            //length of cyphertext, ratio of cyphertext to plaintext length
            System.Diagnostics.Trace.WriteLine("length " + ctxt.Length + " " + ctxt.Length / content.Length);
            var start = DateTime.Now;

            for (var i = 0; i < N; i++)
            {
                PrivateBox.Decrypt(ctxt, alice.PrivateKey, max);
            }
            var hit = DateTime.Now - start;

            System.Diagnostics.Trace.WriteLine("hit " + hit / N);             //ms to decrypt a message that was for us

            start = DateTime.Now;
            for (var i = 0; i < N; i++)
            {
                PrivateBox.Decrypt(ctxt, bob.PrivateKey, max);
            }
            var miss = DateTime.Now - start;

            System.Diagnostics.Trace.WriteLine("miss " + miss / N);             //ms to fail to decrypt a message not for us

            System.Diagnostics.Trace.WriteLine("ratio " + miss / hit);          //how much miss is bigger than hit.
        }
Пример #4
0
        public byte[] Create(int n, int max, byte[] pk)
        {
            var a = new byte[n][];

            a[0] = pk;
            for (int i = 1; i < n; i++)
            {
                a[i] = PublicKeyBox.GenerateKeyPair().PublicKey;
            }

            return(PrivateBox.Encrypt(content, a, max));
        }
Пример #5
0
        public static string Box(object msg, byte[][] recipientsPublicKey)
        {
            var _msg = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));
            var _box = new List <byte[]>();

            foreach (var recPubKey in recipientsPublicKey)
            {
                _box.Add(Sodium.PublicKeyAuth.ConvertEd25519PublicKeyToCurve25519PublicKey(recPubKey));
            }

            var boxed = PrivateBox.Encrypt(_msg, _box);

            return(Convert.ToBase64String(boxed) + ".box");
        }
Пример #6
0
        public void TestSimple()
        {
            var msg = "hello there!";

            var pubKeys = new byte[][] { alice.PublicKey, bob.PublicKey };
            var prvKeys = new byte[][] { alice.PrivateKey, bob.PrivateKey };

            var ctxt = PrivateBox.Encrypt(msg, pubKeys);

            foreach (var sk in prvKeys)
            {
                var txt = System.Text.Encoding.UTF8.GetString(PrivateBox.Decrypt(ctxt, sk));

                Assert.AreNotSame(msg, txt);
            }
        }
Пример #7
0
        public void encryptDecryptTo(int n)
        {
            var msg  = PrivateBox.RandomBytes(1024);
            var keys = new System.Collections.Generic.List <KeyPair>();

            for (int i = 0; i < keys.Count; i++)
            {
                keys[i] = PublicKeyBox.GenerateKeyPair();
            }

            var ctxt = PrivateBox.Multibox(msg, keys.Select(x => x.PublicKey).ToArray(), n);

            // a recipient key may open the message.
            foreach (var key in keys)
            {
                Assert.AreEqual(PrivateBox.MultiboxOpen(ctxt, key.PrivateKey, n), msg);
            }
        }
Пример #8
0
        public static object UnboxBody(byte[] boxed, Keys key)
        {
            if (key == null)
            {
                return(null);
            }
            //var _boxed = Utilities.ToByteArray(boxed);
            var _key = Utilities.ToByteArray(key.Private);
            var msg  = PrivateBox.MultiboxOpenBody(boxed, _key);

            try
            {
                return(JsonConvert.DeserializeObject(Encoding.UTF8.GetString(msg)));
            }
            catch
            {
                return(null);
            }
        }
Пример #9
0
        public void TestErrorsWhenMaxIsMoreThan255OrLessThan1()
        {
            var msg = "hello there!";

            var ctxt = PrivateBox.Multibox(msg, new byte[][] { alice.PublicKey, bob.PublicKey });
            var pk   = alice.PublicKey;
            var sk   = alice.PrivateKey;

            Assert.Catch(new TestDelegate(delegate
            {
                PrivateBox.Multibox(msg, new byte[][] { pk, pk, pk, pk }, -1);
            }));

            Assert.Catch(new TestDelegate(delegate
            {
                PrivateBox.MultiboxOpen(ctxt, sk, 256);
            }));

            Assert.Pass();
        }
Пример #10
0
        public static Newtonsoft.Json.Linq.JObject Unbox(string boxed, byte[] privateKey)
        {
            if (privateKey == null)
            {
                return(null);
            }
            var _boxed = Utilities.ToByteArray(boxed);

            var sk = Sodium.PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(privateKey);

            try
            {
                var msg = PrivateBox.MultiboxOpen(_boxed, sk);
                return(JsonConvert.DeserializeObject <Newtonsoft.Json.Linq.JObject>(Encoding.UTF8.GetString(msg)));
            }
            catch
            {
                return(null);
            }
        }
Пример #11
0
        public void TestErrorsWhenTooManyRecipients()
        {
            var msg = "hello there!";

            var pubKeys = new byte[][]
            {
                alice.PublicKey, alice.PublicKey,
                alice.PublicKey, alice.PublicKey,
                alice.PublicKey, alice.PublicKey,
                alice.PublicKey, alice.PublicKey,
                alice.PublicKey, alice.PublicKey,
                alice.PublicKey, alice.PublicKey,
                alice.PublicKey, alice.PublicKey,
                alice.PublicKey, alice.PublicKey,
            };

            Assert.Catch(new TestDelegate(delegate
            {
                PrivateBox.Multibox(msg, pubKeys);
            }));
        }