Sign & Encrypt a message
With the user's Cards in place, we are now ready to encryptEncryption is the process of encoding a message or information in such a way, that only authorized parties can access it more ... a message for encrypted communication. In this case we will encrypt the message using the Recipient's Virgil CardEach Card contains attributes associated with the holder of the Private key and are used to prove the ownership of a Public key more ....
As previously noted we encrypt data for secure communication, but a recipient also must be sure that no third party modified any of the message's content and that they can trust a sender, which is why we provide Data IntegrityGuarantees a receiver that their data has not been altered over its life-cycle more ... by adding a Digital SignatureA Digital Signature can be used with any kind of message, so the receiver can be sure of the sender's identity and that the message arrived intact more .... Therefore we must digitally sign data first and then encrypt.
In order to sign then encrypt messages, the Sender must load their own recently generated Virgil KeyThe Virgil Key is a wrapper of the user's secure Private Key more ... and search for the receiver's Virgil Cards at Virgil Services, where all Virgil Cards are saved.
// load Alice's Key from storage
$aliceKey = $virgilApi->Keys->load('[KEY_NAME]', '[KEY_PASSWORD]');
// search for Bob's Cards
$bobCards = $virgilApi->Cards->find(['bob']);
$message = 'Hey Bob, how's it going?';
// sign by Alice's key and then encrypt message for found Bob's Cards
$cipherText = $aliceKey->signThenEncrypt($message, $bobCards)->toBase64();
To create a signature, you will need to load Alice's Virgil KeyThe Virgil Key is a wrapper of the user's secure Private Key more .... The Loading Key guide provides more details.
Now the Receiver can verify that the message was sent by a specific Sender.
With the signature in place, the Sender is now ready to transmit the signed and encrypted message to the Receiver.