Esempio n. 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));
        }
Esempio n. 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);
        }
Esempio n. 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())));
        }
Esempio n. 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));
        }