예제 #1
0
    public void ImportFromXml_CustomType_CreatesAppropriateDescriptor()
    {
        // Arrange
        var masterKey  = Convert.ToBase64String(Encoding.UTF8.GetBytes("[PLACEHOLDER]"));
        var descriptor = new ManagedAuthenticatedEncryptorDescriptor(
            new ManagedAuthenticatedEncryptorConfiguration()
        {
            EncryptionAlgorithmType    = typeof(Aes),
            EncryptionAlgorithmKeySize = 192,
            ValidationAlgorithmType    = typeof(HMACSHA384)
        },
            masterKey.ToSecret());
        var control = CreateEncryptorInstanceFromDescriptor(descriptor);

        var xml = $@"
                <descriptor>
                  <encryption algorithm='{typeof(Aes).AssemblyQualifiedName}' keyLength='192' />
                  <validation algorithm='{typeof(HMACSHA384).AssemblyQualifiedName}' />
                  <masterKey enc:requiresEncryption='true' xmlns:enc='http://schemas.asp.net/2015/03/dataProtection'>
                    <value>{masterKey}</value>
                  </masterKey>
                </descriptor>";
        var deserializedDescriptor = new ManagedAuthenticatedEncryptorDescriptorDeserializer().ImportFromXml(XElement.Parse(xml));
        var test = CreateEncryptorInstanceFromDescriptor(deserializedDescriptor as ManagedAuthenticatedEncryptorDescriptor);

        // Act & assert
        byte[] plaintext          = new byte[] { 1, 2, 3, 4, 5 };
        byte[] aad                = new byte[] { 2, 4, 6, 8, 0 };
        byte[] ciphertext         = control.Encrypt(new ArraySegment <byte>(plaintext), new ArraySegment <byte>(aad));
        byte[] roundTripPlaintext = test.Decrypt(new ArraySegment <byte>(ciphertext), new ArraySegment <byte>(aad));
        Assert.Equal(plaintext, roundTripPlaintext);
    }
    public void ExportToXml_BuiltInTypes_ProducesCorrectPayload(Type encryptionAlgorithmType, Type validationAlgorithmType)
    {
        // Arrange
        var masterKey  = Convert.ToBase64String(Encoding.UTF8.GetBytes("[PLACEHOLDER]"));
        var descriptor = new ManagedAuthenticatedEncryptorDescriptor(new ManagedAuthenticatedEncryptorConfiguration()
        {
            EncryptionAlgorithmType    = encryptionAlgorithmType,
            EncryptionAlgorithmKeySize = 2048,
            ValidationAlgorithmType    = validationAlgorithmType
        }, masterKey.ToSecret());

        // Act
        var retVal = descriptor.ExportToXml();

        // Assert
        Assert.Equal(typeof(ManagedAuthenticatedEncryptorDescriptorDeserializer), retVal.DeserializerType);
        var expectedXml = $@"
                <descriptor>
                  <encryption algorithm='{encryptionAlgorithmType.Name}' keyLength='2048' />
                  <validation algorithm='{validationAlgorithmType.Name}' />
                  <masterKey enc:requiresEncryption='true' xmlns:enc='http://schemas.asp.net/2015/03/dataProtection'>
                    <value>{masterKey}</value>
                  </masterKey>
                </descriptor>";

        XmlAssert.Equal(expectedXml, retVal.SerializedDescriptorElement);
    }
예제 #3
0
    private static IAuthenticatedEncryptor CreateEncryptorInstanceFromDescriptor(ManagedAuthenticatedEncryptorDescriptor descriptor)
    {
        var encryptorFactory = new ManagedAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
        var key = new Key(
            Guid.NewGuid(),
            DateTimeOffset.Now,
            DateTimeOffset.Now + TimeSpan.FromHours(1),
            DateTimeOffset.Now + TimeSpan.FromDays(30),
            descriptor,
            new[] { encryptorFactory });

        return(key.CreateEncryptor());
    }