Ejemplo n.º 1
0
        public void SymmetricKey_Serialized_EncodeDecode()
        {
            //-------------------------------------------------------
            //  arrange
            //-------------------------------------------------------
            SymmetricKey originalKey = SymmetricKey.Create();
            var          plainText   = Guid.NewGuid().ToString();
            var          plainBytes  = Encoding.Unicode.GetBytes(plainText);

            //-------------------------------------------------------
            // act
            //-------------------------------------------------------
            var encoded1 = originalKey.Encode(plainBytes);

            var serializedKey   = Serialize.To.DataContract(originalKey);
            var deserializedKey = Serialize.From.DataContract <SymmetricKey>(serializedKey);

            var decoded1 = deserializedKey.Decode(encoded1);
            var result1  = CryptoUtils.FromBytes(decoded1, StringFormat.Unicode);

            //-------------------------------------------------------
            // assert
            //-------------------------------------------------------
            Assert.AreEqual <string>(plainText, result1);
        }
Ejemplo n.º 2
0
        public void SymmetricKey_Constructor_SpecifiedProviderSpecifiedKey_ComparePlainText()
        {
            //-------------------------------------------------------
            //  arrange
            //-------------------------------------------------------
            SymmetricKey key1       = SymmetricKey.Create(Crypto.EncodeModes.TripleDES);
            SymmetricKey key2       = SymmetricKey.Create(Crypto.EncodeModes.TripleDES, key1.KeyBytes, key1.IVBytes);
            var          plainText  = Guid.NewGuid().ToString();
            var          plainBytes = Encoding.Unicode.GetBytes(plainText);

            //-------------------------------------------------------
            // act
            //-------------------------------------------------------
            var encoded1 = key1.Encode(plainBytes);
            var encoded2 = key2.Encode(plainBytes);

            var decoded1 = key1.Decode(encoded2);
            var decoded2 = key2.Decode(encoded1);

            var result1 = CryptoUtils.FromBytes(decoded1, StringFormat.Base64);
            var result2 = CryptoUtils.FromBytes(decoded2, StringFormat.Base64);

            //-------------------------------------------------------
            // assert
            //-------------------------------------------------------
            Assert.AreEqual <string>(result1, result2);
        }
        /// <summary>
        /// Register an encryption key for use with a given database
        /// </summary>
        /// <param name="keyDataOrPassword">A password as a string or key data as a byte
        /// IEnumerable</param>
        /// <param name="dbName">The name of the database to use this key with</param>
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException">Thrown if invalid data
        /// is passed into keyDataOrPassword</exception>
        public void RegisterEncryptionKey(object keyDataOrPassword, string dbName)
        {
            var realKey = default(SymmetricKey);

            try {
                realKey = SymmetricKey.Create(keyDataOrPassword);
            } catch (InvalidOperationException) {
                throw new CouchbaseLiteException("Invalid object passed to SymmetricKey", StatusCode.BadParam);
            }

            Shared.SetValue("encryptionKey", "", dbName, realKey);
        }
        public void OneTimeSetUp()
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            Console.WriteLine("Generating keys for test, this might take a while...");
            _letmein = SymmetricKey.Create("letmein");
            Console.WriteLine("Keys completed (1/3)");
            _letmeout = SymmetricKey.Create("letmeout");
            Console.WriteLine("Keys completed (2/3)");
            _wrong = SymmetricKey.Create("wrong");
            Console.WriteLine("Keys completed (3/3)");
            sw.Stop();
            Console.WriteLine("Created three keys in {0}ms", sw.ElapsedMilliseconds);
        }
Ejemplo n.º 5
0
        public void SymmetricKey_DefaultConstructor()
        {
            //-------------------------------------------------------
            //  arrange
            //-------------------------------------------------------
            SymmetricKey key1       = SymmetricKey.Create();
            var          plainText  = Guid.NewGuid().ToString();
            var          plainBytes = Encoding.Unicode.GetBytes(plainText);

            //-------------------------------------------------------
            // act
            //-------------------------------------------------------
            var encoded = key1.Encode(plainBytes);
            var decoded = key1.Decode(encoded);

            var result = Encoding.Unicode.GetString(decoded);

            //-------------------------------------------------------
            // assert
            //-------------------------------------------------------
            Assert.AreEqual <string>(plainText, result);
        }
Ejemplo n.º 6
0
        public void SymmetricKey_NoOperationAfterSerialization()
        {
            //-------------------------------------------------------
            //  arrange
            //-------------------------------------------------------
            SymmetricKey originalKey = SymmetricKey.Create();
            var          plainText   = Guid.NewGuid().ToString();
            var          plainBytes  = Encoding.Unicode.GetBytes(plainText);

            //-------------------------------------------------------
            // act
            //-------------------------------------------------------
            var encoded1 = originalKey.Encode(plainBytes);

            var serializedKey = Serialize.To.DataContract(originalKey);
            var result        = originalKey.Decode(encoded1);

            //-------------------------------------------------------
            // assert
            //-------------------------------------------------------
            Assert.Fail("Symmetric key should always throw an exception for operations after participating in serializing operations");
        }
Ejemplo n.º 7
0
        public void SymmetricKey_Constructor_SpecifiedProvider()
        {
            //-------------------------------------------------------
            //  arrange
            //-------------------------------------------------------
            SymmetricKey key1       = SymmetricKey.Create(Crypto.EncodeModes.TripleDES);
            var          plainText  = Guid.NewGuid().ToString();
            var          plainBytes = Encoding.Unicode.GetBytes(plainText);

            //-------------------------------------------------------
            // act
            //-------------------------------------------------------
            var encoded = key1.Encode(plainBytes);
            var decoded = key1.Decode(encoded);

            var result = Encoding.Unicode.GetString(decoded);

            //-------------------------------------------------------
            // assert
            //-------------------------------------------------------
            Assert.AreEqual <string>(plainText, result);
        }