public static byte[] Encrypt(byte[] plainText, CngKey key, CngAlgorithm hash) { uint num; BCrypt.BCRYPT_OAEP_PADDING_INFO bCRYPTOAEPPADDINGINFO = new BCrypt.BCRYPT_OAEP_PADDING_INFO(hash.Algorithm); uint num1 = NCrypt.NCryptEncrypt(key.Handle, plainText, (int)plainText.Length, ref bCRYPTOAEPPADDINGINFO, null, 0, out num, 4); if (num1 != 0) { throw new CryptographicException(string.Format("NCrypt.Encrypt() (ciphertext buffer size) failed with status code:{0}", num1)); } byte[] numArray = new byte[num]; num1 = NCrypt.NCryptEncrypt(key.Handle, plainText, (int)plainText.Length, ref bCRYPTOAEPPADDINGINFO, numArray, num, out num, 4); if (num1 != 0) { throw new CryptographicException(string.Format("NCrypt.Encrypt() failed with status code:{0}", num1)); } return(numArray); }
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); }