/// <summary> /// Encrypt raw data, generally used for encrypting symmetric key material. /// </summary> /// <remarks> /// This method can only encrypt (keySize - 88 bits) of data, so should not be used for encrypting /// arbitrary byte arrays. Instead, encrypt a symmetric key with this method, and use the symmetric /// key to encrypt the sensitive data. /// </remarks> /// <param name="rgb">raw data to encrypt</param> /// <param name="fOAEP">true to use OAEP padding (PKCS #1 v2), false to use PKCS #1 type 2 padding</param> /// <returns>Encrypted key</returns> public byte[] Encrypt(byte[] rgb, bool fOAEP) { if (rgb == null) { throw new ArgumentNullException(nameof(rgb)); } byte[] encryptedKey = null; CapiHelper.EncryptKey(SafeKeyHandle, rgb, rgb.Length, fOAEP, ref encryptedKey); return(encryptedKey); }
/// <summary> /// Encrypt raw data, generally used for encrypting symmetric key material. /// </summary> /// <remarks> /// This method can only encrypt (keySize - 88 bits) of data, so should not be used for encrypting /// arbitrary byte arrays. Instead, encrypt a symmetric key with this method, and use the symmetric /// key to encrypt the sensitive data. /// </remarks> /// <param name="rgb">raw data to encryt</param> /// <param name="fOAEP">true to use OAEP padding (PKCS #1 v2), false to use PKCS #1 type 2 padding</param> /// <returns>Encrypted key</returns> public byte[] Encrypt(byte[] rgb, bool fOAEP) { if (rgb == null) { throw new ArgumentNullException("rgb"); } GetKeyPair(); byte[] encryptedKey = null; CapiHelper.EncryptKey(_safeKeyHandle, rgb, rgb.Length, fOAEP, ref encryptedKey); return(encryptedKey); }
/// <summary> /// Encrypt raw data, generally used for encrypting symmetric key material. /// </summary> /// <remarks> /// This method can only encrypt (keySize - 88 bits) of data, so should not be used for encrypting /// arbitrary byte arrays. Instead, encrypt a symmetric key with this method, and use the symmetric /// key to encrypt the sensitive data. /// </remarks> /// <param name="rgb">raw data to encrypt</param> /// <param name="fOAEP">true to use OAEP padding (PKCS #1 v2), false to use PKCS #1 type 2 padding</param> /// <returns>Encrypted key</returns> public byte[] Encrypt(byte[] rgb, bool fOAEP) { ArgumentNullException.ThrowIfNull(rgb); if (fOAEP) { int rsaSize = (KeySize + 7) / 8; const int OaepSha1Overhead = 20 + 20 + 2; // Normalize the Windows 7 and Windows 8.1+ exception if (rsaSize - OaepSha1Overhead < rgb.Length) { const int NTE_BAD_LENGTH = unchecked ((int)0x80090004); throw NTE_BAD_LENGTH.ToCryptographicException(); } } byte[]? encryptedKey = null; CapiHelper.EncryptKey(SafeKeyHandle, rgb, rgb.Length, fOAEP, ref encryptedKey); return(encryptedKey); }