Encrypt a file with several public keys
Sometimes there is a need to encrypt the same file with several public keys, e.g. where you need several different people to be able to decrypt it independently. SecureBlackbox allows to do that easily.
You might wish to read Encrypt a File With a Public Key article to get information about encrypting files with a single public key. Encryption for multiple keys is performed in pretty much the same way. The only difference is that you fill the keyring assigned to the TElPGPWriter.EncryptingKeys property with all the needed keys instead of just one public key:
TElPGPKeyring myKeys = new TElPGPKeyring(); myKeys.Load("pubring.pkr", "secring.skr", true); int key1Index = myKeys.FindPublicKeyByEmailAddress("email@example.com"); int key2Index = myKeys.FindPublicKeyByEmailAddress("firstname.lastname@example.org"); int key3Index = myKeys.FindPublicKeyByEmailAddress("email@example.com"); TElPGPKeyring encryptionKeys = new TElPGPKeyring(); encryptionKeys.AddPublicKey(myKeys.get_PublicKeys(key1Index)); encryptionKeys.AddPublicKey(myKeys.get_PublicKeys(key2Index)); encryptionKeys.AddPublicKey(myKeys.get_PublicKeys(key3Index));
Now, let's proceed to the encryption itself.
TElPGPWriter writer = new TElPGPWriter();
pgpWriter.EncryptionType = SBPGP.TSBPGPEncryptionType.etPublicKey;
pgpWriter.EncryptingKeys = encryptionKeys;
pgpWriter.Filename = "picture.jpg"; pgpWriter.Timestamp = DateTime.UtcNow;Note: an empty string assigned to the Filename property will make the components create a for-your-eyes-only file, which won't be decrypted to a persistent media.
pgpWriter.SymmetricKeyAlgorithm = SBPGPConstants.Unit.SB_PGP_ALGORITHM_SK_AES256;Note: in default configuration, TElPGPWriter comes with its own pre-defined encryption settings (CAST5 with 128 bit key, on the date of creation of this article).
pgpWriter.Armor = true; pgpWriter.Compress = true;
Note: in certain scenarios, especially where heavy multi-subkey structures are used, there might be a need to specify the exact subkey to be used for encryption. While SecureBlackbox does its best to find the appropriate encryption key or subkey automatically, sometimes this is not possible due to lack of information stored within the keys. If unsure about the purpose of a particular primary key or subkey (i.e. whether it is signing-only, encryption-only or both encryption and signing capable), SecureBlackbox always uses it for encryption. As a result, the resulting file may be encrypted with multiple keys, which might be confusing for the receiving software. You can tell TElPGPWriter that it must not encrypt the file for a particular key or subkey by setting its Enabled property to false. For instance, the following setting makes TElPGPWriter not encrypt the file with the primary key and only use its subkeys:
encryptionKeys.get_PublicKeys(0).Enabled = false;