SebWindowsClient.ConfigurationUtils.SEBConfigFileManager.EncryptDataUsingIdentity C# (CSharp) Method

EncryptDataUsingIdentity() public static method

Encrypt preferences using a certificate
public static EncryptDataUsingIdentity ( byte data, X509Certificate2 certificateRef ) : byte[]
data byte
certificateRef System.Security.Cryptography.X509Certificates.X509Certificate2
return byte[]
        public static byte[] EncryptDataUsingIdentity(byte[] data, X509Certificate2 certificateRef)
        {
            //get public key hash from selected identity's certificate
            byte[] publicKeyHash = SEBProtectionController.GetPublicKeyHashFromCertificate(certificateRef);

            //encrypt data using public key
            byte[] encryptedData = SEBProtectionController.EncryptDataWithCertificate(data, certificateRef);

            // Create byte array large enough to hold prefix, public key hash and encrypted data
            byte[] encryptedSebData = new byte[encryptedData.Length + PREFIX_LENGTH + publicKeyHash.Length];
            // Copy prefix indicating data has been encrypted with a public key identified by hash into out data
            string prefixString = PUBLIC_KEY_HASH_MODE;
            Buffer.BlockCopy(Encoding.UTF8.GetBytes(prefixString), 0, encryptedSebData, 0, PREFIX_LENGTH);
            // Copy public key hash to out data
            Buffer.BlockCopy(publicKeyHash, 0, encryptedSebData, PREFIX_LENGTH, publicKeyHash.Length);
            // Copy encrypted data to out data
            Buffer.BlockCopy(encryptedData, 0, encryptedSebData, PREFIX_LENGTH + publicKeyHash.Length, encryptedData.Length);

            return encryptedSebData;
        }

Usage Example

        public static byte[] EncryptSEBSettingsWithCredentials(string settingsPassword, bool passwordIsHash, X509Certificate2 certificateRef, SEBSettings.sebConfigPurposes configPurpose, bool forEditing)
        {
            byte[] bytes    = Encoding.UTF8.GetBytes(Plist.writeXml((object)SEBSettings.settingsCurrent).Replace("<array />", "<array></array>").Replace("<dict />", "<dict></dict>").Replace("<data />", "<data></data>"));
            string password = (string)null;

            if (string.IsNullOrEmpty(settingsPassword) && configPurpose == SEBSettings.sebConfigPurposes.sebConfigPurposeConfiguringClient)
            {
                password = "";
            }
            else if (string.IsNullOrEmpty(settingsPassword) && certificateRef == null)
            {
                if (SEBMessageBox.Show(SEBUIStrings.noEncryptionChosen, SEBUIStrings.noEncryptionChosenSaveUnencrypted, MessageBoxIcon.Question, MessageBoxButtons.YesNo, forEditing) == DialogResult.Yes)
                {
                    return(bytes);
                }
                return((byte[])null);
            }
            byte[] data = GZipByte.Compress(bytes);
            if (!string.IsNullOrEmpty(settingsPassword))
            {
                password = settingsPassword;
            }
            byte[] numArray1;
            if (password != null)
            {
                numArray1 = SEBConfigFileManager.EncryptDataUsingPassword(data, password, passwordIsHash, configPurpose);
            }
            else
            {
                byte[] numArray2 = new byte[data.Length + 4];
                Buffer.BlockCopy((Array)Encoding.UTF8.GetBytes("plnd"), 0, (Array)numArray2, 0, 4);
                Buffer.BlockCopy((Array)data, 0, (Array)numArray2, 4, data.Length);
                numArray1 = (byte[])numArray2.Clone();
            }
            if (certificateRef != null)
            {
                numArray1 = SEBConfigFileManager.EncryptDataUsingIdentity(numArray1, certificateRef);
            }
            return(GZipByte.Compress(numArray1));
        }