Пример #1
0
        public async Task Can_Encrypt_And_Decrypt_bytes_with_password()
        {
            const string content = "this test will be encrypted";

            byte[] stringContent = Encoding.UTF8.GetBytes(content);

            var encrypted = await StaticEncryptor.AesEncryptWithPasswordAsync(
                stringContent,
                Password).ConfigureAwait(false);

            //Now decrypt
            var decrypted = await StaticEncryptor.AesDecryptWithPasswordAsync(
                encrypted,
                Password).ConfigureAwait(false);

            Assert.Equal(stringContent, decrypted);
        }
Пример #2
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);
            }
        }
Пример #3
0
        public async Task Can_Encrypt_And_Decrypt_Stream_with_password()
        {
            const string content = "this test will be encrypted";

            byte[] stringContent = Encoding.UTF8.GetBytes(content);
            using var sourceStream    = new MemoryStream(stringContent);
            using var encryptedStream = new MemoryStream();
            await StaticEncryptor.AesEncryptWithPasswordAsync(
                sourceStream,
                encryptedStream,
                Password).ConfigureAwait(false);

            //Now decrypt
            var decryptedMemoryStream  = new MemoryStream();
            var readingEncryptedStream = new MemoryStream(encryptedStream.ToArray());
            await StaticEncryptor.AesDecryptWithPasswordAsync(
                readingEncryptedStream,
                decryptedMemoryStream,
                Password).ConfigureAwait(false);

            var decryptedString = Encoding.UTF8.GetString(decryptedMemoryStream.ToArray());

            Assert.Equal(decryptedString, content);
        }