Example #1
0
        public async Task <EncryptionKey> DecriptAsync(byte[] encryptedKey)
        {
            using var sourceMs      = new MemoryStream(encryptedKey);
            using var destinationMs = new MemoryStream();
            await StaticEncryptor.DecryptAsync(sourceMs, destinationMs, _key).ConfigureAwait(false);

            return(EncryptionKey.CreateFromSerializedVersion(destinationMs.ToArray()));
        }
        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 #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);
            }
        }