/// <summary>
        /// Performs an encryption or decryption operation.
        /// </summary>
        /// <param name="cipherFunction">The delegate that will actually perform the cryptographic operation.</param>
        /// <returns>A buffer containing the result of the cryptographic operation.</returns>
        protected unsafe byte[] EncryptOrDecrypt(EncryptOrDecryptFunction cipherFunction)
        {
            Requires.NotNull(cipherFunction, nameof(cipherFunction));

            if (this.EncryptionPadding.Value == AsymmetricEncryptionPadding.None)
            {
                return(cipherFunction(null, NCryptEncryptFlags.NCRYPT_NO_PADDING_FLAG));
            }

            switch (this.EncryptionPadding.Value)
            {
            case AsymmetricEncryptionPadding.Pkcs1:
                return(cipherFunction(null, NCryptEncryptFlags.NCRYPT_PAD_PKCS1_FLAG));

            case AsymmetricEncryptionPadding.Oaep:
                fixed(char *hashAlgorithmNamePointer = &HashAlgorithmProviderFactory.GetHashAlgorithmName(this.SignatureHash.Value).ToCharArrayWithNullTerminator()[0])
                {
                    var paddingInfo = new BCrypt.BCRYPT_OAEP_PADDING_INFO
                    {
                        pszAlgId = hashAlgorithmNamePointer,
                        pbLabel  = null,
                        cbLabel  = 0,
                    };

                    return(cipherFunction(&paddingInfo, NCryptEncryptFlags.NCRYPT_PAD_OAEP_FLAG));
                }

            default:
                throw new NotImplementedException();
            }
        }
Example #2
0
        public static byte[] Decrypt(byte[] cipherText, CngKey key, CngAlgorithm hash)
        {
            uint num;

            BCrypt.BCRYPT_OAEP_PADDING_INFO bCRYPTOAEPPADDINGINFO = new BCrypt.BCRYPT_OAEP_PADDING_INFO(hash.Algorithm);
            uint num1 = NCrypt.NCryptDecrypt(key.Handle, cipherText, (int)cipherText.Length, ref bCRYPTOAEPPADDINGINFO, null, 0, out num, 4);

            if (num1 != 0)
            {
                throw new CryptographicException(string.Format("NCrypt.Decrypt() (plaintext buffer size) failed with status code:{0}", num1));
            }
            byte[] numArray = new byte[num];
            num1 = NCrypt.NCryptDecrypt(key.Handle, cipherText, (int)cipherText.Length, ref bCRYPTOAEPPADDINGINFO, numArray, num, out num, 4);
            if (num1 != 0)
            {
                throw new CryptographicException(string.Format("NCrypt.Decrypt() failed with status code:{0}", num1));
            }
            return(numArray);
        }
Example #3
0
        public static byte[] Decrypt(byte[] cipherText, CngKey key, CngAlgorithm hash)
        {
            var paddingInfo = new BCrypt.BCRYPT_OAEP_PADDING_INFO(hash.Algorithm);

            uint plainTextByteSize;
            uint status = NCrypt.NCryptDecrypt(key.Handle, cipherText, cipherText.Length, ref paddingInfo, null, 0, out plainTextByteSize, BCrypt.BCRYPT_PAD_OAEP);

            if (status != BCrypt.ERROR_SUCCESS)
            {
                throw new CryptographicException(string.Format("NCrypt.Decrypt() (plaintext buffer size) failed with status code:{0}", status));
            }

            var plainText = new byte[plainTextByteSize];

            status = NCrypt.NCryptDecrypt(key.Handle, cipherText, cipherText.Length, ref paddingInfo, plainText, plainTextByteSize, out plainTextByteSize, BCrypt.BCRYPT_PAD_OAEP);

            if (status != BCrypt.ERROR_SUCCESS)
            {
                throw new CryptographicException(string.Format("NCrypt.Decrypt() failed with status code:{0}", status));
            }

            return(plainText);
        }