public async Task TestAddingSingleV2AsymmetricKeyWrap() { EncryptionParameters encryptionParameters = new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("allan")); IAsymmetricPublicKey publicKey = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); await encryptionParameters.AddAsync(new UserPublicKey[] { new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey), }); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(encryptionParameters, 1000); IEnumerable <V2AsymmetricKeyWrapHeaderBlock> wraps = documentHeaders.Headers.HeaderBlocks.OfType <V2AsymmetricKeyWrapHeaderBlock>(); Assert.That(wraps.Count(), Is.EqualTo(1), "There should be one V2AsymmetricKeyWrapHeaderBlock found."); V2AsymmetricKeyWrapHeaderBlock block1 = wraps.First(); ICryptoFactory cryptoFactory = Resolve.CryptoFactory.Create(encryptionParameters.CryptoId); IAsymmetricPrivateKey privateKey1 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); block1.SetPrivateKey(cryptoFactory, privateKey1); ICrypto cryptoFromAsymmetricKey = block1.Crypto(0); V2KeyWrapHeaderBlock symmetricKeyWrap = documentHeaders.Headers.HeaderBlocks.OfType <V2KeyWrapHeaderBlock>().First(); ICrypto cryptoFromSymmetricKey = cryptoFactory.CreateCrypto(symmetricKeyWrap.MasterKey, symmetricKeyWrap.MasterIV, 0); Assert.That(cryptoFromAsymmetricKey.Key, Is.EqualTo(cryptoFromSymmetricKey.Key), "The keys from Asymmetric and Symmetric should be equal."); IAsymmetricPrivateKey privateKey2 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey2); block1.SetPrivateKey(cryptoFactory, privateKey2); ICrypto cryptoFromAsymmetricKey1WithKey2 = block1.Crypto(0); Assert.That(cryptoFromAsymmetricKey1WithKey2, Is.Null, "There should be no valid key set and thus no ICrypto instance returned."); }
public bool Equals(IAsymmetricPrivateKey other) { if ((object)other == null) { return(false); } return(ToString().Equals(other.ToString())); }
public Signer(IAsymmetricPrivateKey privateKey) { if (privateKey == null) { throw new ArgumentNullException(nameof(privateKey)); } _privateKey = privateKey; }
/// <summary> /// Initializes a new instance of the <see cref="DecryptionParameter"/> class. /// </summary> /// <param name="privateKey">The private key.</param> /// <param name="cryptoId">The crypto identifier.</param> /// <exception cref="System.ArgumentNullException">privateKey</exception> public DecryptionParameter(IAsymmetricPrivateKey privateKey, Guid cryptoId) { if (privateKey == null) { throw new ArgumentNullException("privateKey"); } PrivateKey = privateKey; CryptoId = cryptoId; }
public override bool Equals(object obj) { IAsymmetricPrivateKey other = obj as IAsymmetricPrivateKey; if (other == null) { return(false); } return(Equals(other)); }
public override IAxCryptDocument Document(IAsymmetricPrivateKey privateKey, Guid cryptoId, Headers headers) { V2AxCryptDocument v2Document = new V2AxCryptDocument(); if (cryptoId == new V1Aes128CryptoFactory().CryptoId) { return(v2Document); } v2Document.Load(privateKey, cryptoId, headers); return(v2Document); }
public static async Task TestGetTwoAsymmetricCryptosFromHeaders(CryptoImplementation cryptoImplementation) { SetupAssembly.AssemblySetupCrypto(cryptoImplementation); Headers headers = new Headers(); EncryptionParameters parameters = new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("secrets")); IAsymmetricPublicKey publicKey1 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); IAsymmetricPublicKey publicKey2 = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey2); await parameters.AddAsync(new UserPublicKey[] { new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey1), new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey2), }); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(parameters, 10); using (V2HmacStream <MemoryStream> stream = V2HmacStream.Create <MemoryStream>(new V2HmacCalculator(new SymmetricKey(new byte[0])), new MemoryStream())) { documentHeaders.WriteStartWithHmac(stream); stream.Flush(); stream.Chained.Position = 0; using (V2AxCryptReader reader = new V2AxCryptReader(new LookAheadStream(stream.Chained))) { while (reader.Read()) { if (reader.CurrentItemType == AxCryptItemType.HeaderBlock) { headers.HeaderBlocks.Add(reader.CurrentHeaderBlock); } } SymmetricKey dataEncryptingKey = documentHeaders.Headers.FindHeaderBlock <V2KeyWrapHeaderBlock>().MasterKey; IEnumerable <V2AsymmetricKeyWrapHeaderBlock> readerAsymmetricKeys = headers.HeaderBlocks.OfType <V2AsymmetricKeyWrapHeaderBlock>(); Assert.That(readerAsymmetricKeys.Count(), Is.EqualTo(2), "There should be two asymmetric keys in the headers."); V2AsymmetricKeyWrapHeaderBlock asymmetricKey1 = readerAsymmetricKeys.First(); IAsymmetricPrivateKey privateKey1 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); asymmetricKey1.SetPrivateKey(new V2Aes256CryptoFactory(), privateKey1); Assert.That(dataEncryptingKey, Is.EqualTo(asymmetricKey1.Crypto(0).Key), "The asymmetric wrapped key should be the one in the ICrypto instance."); IAsymmetricPrivateKey privateKey2 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey2); asymmetricKey1.SetPrivateKey(new V2Aes256CryptoFactory(), privateKey2); Assert.That(asymmetricKey1.Crypto(0), Is.Null, "The ICrypto instance should be null, since the private key was wrong."); V2AsymmetricKeyWrapHeaderBlock asymmetricKey2 = readerAsymmetricKeys.Last(); asymmetricKey2.SetPrivateKey(new V2Aes256CryptoFactory(), privateKey2); Assert.That(dataEncryptingKey, Is.EqualTo(asymmetricKey2.Crypto(0).Key), "The asymmetric wrapped key should be the one in the ICrypto instance."); asymmetricKey2.SetPrivateKey(new V2Aes256CryptoFactory(), privateKey1); Assert.That(asymmetricKey2.Crypto(0), Is.Null, "The ICrypto instance should be null, since the private key was wrong."); } } }
public static void TestAsymmetricEncryptionFailedDecryptionWrongKey2() { IAsymmetricPublicKey publicKey = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey2); string text = "AxCrypt is really very great!"; byte[] encryptedBytes = publicKey.Transform(Encoding.UTF8.GetBytes(text)); Assert.That(encryptedBytes.Length, Is.EqualTo(512)); IAsymmetricPrivateKey privateKey = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); byte[] decryptedBytes = privateKey.Transform(encryptedBytes); Assert.That(decryptedBytes, Is.Null); }
public static async Task TestEncryptWithOneAsymmetricKey() { EncryptionParameters encryptionParameters = new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("allan")); IAsymmetricPublicKey publicKey = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); await encryptionParameters.AddAsync(new UserPublicKey[] { new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey), }); byte[] plainText = Resolve.RandomGenerator.Generate(25000); byte[] output = EncrytionHelper(encryptionParameters, "TestEncryptWithOneAsymmetricKey.txt", AxCryptOptions.EncryptWithCompression, plainText); IAsymmetricPrivateKey privateKey1 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); DecryptionParameter decryptionParameter = new DecryptionParameter(privateKey1, new V2Aes256CryptoFactory().CryptoId); byte[] decryptedText = DecryptionHelper(new DecryptionParameter[] { decryptionParameter }, output); Assert.That(decryptedText, Is.Not.Null, "The deryption failed because no valid decryption parameter was found."); Assert.That(decryptedText, Is.EquivalentTo(plainText), "The decrypted text should be the same as was originally encrypted."); }
public async Task <string> SignAsync(UserAccount userAccount) { if (userAccount == null) { throw new ArgumentNullException(nameof(userAccount)); } if (userAccount.SubscriptionLevel <= SubscriptionLevel.Free) { return(string.Empty); } IAsymmetricPrivateKey privateKey = await New <ILicenseAuthority>().PrivateKeyAsync(); byte[] signature = new Signer(privateKey).Sign(SignedFields(userAccount)); return(Convert.ToBase64String(signature)); }
public bool Load(IAsymmetricPrivateKey privateKey, Guid cryptoId, Headers headers) { if (headers == null) { throw new ArgumentNullException("headers"); } ResetState(); CryptoFactory = Resolve.CryptoFactory.Create(cryptoId); IEnumerable <V2AsymmetricKeyWrapHeaderBlock> keyWraps = headers.HeaderBlocks.OfType <V2AsymmetricKeyWrapHeaderBlock>(); foreach (V2AsymmetricKeyWrapHeaderBlock keyWrap in keyWraps) { keyWrap.SetPrivateKey(CryptoFactory, privateKey); if (keyWrap.Crypto(0) == null) { continue; } DocumentHeaders = new V2DocumentHeaders(keyWrap); if (!DocumentHeaders.Load(headers)) { throw new InvalidOperationException("If the master key was decrypted with the private key, the load should not be able to fail."); } V2AlgorithmVerifierEncryptedHeaderBlock algorithmVerifier = DocumentHeaders.Headers.FindHeaderBlock <V2AlgorithmVerifierEncryptedHeaderBlock>(); PassphraseIsValid = algorithmVerifier != null && algorithmVerifier.IsVerified; if (PassphraseIsValid) { Properties = EncryptedProperties.Create(this); return(true); } } return(false); }
public bool Load(IAsymmetricPrivateKey privateKey, Guid cryptoId, Headers headers) { ResetState(Passphrase.Empty); return(false); }
/// <summary> /// Constructor with a random source for blinding operations on the private key. /// </summary> /// <param name="privKey">The private key to use.</param> /// <param name="random">A source of randomness.</param> public CmsKeyTransEnvelopedRecipient(IAsymmetricPrivateKey privKey, SecureRandom random) { this.privKey = privKey; this.random = random; }
public override IAxCryptDocument Document(IAsymmetricPrivateKey privateKey, Guid cryptoId, Headers headers) { return(new V1AxCryptDocument()); }
/// <summary> /// Opens an AxCrypt document instance by way of a asymmetrical private key and algorithm, if possible. /// </summary> /// <param name="privateKey">The private key.</param> /// <param name="cryptoId">The crypto identifier.</param> /// <param name="headers">The headers.</param> /// <returns>An instance with a valid passphrase or not.</returns> public abstract IAxCryptDocument Document(IAsymmetricPrivateKey privateKey, Guid cryptoId, Headers headers);
public Pkcs12SafeBagBuilder(IAsymmetricPrivateKey privateKey) : this(PrivateKeyInfo.GetInstance(privateKey.GetEncoded())) { }
public Pkcs12SafeBagBuilder(IAsymmetricPrivateKey privateKey, ICipherBuilder <AlgorithmIdentifier> encryptor) : this(PrivateKeyInfo.GetInstance(privateKey.GetEncoded()), encryptor) { }
/// <summary> /// Sets the private key. /// </summary> /// <param name="privateKey">The private key.</param> public void SetPrivateKey(ICryptoFactory cryptoFactory, IAsymmetricPrivateKey privateKey) { _cryptoFactory = cryptoFactory; _privateKey = privateKey; _decryptedDataBlock = null; }
public override IAxCryptDocument Document(IAsymmetricPrivateKey privateKey, Guid cryptoId, Headers headers) { throw new NotImplementedException(); }
public PgpPrivateKey(long keyId, IAsymmetricPrivateKey privateKey) { this.keyId = keyId; this.privateKey = privateKey; }
/// <summary> /// Base constructor - just the private key. /// </summary> /// <param name="privKey">The private key to use.</param> public CmsKeyTransEnvelopedRecipient(IAsymmetricPrivateKey privKey) : this(privKey, null) { }