/// <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); } }
/// <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); } }
/// <summary> /// Signs and encrypts the specified data. /// </summary> /// <param name="data">The data to encrypt.</param> /// <param name="privateKey">The Private key to sign the data.</param> /// <param name="recipients">The list of Public key recipients to encrypt the data.</param> /// <returns>Signed and encrypted data bytes.</returns> /// <exception cref="Virgil.Crypto.VirgilCryptoException"></exception> /// <example> /// <code> /// var crypto = new VirgilCrypto(); /// var alice = crypto.GenerateKeys(); /// var bob = crypto.GenerateKeys(); /// var originalData = Encoding.UTF8.GetBytes("Hello Bob, How are you?"); /// // The data to be signed with Alice's Private key and then encrypted for Bob. /// var cipherData = crypto.SignThenEncrypt(originalData, alice.PrivateKey, bob.PublicKey); /// </code> /// </example> public byte[] SignThenEncrypt(byte[] data, IPrivateKey privateKey, params IPublicKey[] recipients) { try { using (VirgilSigner signer = new VirgilSigner(VirgilHash.Algorithm.SHA512)) using (VirgilCipher cipher = new VirgilCipher()) { byte[] signature = signer.Sign(data, VirgilCryptoExtentions.Get(privateKey).RawKey); VirgilCustomParams customData = cipher.CustomParams(); customData.SetData(this.CustomParamKeySignature, signature); IPublicKey publicKey = this.ExtractPublicKey(privateKey); customData.SetData(this.CustomParamKeySignerId, VirgilCryptoExtentions.Get(publicKey).Id); foreach (IPublicKey recipientPublicKey in recipients) { cipher.AddKeyRecipient(VirgilCryptoExtentions.Get(recipientPublicKey).Id, VirgilCryptoExtentions.Get(recipientPublicKey).RawKey); } return(cipher.Encrypt(data, true)); } } catch (Exception ex) { throw new VirgilCryptoException(ex.Message); } }
/// <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); } }
public byte[] Encrypt(byte[] data) { using (var cipher = new VirgilCipher()) { cipher.AddKeyRecipient(this.GetRecepientId(), this.PublicKey.Data); return(cipher.Encrypt(data, true)); } }
public string Decrypt(HttpPostedFileBase file) { VirgilCipher vc = new VirgilCipher(); var encryptedText = CryptoLogic.GetTxtFromFile(file); return(CryptoHelper.Decrypt(encryptedText, _testRecipient, CryptoLogic.KeyCollection[CryptoLogic.PRIVATE])); }
public void AddEncryptedValue(string key, string value, byte[] recipientId, byte[] publicKey) { using (var cipher = new VirgilCipher()) { cipher.AddKeyRecipient(recipientId, publicKey); var encryptedValue = cipher.Encrypt(Encoding.UTF8.GetBytes(value), true); this.virgilCipher.CustomParams().SetString(Encoding.UTF8.GetBytes(key), encryptedValue); } }
public static PersonalCard Import(byte[] personalCard, string serializationPassword) { using (var cipher = new VirgilCipher()) { var json = cipher.DecryptWithPassword(personalCard, serializationPassword.GetBytes(Encoding.UTF8)); var dto = JsonConvert.DeserializeObject <PersonalCardStorageDto>(json.GetString()); return(new PersonalCard(dto.virgil_card, new PrivateKey(dto.private_key))); } }
/// <summary> /// Encrypts the specified data for all recipient cards in the collection. /// </summary> /// <param name="data">The data to be encrypted.</param> /// <returns>Encrypted array of bytes.</returns> public byte[] Encrypt(byte[] data) { using (var cipher = new VirgilCipher()) { foreach (var card in this) { cipher.AddKeyRecipient(card.GetRecepientId(), card.PublicKey.Data); } return(cipher.Encrypt(data)); } }
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)); } }
/// <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); } }
public byte[] Export(string password) { var data = new PersonalCardStorageDto { virgil_card = this.VirgilCardDto, private_key = this.PrivateKey.Data }; var json = JsonConvert.SerializeObject(data); using (var cipher = new VirgilCipher()) { cipher.AddPasswordRecipient(password.GetBytes(Encoding.UTF8)); return(cipher.Encrypt(json.GetBytes(Encoding.UTF8), true)); } }
/// <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); } }
/// <summary> /// Encrypts the specified data using recipients Public keys. /// </summary> public override byte[] Encrypt(byte[] data, params IPublicKey[] recipients) { try { using (var cipher = new VirgilCipher()) { foreach (var publicKey in recipients) { cipher.AddKeyRecipient(publicKey.Get().ReceiverId, publicKey.Get().Value); } var encryptedData = cipher.Encrypt(data, true); return(encryptedData); } } catch (Exception ex) { throw new CryptoException(ex.Message); } }
/// <summary> /// Encrypts the specified data using the specified recipients Public keys. /// </summary> /// <param name="data">raw data bytes for encryption.</param> /// <param name="recipients"> list of recipients' public keys.</param> /// <returns>Encrypted bytes.</returns> /// <example> /// <code> /// var crypto = new VirgilCrypto(); /// var keyPair = crypto.GenerateKeys(); /// var data = Encoding.UTF8.GetBytes("Encrypt me!"); /// var encryptedData = crypto.Encrypt(data, keyPair.PublicKey); /// </code> /// </example> /// <remarks>How to decrypt data <see cref="Decrypt(byte[], Virgil.CryptoAPI.IPrivateKey)"/>.</remarks> public byte[] Encrypt(byte[] data, params IPublicKey[] recipients) { try { using (VirgilCipher cipher = new VirgilCipher()) { foreach (IPublicKey publicKey in recipients) { cipher.AddKeyRecipient(VirgilCryptoExtentions.Get(publicKey).Id, VirgilCryptoExtentions.Get(publicKey).RawKey); } byte[] encryptedData = cipher.Encrypt(data, true); return(encryptedData); } } catch (Exception ex) { throw new VirgilCryptoException(ex.Message); } }
/// <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); } }
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); } }