Exemplo n.º 1
0
        public void GenerateKey_KeySize192()
        {
            var key    = Symmetric.GenerateKey("GenerateKey_KeySize192", "salt", keySize: KeySize.Bits192);
            var keyB64 = Convert.ToBase64String(key);

            Assert.AreEqual("eJmnbYPLuu1cUx4WRfUgH/PXzhQI7NLp", keyB64);
        }
Exemplo n.º 2
0
        public void GenerateKey_KeySize128()
        {
            var key    = Symmetric.GenerateKey("GenerateKey_KeySize128", "salt", keySize: KeySize.Bits128);
            var keyB64 = Convert.ToBase64String(key);

            Assert.AreEqual("MPkpMHyGEYhK9ymWaVSMfg==", keyB64);
        }
Exemplo n.º 3
0
        public void GenerateKey_MD5()
        {
            var key    = Symmetric.GenerateKey("GenerateKey_MD5", "salt", TNT.Cryptography.Enumerations.HashAlgorithm.MD5);
            var keyB64 = Convert.ToBase64String(key);

            Assert.AreEqual("oaVJMDQueZox8TP9E9Wrz0Fwxquezw3Sw7JjmdRIBHs=", keyB64);
        }
Exemplo n.º 4
0
        public void GenerateKey_Defaults()
        {
            var key    = Symmetric.GenerateKey("GenerateKey_Defaults", "salt");
            var keyB64 = Convert.ToBase64String(key);

            Assert.AreEqual("XMIqI9XOfHYsg0+y7vtLvGReLaWs1z6QwM+9faMjSXw=", keyB64);
        }
Exemplo n.º 5
0
        public void Constructor_WithBase64String()
        {
            var key = Convert.ToBase64String(Symmetric.GenerateKey("Constructor_WithByteArray", "salt"));
            var sut = new Symmetric(key);

            Assert.AreEqual(key, Convert.ToBase64String(sut.Key));
        }
Exemplo n.º 6
0
        public void Constructor_WithByteArray()
        {
            var key = Symmetric.GenerateKey("Constructor_WithByteArray", "salt");
            var sut = new Symmetric(key);

            Assert.AreEqual(key, sut.Key);
        }
Exemplo n.º 7
0
        public void BasicCanDecryptTest()
        {
            const string PLAINTEXT = "SmartEncryption Sym Test";
            var          key       = Symmetric.GenerateKey();
            var          cipher    = Symmetric.Encrypt(Encoding.UTF8.GetBytes(PLAINTEXT), key);
            var          plain     = Symmetric.Decrypt(cipher, key);

            Assert.AreEqual(PLAINTEXT, Encoding.UTF8.GetString(plain));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Asynchronously initializes the symmetric key for encryption.
        /// </summary>
        /// <returns>
        /// A <see cref="Task"/> object representing the process of asynchronous initialization.
        /// </returns>
        protected virtual async Task InitializeSymmetricKeyAsync()
        {
            if (IsSymmetricKeyInitialized)
            {
                return;
            }

            // get or create and put the file from/to its location
            if (KeyStorage.KeyLocationExists(KeyLocation))
            {
                DecryptSymmetricKey(await KeyStorage.GetKeyAsync(KeyLocation));
            }
            else
            {
                Symmetric.GenerateKey();
                await KeyStorage.PutKeyAsync(EncryptSymmetricKey(), KeyLocation);
            }

            IsSymmetricKeyInitialized = true;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes the symmetric key for encryption.
        /// </summary>
        protected virtual void InitializeSymmetricKey()
        {
            if (IsSymmetricKeyInitialized)
            {
                return;
            }

            // get or create and put the file from/to its physical location
            if (KeyStorage.KeyLocationExists(KeyLocation))
            {
                DecryptSymmetricKey(KeyStorage.GetKey(KeyLocation));
            }
            else
            {
                Symmetric.GenerateKey();
                KeyStorage.PutKey(EncryptSymmetricKey(), KeyLocation);
            }

            IsSymmetricKeyInitialized = true;
        }
Exemplo n.º 10
0
 /// <summary>
 /// Initializes the symmetric key by either reading it from the storage with the specified key location name or by
 /// generating a new key and saving it in it.
 /// </summary>
 /// <remarks>The method is called by the GoF template-methods.</remarks>
 protected override void InitializeSymmetricKey()
 {
     Symmetric.GenerateKey();
     Symmetric.GenerateIV();
     IsSymmetricKeyInitialized = true;
 }