示例#1
0
        /// <summary>
        /// Signs the specified data using Private key.
        /// </summary>
        public override byte[] Sign(byte[] data, IPrivateKey privateKey)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }

            try
            {
                using (var signer = new VirgilSigner())
                {
                    var signature = signer.Sign(data, privateKey.Get().Value);
                    return(signature);
                }
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
示例#2
0
        /// <summary>
        /// Signs and encrypts the data.
        /// </summary>
        /// <param name="data">The data to encrypt.</param>
        /// <param name="privateKey">The Private key to sign the <param name="data"></param>.</param>
        /// <param name="recipients">The list of Public key recipients to encrypt the <param name="data"></param>.</param>
        /// <returns></returns>
        /// <exception cref="Virgil.SDK.Exceptions.CryptoException"></exception>
        public override byte[] SignThenEncrypt(byte[] data, IPrivateKey privateKey, params IPublicKey[] recipients)
        {
            try
            {
                using (var signer = new VirgilSigner())
                    using (var cipher = new VirgilCipher())
                    {
                        var signature = signer.Sign(data, privateKey.Get().Value);

                        var customData = cipher.CustomParams();
                        customData.SetData(this.CustomParamKeySignature, signature);

                        foreach (var publicKey in recipients)
                        {
                            cipher.AddKeyRecipient(publicKey.Get().ReceiverId, publicKey.Get().Value);
                        }

                        return(cipher.Encrypt(data, true));
                    }
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
示例#3
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);
            }
        }
示例#4
0
        /// <summary>
        /// Extracts the Public key from Private key.
        /// </summary>
        public override IPublicKey ExtractPublicKey(IPrivateKey privateKey)
        {
            try
            {
                var publicKeyData = VirgilKeyPair.ExtractPublicKey(privateKey.Get().Value, new byte[] { });

                var publicKey = new PublicKey
                {
                    ReceiverId = privateKey.Get().ReceiverId,
                    Value      = VirgilKeyPair.PublicKeyToDER(publicKeyData)
                };

                return(publicKey);
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
示例#5
0
        /// <summary>
        /// Exports the Private key into material representation.
        /// </summary>
        public override byte[] ExportPrivateKey(IPrivateKey privateKey, string password = null)
        {
            try
            {
                if (string.IsNullOrEmpty(password))
                {
                    return(VirgilKeyPair.PrivateKeyToDER(privateKey.Get().Value));
                }

                var passwordBytes = Encoding.UTF8.GetBytes(password);
                var encryptedKey  = VirgilKeyPair.EncryptPrivateKey(privateKey.Get().Value, passwordBytes);

                return(VirgilKeyPair.PrivateKeyToDER(encryptedKey, passwordBytes));
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
示例#6
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);
     }
 }
示例#7
0
 /// <summary>
 /// Signs the specified stream using Private key.
 /// </summary>
 public override byte[] Sign(Stream inputStream, IPrivateKey privateKey)
 {
     try
     {
         using (var signer = new VirgilStreamSigner())
             using (var source = new VirgilStreamDataSource(inputStream))
             {
                 var signature = signer.Sign(source, privateKey.Get().Value);
                 return(signature);
             }
     }
     catch (Exception ex)
     {
         throw new CryptoException(ex.Message);
     }
 }
示例#8
0
 /// <summary>
 /// Decrypts the specified stream using Private key.
 /// </summary>
 public override void Decrypt(Stream cipherStream, Stream outputStream, IPrivateKey privateKey)
 {
     try
     {
         using (var cipher = new VirgilChunkCipher())
             using (var source = new VirgilStreamDataSource(cipherStream))
                 using (var sink = new VirgilStreamDataSink(outputStream))
                 {
                     cipher.DecryptWithKey(source, sink, privateKey.Get().ReceiverId, privateKey.Get().Value);
                 }
     }
     catch (Exception ex)
     {
         throw new CryptoException(ex.Message);
     }
 }
示例#9
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);
            }
        }