Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }