Exemplo n.º 1
0
 internal static extern uint NCryptEncrypt(SafeNCryptKeyHandle hKey,
                                           byte[] pbInput,
                                           int cbInput,
                                           ref BCrypt.BCRYPT_OAEP_PADDING_INFO pvPadding,
                                           byte[] pbOutput,
                                           uint cbOutput,
                                           out uint pcbResult,
                                           uint dwFlags);
Exemplo n.º 2
0
        public static byte[] Encrypt(byte[] plainText, CngKey key, CngAlgorithm hash)
        {
            var paddingInfo = new BCrypt.BCRYPT_OAEP_PADDING_INFO(hash.Algorithm);

            uint cipherTextByteSize;
            uint status = NCrypt.NCryptEncrypt(key.Handle, plainText, plainText.Length, ref paddingInfo, null, 0, out cipherTextByteSize, BCrypt.BCRYPT_PAD_OAEP);

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

            var cipherText = new byte[cipherTextByteSize];

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

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

            return cipherText;
        }