Example #1
0
        public void CipherTest()
        {
            Random rnd = new Random();

            byte[] message = new byte[511];
            rnd.NextBytes(message);
            BFCText encypted = BFCipher.encrypt(pKey, message, rnd);

            byte[] result = BFCipher.decrypt(encypted, key);
            Assert.AreEqual(Encoding.UTF8.GetString(message, 0, message.Length), Encoding.UTF8.GetString(result, 0, message.Length));
        }
Example #2
0
        public async Task <byte[]> DecipherMessage(string ct, SerializedPrivateKey sKey)
        {
            var decipherTask = Task <byte[]> .Factory.StartNew(() =>
            {
                BFUserPrivateKey key       = new BFUserPrivateKey(sKey);
                SerializedBFCText ciphered = (SerializedBFCText)JsonConvert.DeserializeObject(HttpUtility.UrlDecode(ct), typeof(SerializedBFCText));
                BFCText cText = new BFCText(ciphered);
                return(BFCipher.decrypt(cText, key));
            });

            await decipherTask;

            return((byte[])decipherTask.Result);
        }
Example #3
0
        public async Task <string> CipherMessage(byte[] message, string identity, SerializedPrivateKey sKey)
        {
            var cipherTask = Task <BFCText> .Factory.StartNew(() =>
            {
                BFUserPrivateKey key = new BFUserPrivateKey(sKey);
                BFUserPublicKey pKey = new BFUserPublicKey(identity, key.Param);
                return(BFCipher.encrypt(pKey, message, new Random()));
            });

            await   cipherTask;
            BFCText cipher = (BFCText)cipherTask.Result;

            return(HttpUtility.UrlEncode(JsonConvert.SerializeObject(cipher.Serialize())));
        }
Example #4
0
        public async Task <string> DecipherText(string ct, SerializedPrivateKey sKey)
        {
            var decipherTask = Task <byte[]> .Factory.StartNew(() =>
            {
                BFUserPrivateKey key       = new BFUserPrivateKey(sKey);
                SerializedBFCText ciphered = (SerializedBFCText)JsonConvert.DeserializeObject(ct, typeof(SerializedBFCText));
                BFCText cText = new BFCText(ciphered);
                return(BFCipher.decrypt(cText, key));
            });

            await decipherTask;

            byte[] bMessage = (byte[])decipherTask.Result;
            return(Encoding.UTF8.GetString(bMessage, 0, bMessage.Length));
        }
Example #5
0
        public void CipherTest()
        {
            KeyPair victor = BFCipher.Extract(master, "victor", new Random());
            String  msg    = "hola";

            byte[]  msgByte;
            byte[]  plainByte;
            String  plain;
            BFCText encrypted;

            msgByte   = Encoding.UTF8.GetBytes(msg);
            encrypted = BFCipher.encrypt((BFUserPublicKey)victor.Public, msgByte, new Random());
            plainByte = BFCipher.decrypt(encrypted, (BFUserPrivateKey)victor.Private);
            plain     = Encoding.UTF8.GetString(plainByte);
            Assert.AreEqual(msg, plain);
        }
Example #6
0
        public void CipherInReasonableTimeTest()
        {
            int times = 50;
            int size  = 511;

            long[] measures = new long[50];
            Random rnd      = new Random();

            System.Diagnostics.Stopwatch m_watch = new System.Diagnostics.Stopwatch();
            m_watch.Start();
            for (int i = 0; i < times; i++)
            {
                byte[] message = new byte[size];
                rnd.NextBytes(message);
                m_watch.Reset();
                m_watch.Start();
                BFCipher.encrypt(pKey, message, rnd);
                measures[i] = m_watch.ElapsedMilliseconds;
                m_watch.Stop();
            }
            string prompt = "Time spent to cipher " + times + " messages of " + size + " bytes:";

            System.Diagnostics.Debug.WriteLine("{0}  {1} msec", prompt, m_watch.ElapsedMilliseconds);
        }
Example #7
0
        public SerializedPrivateKey Private(string id)
        {
            KeyPair user = BFCipher.Extract(master, id, new Random());

            return(((BFUserPrivateKey)user.Private).Serialize());
        }
Example #8
0
        public BFUserPublicKey Public(string id)
        {
            BFUserPublicKey userKey = (BFUserPublicKey)BFCipher.ExtractPublic(master, id);

            return(userKey);
        }