Пример #1
0
        public void CanSerializeDeserialize()
        {
            var value = new EncryptedValue
            {
                AlgorithmVersion = '\u00A9',
                KeyVersion = "123",
                Value = "abc"
            };

            var serializedValue = value.Serialize();

            var newValue = EncryptedValue.Deserialize(serializedValue);
            Assert.That(newValue.AlgorithmVersion, Is.EqualTo(value.AlgorithmVersion));
            Assert.That(newValue.KeyVersion, Is.EqualTo(value.KeyVersion));
            Assert.That(newValue.Value, Is.EqualTo(value.Value));
        }
Пример #2
0
        public string Encrypt(string plainText)
        {
            if (string.IsNullOrEmpty(plainText)) return plainText;

            var buffer = _encryptionAlgorithm.Encrypt(GetEncryptionKey(_store.EncryptionVersion),
                Encoding.UTF8.GetBytes(plainText));

            var value = new EncryptedValue
            {
                AlgorithmVersion = _encryptionAlgorithm.Version,
                KeyVersion = _store.EncryptionVersion,
                Value = Convert.ToBase64String(buffer)
            };

            return value.Serialize();
        }
Пример #3
0
        public void CanEncryptDecrypt()
        {
            var plainTextBuffer = Encoding.UTF8.GetBytes(_plainText);
            var cipherTextBuffer = new byte[4] { 25, 100, 128, 5 };
            var encryptionKeyCipherTextBuffer = GenerateRandomKey();
            var EncryptionKeyPlainTextBuffer = Encoding.UTF8.GetBytes("e_" + _stubCryptoStore.EncryptionVersion);

            SetupMockHashAlgorithmToHash(_masterKeyPlainTextBuffer,
                EncryptionKeyPlainTextBuffer, encryptionKeyCipherTextBuffer);
            SetupMockEncryptionAlgorithmToEncryptDecrypt(encryptionKeyCipherTextBuffer,
                plainTextBuffer, cipherTextBuffer);

            var expectedEncryptedValue = new EncryptedValue
            {
                AlgorithmVersion = _algorithmVersion,
                KeyVersion = _stubCryptoStore.EncryptionVersion,
                Value = Convert.ToBase64String(cipherTextBuffer)
            };

            var actualEncryptedValue = _cryptoService.Encrypt(_plainText);
            Assert.That(expectedEncryptedValue.Serialize(), Is.EqualTo(actualEncryptedValue));
            Assert.That(_cryptoService.Decrypt(actualEncryptedValue), Is.EqualTo(_plainText));
        }