Ejemplo n.º 1
0
        public string GetEncryptedValue(string key)
        {
            var encryptedValue = this.virgilCipher.CustomParams().GetString(Encoding.UTF8.GetBytes(key));

            using (var cipher = new VirgilCipher())
            {
                var decrypted = this.password == null?
                                cipher.DecryptWithKey(encryptedValue, this.recipientId, this.privateKey) :
                                    cipher.DecryptWithKey(encryptedValue, this.recipientId, this.privateKey, Encoding.UTF8.GetBytes(this.password));

                return(Encoding.UTF8.GetString(decrypted));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decrypts and verifies the data.
        /// </summary>
        /// <param name="cipherData">The cipher data.</param>
        /// <param name="privateKey">The Private key to decrypt.</param>
        /// <param name="publicKeys"> The list of trusted public keys for verification,
        /// which can contain signer's public key.</param>
        /// <returns>The decrypted data</returns>
        /// <exception cref="Virgil.SDK.Exceptions.SignatureIsNotValidException"></exception>
        /// <exception cref="Virgil.SDK.Exceptions.CryptoException"></exception>
        /// <example>
        ///     <code>
        ///         var crypto = new VirgilCrypto();
        ///         var decryptedData = crypto.DecryptThenVerify(cipherData, bob.PrivateKey, alice.PublicKey);
        ///     </code>
        /// </example>
        /// How to get cipherData as well as Alice's and Bob's key pairs.
        /// <see cref="SignThenEncrypt(byte[] data, IPrivateKey privateKey, params IPublicKey[] recipients)"/>
        public override byte[] DecryptThenVerify(byte[] cipherData, IPrivateKey privateKey, params IPublicKey[] publicKeys)
        {
            try
            {
                using (var signer = new VirgilSigner())
                    using (var cipher = new VirgilCipher())
                    {
                        var decryptedData = cipher.DecryptWithKey(cipherData, privateKey.Get().ReceiverId, privateKey.Get().Value);
                        var signature     = cipher.CustomParams().GetData(this.CustomParamKeySignature);

                        var signerPublicKey = publicKeys.FirstOrDefault();
                        if (publicKeys.Length > 1)
                        {
                            var signerId = cipher.CustomParams().GetData(this.CustomParamKeySignerId);
                            signerPublicKey = publicKeys.Single(publicKey => publicKey.Get().ReceiverId.SequenceEqual(signerId));
                        }

                        var isValid = signer.Verify(decryptedData, signature, signerPublicKey.Get().Value);
                        if (!isValid)
                        {
                            throw new SignatureIsNotValidException();
                        }

                        return(decryptedData);
                    }
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Decrypts and verifies the specified data.
        /// </summary>
        /// <param name="cipherData">The cipher data.</param>
        /// <param name="privateKey">The Private key to decrypt.</param>
        /// <param name="publicKeys"> The list of trusted public keys for verification,
        /// which can contain signer's public key.</param>
        /// <returns>The decrypted and verified data</returns>
        /// <exception cref="VirgilCryptoException"></exception>
        /// <example>
        ///     <code>
        ///         var crypto = new VirgilCrypto();
        ///         var decryptedData = crypto.DecryptThenVerify(cipherData, bob.PrivateKey, alice.PublicKey);
        ///     </code>
        /// </example>
        /// <remarks>How to get cipherData as well as Alice's and Bob's key pairs
        /// <see cref="SignThenEncrypt(byte[], IPrivateKey, IPublicKey[])"/>.</remarks>
        public byte[] DecryptThenVerify(byte[] cipherData, IPrivateKey privateKey, params IPublicKey[] publicKeys)
        {
            try
            {
                using (VirgilSigner signer = new VirgilSigner(VirgilHash.Algorithm.SHA512))
                    using (VirgilCipher cipher = new VirgilCipher())
                    {
                        byte[] decryptedData =
                            cipher.DecryptWithKey(cipherData, VirgilCryptoExtentions.Get(privateKey).Id,
                                                  VirgilCryptoExtentions.Get(privateKey).RawKey);
                        byte[] signature = cipher.CustomParams().GetData(this.CustomParamKeySignature);

                        IPublicKey signerPublicKey = (publicKeys.Length > 0) ? publicKeys[0] : null;
                        if (publicKeys.Length > 1)
                        {
                            byte[] signerId = cipher.CustomParams().GetData(this.CustomParamKeySignerId);
                            signerPublicKey = FindPublicKeyBySignerId(publicKeys, signerId);
                        }

                        bool isValid = signer.Verify(decryptedData, signature, VirgilCryptoExtentions.Get(signerPublicKey).RawKey);
                        if (!isValid)
                        {
                            throw new VirgilCryptoException("Signature is not valid.");
                        }

                        return(decryptedData);
                    }
            }
            catch (Exception ex)
            {
                throw new VirgilCryptoException(ex.Message);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Decrypts the specified data using Private key.
 /// </summary>
 public override byte[] Decrypt(byte[] cipherData, IPrivateKey privateKey)
 {
     try
     {
         using (var cipher = new VirgilCipher())
         {
             var data = cipher.DecryptWithKey(cipherData, privateKey.Get().ReceiverId, privateKey.Get().Value);
             return(data);
         }
     }
     catch (Exception ex)
     {
         throw new CryptoException(ex.Message);
     }
 }
Ejemplo n.º 5
0
        public byte[] Decrypt(byte[] cipherData, string privateKeyPassword = null)
        {
            using (var cipher = new VirgilCipher())
            {
                var contentInfoSize = VirgilCipherBase.DefineContentInfoSize(cipherData);
                if (contentInfoSize == 0)
                {
                    throw new ArgumentException("Content info header is missing or corrupted", nameof(cipherData));
                }

                byte[] result;
                if (privateKeyPassword != null)
                {
                    result = cipher.DecryptWithKey(cipherData, this.GetRecepientId(), this.PrivateKey.Data,
                                                   privateKeyPassword.GetBytes());
                }
                else
                {
                    result = cipher.DecryptWithKey(cipherData, this.GetRecepientId(), this.PrivateKey.Data);
                }

                return(result);
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Decrypts the specified data using the specified Private key.
 /// </summary>
 /// <param name="cipherData">encrypted data bytes for decryption.</param>
 /// <param name="privateKey">private key for decryption.</param>
 /// <returns>Decrypted data bytes.</returns>
 /// <example>
 ///     <code>
 ///         var crypto = new VirgilCrypto();
 ///         var keyPair = crypto.GenerateKeys();
 ///         var plainData = crypto.Decrypt(encryptedData, keyPair.PrivateKey);
 ///     </code>
 /// </example>
 /// <remarks>How to get encryptedData <see cref="Encrypt(byte[], IPublicKey[])"/>.</remarks>
 public byte[] Decrypt(byte[] cipherData, IPrivateKey privateKey)
 {
     try
     {
         using (VirgilCipher cipher = new VirgilCipher())
         {
             byte[] data = cipher.DecryptWithKey(cipherData,
                                                 VirgilCryptoExtentions.Get(privateKey).Id,
                                                 VirgilCryptoExtentions.Get(privateKey).RawKey);
             return(data);
         }
     }
     catch (Exception ex)
     {
         throw new VirgilCryptoException(ex.Message);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Decrypts and verifies the data.
        /// </summary>
        /// <param name="cipherData">The cipher data.</param>
        /// <param name="privateKey">The Private key to decrypt.</param>
        /// <param name="publicKey">The Public key to verify.</param>
        /// <returns>The decrypted data</returns>
        /// <exception cref="Virgil.SDK.Exceptions.SignatureIsNotValidException"></exception>
        /// <exception cref="Virgil.SDK.Exceptions.CryptoException"></exception>
        public override byte[] DecryptThenVerify(byte[] cipherData, IPrivateKey privateKey, IPublicKey publicKey)
        {
            try
            {
                using (var signer = new VirgilSigner())
                    using (var cipher = new VirgilCipher())
                    {
                        var decryptedData = cipher.DecryptWithKey(cipherData, privateKey.Get().ReceiverId, privateKey.Get().Value);
                        var signature     = cipher.CustomParams().GetData(this.CustomParamKeySignature);

                        var isValid = signer.Verify(decryptedData, signature, publicKey.Get().Value);
                        if (!isValid)
                        {
                            throw new SignatureIsNotValidException();
                        }

                        return(decryptedData);
                    }
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }