public DevelopKeyValueStore(string keyFolder)
        {
            InternalUtils.EnsureDirectory(keyFolder);
            var keyName = Path.Combine(keyFolder, DeveloperKeyName);

            if (!File.Exists(keyName))
            {
                using var key = EncryptionKey.CreateDefault();
                File.WriteAllBytes(keyName, key.Serialize());
            }

            _key = EncryptionKey.CreateFromSerializedVersion(File.ReadAllBytes(keyName));
        }
Example #2
0
        public async Task Avoid_using_the_same_IV()
        {
            using var key = EncryptionKey.CreateDefault();
            string keyMaterialFolder = Path.GetTempPath() + Guid.NewGuid().ToString();
            var    sut = new FolderBasedKeyValueStore(
                keyMaterialFolder,
                "password");

            //Same encryption with the same key will return the very same result.
            var encrypted = await sut.EncryptAsync(key).ConfigureAwait(false);

            var otherEncrypted = await sut.EncryptAsync(key).ConfigureAwait(false);

            //Same key encrypted two times should generate a different result due to different IV used
            Assert.NotEqual(encrypted, otherEncrypted);
        }
Example #3
0
        public FolderBasedKeyValueStore(
            string keyMaterialFolderStore,
            string password)
        {
            InternalUtils.EnsureDirectory(keyMaterialFolderStore);

            var keyName = Path.Combine(keyMaterialFolderStore, "1.key");

            if (!File.Exists(keyName))
            {
                //create the first key
                _key = EncryptionKey.CreateDefault();
                var serializedKey          = _key.Serialize();
                var encryptedSerializedKey = StaticEncryptor.AesEncryptWithPasswordAsync(serializedKey, password).Result;
                File.WriteAllBytes(keyName, encryptedSerializedKey);
            }
            else
            {
                var encryptedSerializedKey = File.ReadAllBytes(keyName);
                var serializedKey          = StaticEncryptor.AesDecryptWithPasswordAsync(encryptedSerializedKey, password).Result;
                _key = EncryptionKey.CreateFromSerializedVersion(serializedKey);
            }
        }