Encrypt Method
Encrypts the specified data.
procedure Encrypt();
Remarks
Encrypt encrypts the specified data with the ECDSA public key specified in RecipientKey.
Encryption is performed using ECIES which requires an ECDSA key. RecipientKey must contain an ECDSA key. Algorithm is used to determine the eligibility of the key for this operation. Supported algorithms for encryption are:
- secp256r1
- secp384r2
- secp521r1
See CreateKey for details about key creation and algorithms.
When this method is called the component will encrypt the specified data using ECIES and the encrypted data will be output. To hex encode the output set UseHex to True.
The following properties are applicable when calling this method:
- EncryptionAlgorithm
- HMACAlgorithm
- HMACOptionalInfo
- HMACKeySize
- IV
- KDF
- KDFHashAlgorithm
- KDFOptionalInfo
- UseHex
Input and Output Properties
The component will determine the source and destination of the input and output based on which properties are set.
The order in which the input properties are checked is as follows:
When a valid source is found the search stops. The order in which the output properties are checked is as follows:
- SetOutputStream
- OutputFile
- OutputMessage: The output data is written to this property if no other destination is specified.
When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.
Encrypt and Decrypt Example
//Create an ECDSA key on Party 2
Ecc ecc2 = new Ecc();
ecc2.CreateKey("secp256r1");
string ecc2_priv = ecc2.Key.PrivateKey;
string ecc2_pub = ecc2.Key.PublicKey;
//Transmit public key to Party 1
//Encrypt the message on Party 1 using public key from Party 2
Ecc ecc1 = new Ecc();
ecc1.InputMessage = "hello ecc";
ecc1.RecipientKey.PublicKey = ecc2_pub;
ecc1.UseHex = true;
ecc1.Encrypt();
string encryptedMessage = ecc1.OutputMessage;
//Transmit the encrypted message to Party 2
//Decrypt the message using the private key for Party 2
ecc2.Key.PrivateKey = ecc2_priv;
ecc2.InputMessage = encryptedMessage;
ecc2.UseHex = true;
ecc2.Decrypt();
Console.WriteLine(ecc2.OutputMessage);
Encrypt and Decrypt Example (AES with IV)
//Create an ECDSA key on Party 2
Ecc ecc2 = new Ecc();
ecc2.CreateKey("secp256r1");
string ecc2_priv = ecc2.Key.PrivateKey;
string ecc2_pub = ecc2.Key.PublicKey;
//Transmit public key to Party 1
//Encrypt the message on Party 1 using public key from Party 2
Ecc ecc1 = new Ecc();
//Use an IV (16 bytes for AES) - In a real environment this should be random
byte[] IV = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
ecc1.EncryptionAlgorithm = EccEncryptionAlgorithms.iesAES;
ecc1.IVB = IV;
ecc1.InputMessage = "hello ecc";
ecc1.RecipientKey.PublicKey = ecc2_pub;
ecc1.UseHex = true;
ecc1.Encrypt();
string encryptedMessage = ecc1.OutputMessage;
//Transmit the encrypted message and the IV to Party 2
//Decrypt the message using the private key for Party 2 and the IV
ecc2.EncryptionAlgorithm = EccEncryptionAlgorithms.iesAES;
ecc2.IVB = IV;
ecc2.Key.PrivateKey = ecc2_priv;
ecc2.InputMessage = encryptedMessage;
ecc2.UseHex = true;
ecc2.Decrypt();
Console.WriteLine(ecc2.OutputMessage);
Encrypt and Decrypt Example (XOR Encryption Algorithm)
//Create an ECDSA key on Party 2
Ecc ecc2 = new Ecc();
ecc2.CreateKey("secp256r1");
string ecc2_priv = ecc2.Key.PrivateKey;
string ecc2_pub = ecc2.Key.PublicKey;
//Transmit public key to Party 1
//Encrypt the message on Party 1 using public key from Party 2
Ecc ecc1 = new Ecc();
ecc1.EncryptionAlgorithm = EccEncryptionAlgorithms.iesXOR;
ecc1.InputMessage = "hello ecc";
ecc1.RecipientKey.PublicKey = ecc2_pub;
ecc1.UseHex = true;
ecc1.Encrypt();
string encryptedMessage = ecc1.OutputMessage;
//Transmit the encrypted message to Party 2
//Decrypt the message using the private key for Party 2
ecc2.EncryptionAlgorithm = EccEncryptionAlgorithms.iesXOR;
ecc2.Key.PrivateKey = ecc2_priv;
ecc2.InputMessage = encryptedMessage;
ecc2.UseHex = true;
ecc2.Decrypt();
Console.WriteLine(ecc2.OutputMessage);
Encrypt and Decrypt Example (KDF Options)
//Create an ECDSA key on Party 2
Ecc ecc2 = new Ecc();
ecc2.CreateKey("secp256r1");
string ecc2_priv = ecc2.Key.PrivateKey;
string ecc2_pub = ecc2.Key.PublicKey;
//Transmit public key to Party 1
//Encrypt the message on Party 1 using public key from Party 2
Ecc ecc1 = new Ecc();
ecc1.KDF = "KDF1"; //Use KDF1
ecc1.KDFHashAlgorithm = EccKDFHashAlgorithms.iesSHA1;
ecc1.Config("KDFOptionalInfo=202122232425262728292a2b2c2d2e2f"); //Hex encoded string
ecc1.InputMessage = "hello ecc";
ecc1.RecipientKey.PublicKey = ecc2_pub;
ecc1.UseHex = true;
ecc1.Encrypt();
string encryptedMessage = ecc1.OutputMessage;
//Transmit the encrypted message to Party 2
//Decrypt the message using the private key for Party 2
ecc2.KDF = "KDF1";
ecc2.KDFHashAlgorithm = EccKDFHashAlgorithms.iesSHA1;
ecc2.Config("KDFOptionalInfo=202122232425262728292a2b2c2d2e2f");
ecc2.Key.PrivateKey = ecc2_priv;
ecc2.InputMessage = encryptedMessage;
ecc2.UseHex = true;
ecc2.Decrypt();
Console.WriteLine(ecc2.OutputMessage);