Example #1
0
        /// <summary>
        ///     Decrypt by Rfc 2898 DeriveBytes - PBKDF2
        /// </summary>
        /// <param name="value"></param>
        /// <param name="key">  </param>
        /// <returns></returns>
        public static string Decrypt(string value, string key)
        {
            byte[] cipherBytes = Safe64Encoding.DecodeBytes(value);

            using (var encrypt = Aes.Create())
            {
                var pdb = new Rfc2898DeriveBytes(key,
                                                 new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });

                encrypt.Key = pdb.GetBytes(32);

                encrypt.IV = pdb.GetBytes(16);

                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, encrypt.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                    }

                    value = Encoding.ASCII.GetString(ms.ToArray());
                }
            }

            return(value);
        }
        public void TestSafe64StreamRead()
        {
            string encoded = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";

            byte[] data = Safe64Encoding.DecodeBytes(encoded);
            byte[] test;
            using (Stream io = new Safe64Stream(new MemoryStream(Encoding.ASCII.GetBytes(encoded)), CryptoStreamMode.Read))
                test = IOStream.Read(io, data.Length);

            Assert.AreEqual(data, test);
        }
        public void TestSafe64EncodingAllChars()
        {
            //char count must by multiple of 4 for the compare to work
            string encoded = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";

            byte[] data = Safe64Encoding.DecodeBytes(encoded);
            Assert.AreEqual(encoded, Safe64Encoding.EncodeBytes(data));
            data = AsciiEncoder.DecodeBytes(encoded);
            Assert.AreEqual(encoded, AsciiEncoder.EncodeBytes(data));
            data = AsciiEncoder.DecodeBytes(Encoding.ASCII.GetBytes(encoded));
            Assert.AreEqual(encoded, AsciiEncoder.EncodeBytes(data));
        }
        public void TestSafe64EncodingLargeArray()
        {
            Random rand = new Random();

            byte[] data = new byte[0x400000];

            rand.NextBytes(data);
            string testAsc = Safe64Encoding.EncodeBytes(data);

            Assert.AreEqual(0, BinaryComparer.Compare(data, Safe64Encoding.DecodeBytes(testAsc)));
            testAsc = AsciiEncoder.EncodeBytes(data);
            Assert.AreEqual(0, BinaryComparer.Compare(data, AsciiEncoder.DecodeBytes(testAsc)));
        }
Example #5
0
        public SitePublisher(string storagePath, string site)
        {
            _storagePath = storagePath;
            _siteUri     = new Uri(site, UriKind.Absolute);
            _content     = new ContentStorage(storagePath, true);

            _keyfile = Path.Combine(storagePath, "client-publishing.key");
            if (File.Exists(_keyfile))
            {
                _rsaKeyPair = new RSAKeyPair(_keyfile, true);
                // we publish on the hash of both client and server keys so that if the handler is invoked there is already
                // a high-probability that the keyset will match.
                _publishUri = "/api/publish/" + Safe64Encoding.EncodeBytes(_rsaKeyPair.KeyPairHash.ToArray()) + "/";
            }
        }
        public void TestSafe64StreamWrite()
        {
            byte[] data = new byte[222];
            new Random().NextBytes(data);
            using (Stream mem = new MemoryStream())
            {
                using (Stream io = new Safe64Stream(new NonClosingStream(mem), CryptoStreamMode.Write))
                    io.Write(data, 0, data.Length);


                Assert.AreEqual((long)Math.Ceiling((data.Length * 8) / 6d), mem.Position);
                mem.Position = 0;
                string test = new StreamReader(mem).ReadToEnd();
                Assert.AreEqual(Safe64Encoding.EncodeBytes(data), test);
            }
        }
 public bool IsMatch(string method, string rawUrl)
 {
     if (method == "POST" && rawUrl.StartsWith(PathPrefix, StringComparison.Ordinal))
     {
         if (_publishUri == null)
         {
             try
             { _publishUri = PathPrefix + Safe64Encoding.EncodeBytes(_content.KeyPair.KeyPairHash.ToArray()) + "/"; }
             catch (Exception e)
             { _publishUri = String.Empty; Log.Error(e); /* no keys? */ }
         }
         return(StringComparer.Ordinal.Equals(rawUrl, PathSetPassword) ||
                StringComparer.Ordinal.Equals(rawUrl, _publishUri));
     }
     return(false);
 }
        void TestEncoderAgainstBase64(int repeat, int size)
        {
            Random rand = new Random();

            byte[] data = new byte[size];

            while (repeat-- > 0)
            {
                rand.NextBytes(data);
                string testB64 = Convert.ToBase64String(data);
                string testAsc = Safe64Encoding.EncodeBytes(data);

                Assert.AreEqual(testB64.Replace('+', '-').Replace('/', '_').Replace("=", ""), testAsc);
                Assert.AreEqual(0, BinaryComparer.Compare(data, Safe64Encoding.DecodeBytes(testAsc)));
                Assert.AreEqual(0, BinaryComparer.Compare(data, AsciiEncoder.DecodeBytes(testAsc)));
            }
        }
 public void TestBadInputCharacter()
 {
     byte[] trash = new byte[] { (byte)'a', (byte)'b', (byte)'c', (byte)'\0' };
     Safe64Encoding.DecodeBytes(trash);
     Assert.Fail();
 }