Ejemplo n.º 1
0
    public void ImportFromXml_CreatesAppropriateDescriptor()
    {
        // Arrange
        var masterKey  = Convert.ToBase64String(Encoding.UTF8.GetBytes("[PLACEHOLDER]"));
        var descriptor = new CngGcmAuthenticatedEncryptorDescriptor(
            new CngGcmAuthenticatedEncryptorConfiguration()
        {
            EncryptionAlgorithm         = Constants.BCRYPT_AES_ALGORITHM,
            EncryptionAlgorithmKeySize  = 192,
            EncryptionAlgorithmProvider = null
        },
            masterKey.ToSecret());
        var control = CreateEncryptorInstanceFromDescriptor(descriptor);

        var xml = $@"
                <descriptor version='1' xmlns:enc='http://schemas.asp.net/2015/03/dataProtection'>
                  <encryption algorithm='AES' keyLength='192' />
                  <masterKey enc:requiresEncryption='true'>{masterKey}</masterKey>
                </descriptor>";
        var deserializedDescriptor = new CngGcmAuthenticatedEncryptorDescriptorDeserializer().ImportFromXml(XElement.Parse(xml));
        var test = CreateEncryptorInstanceFromDescriptor(deserializedDescriptor as CngGcmAuthenticatedEncryptorDescriptor);

        // 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);
    }
Ejemplo n.º 2
0
    public void ExportToXml_WithoutProviders_ProducesCorrectPayload()
    {
        // Arrange
        var masterKey  = Convert.ToBase64String(Encoding.UTF8.GetBytes("[PLACEHOLDER]"));
        var descriptor = new CngGcmAuthenticatedEncryptorDescriptor(new CngGcmAuthenticatedEncryptorConfiguration()
        {
            EncryptionAlgorithm        = "enc-alg",
            EncryptionAlgorithmKeySize = 2048
        }, masterKey.ToSecret());

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

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

        XmlAssert.Equal(expectedXml, retVal.SerializedDescriptorElement);
    }
Ejemplo n.º 3
0
    private static IAuthenticatedEncryptor CreateEncryptorInstanceFromDescriptor(CngGcmAuthenticatedEncryptorDescriptor descriptor)
    {
        var encryptorFactory = new CngGcmAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
        var key = new Key(
            keyId: Guid.NewGuid(),
            creationDate: DateTimeOffset.Now,
            activationDate: DateTimeOffset.Now + TimeSpan.FromHours(1),
            expirationDate: DateTimeOffset.Now + TimeSpan.FromDays(30),
            descriptor: descriptor,
            encryptorFactories: new[] { encryptorFactory });

        return(key.CreateEncryptor());
    }