public static byte[] EncryptDataUsingIdentity(byte[] data, X509Certificate2 certificateRef)
 {
     byte[] hashFromCertificate = SEBProtectionController.GetPublicKeyHashFromCertificate(certificateRef);
     byte[] numArray1           = SEBProtectionController.EncryptDataWithCertificate(data, certificateRef);
     byte[] numArray2           = new byte[numArray1.Length + 4 + hashFromCertificate.Length];
     Buffer.BlockCopy((Array)Encoding.UTF8.GetBytes("pkhs"), 0, (Array)numArray2, 0, 4);
     Buffer.BlockCopy((Array)hashFromCertificate, 0, (Array)numArray2, 4, hashFromCertificate.Length);
     Buffer.BlockCopy((Array)numArray1, 0, (Array)numArray2, 4 + hashFromCertificate.Length, numArray1.Length);
     return(numArray2);
 }
Exemplo n.º 2
0
        /// ----------------------------------------------------------------------------------------
        /// <summary>
        /// Encrypt preferences using a certificate
        /// </summary>
        /// ----------------------------------------------------------------------------------------

        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;
        }