13 private int _dwKeySize;
17 private bool _randomKeyContainer;
20 private SafeProvHandle _safeProvHandle;
23 private SafeKeyHandle _safeKeyHandle;
33 [SecuritySafeCritical]
37 byte[] array = Utils._GetKeyParameter(_safeKeyHandle, 2u);
47 [SecuritySafeCritical]
59 [SecuritySafeCritical]
63 byte[] array = Utils._GetKeyParameter(_safeKeyHandle, 1u);
64 _dwKeySize = (array[0] | (array[1] << 8) | (array[2] << 16) | (array[3] << 24));
77 return "RSA-PKCS1-KeyEx";
107 [SecuritySafeCritical]
110 if (_safeProvHandle ==
null)
114 if (_safeProvHandle ==
null)
116 _safeProvHandle = Utils.CreateProvHandle(_parameters, _randomKeyContainer);
120 return Utils.GetPersistKeyInCsp(_safeProvHandle);
122 [SecuritySafeCritical]
126 if (value == persistKeyInCsp)
130 if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
143 keyContainerPermission.
Demand();
145 Utils.SetPersistKeyInCsp(_safeProvHandle, value);
151 [SuppressUnmanagedCodeSecurity]
152 private static extern void DecryptKey(SafeKeyHandle pKeyContext, [MarshalAs(
UnmanagedType.LPArray)]
byte[] pbEncryptedKey,
int cbEncryptedKey, [MarshalAs(
UnmanagedType.Bool)]
bool fOAEP, ObjectHandleOnStack ohRetDecryptedKey);
156 [SuppressUnmanagedCodeSecurity]
157 private static extern void EncryptKey(SafeKeyHandle pKeyContext, [MarshalAs(
UnmanagedType.LPArray)]
byte[] pbKey,
int cbKey, [MarshalAs(
UnmanagedType.Bool)]
bool fOAEP, ObjectHandleOnStack ohRetEncryptedKey);
161 [SecuritySafeCritical]
163 : this(0, new
CspParameters(24, null, null, s_UseMachineKeyStore), useDefaultKeySize: true)
170 [SecuritySafeCritical]
172 : this(dwKeySize, new
CspParameters(24, null, null, s_UseMachineKeyStore), useDefaultKeySize: false)
179 [SecuritySafeCritical]
181 : this(0, parameters, useDefaultKeySize: true)
189 [SecuritySafeCritical]
191 : this(dwKeySize, parameters, useDefaultKeySize: false)
202 _parameters = Utils.SaveCspParameters(CspAlgorithmType.Rsa, parameters, s_UseMachineKeyStore, ref _randomKeyContainer);
205 new KeySizes(384, 16384, 8)
207 _dwKeySize = (useDefaultKeySize ? 1024 : dwKeySize);
208 if (!_randomKeyContainer ||
Environment.GetCompatibilityFlag(CompatibilityFlag.EagerlyGenerateRandomAsymmKeys))
215 private void GetKeyPair()
217 if (_safeKeyHandle ==
null)
221 if (_safeKeyHandle ==
null)
223 Utils.GetKeyPairHelper(CspAlgorithmType.Rsa, _parameters, _randomKeyContainer, _dwKeySize, ref _safeProvHandle, ref _safeKeyHandle);
229 [SecuritySafeCritical]
230 protected override void Dispose(
bool disposing)
232 base.Dispose(disposing);
233 if (_safeKeyHandle !=
null && !_safeKeyHandle.IsClosed)
235 _safeKeyHandle.Dispose();
237 if (_safeProvHandle !=
null && !_safeProvHandle.IsClosed)
239 _safeProvHandle.Dispose();
248 [SecuritySafeCritical]
252 if (includePrivateParameters && !CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
257 keyContainerPermission.
Demand();
259 RSACspObject rSACspObject =
new RSACspObject();
260 int blobType = includePrivateParameters ? 7 : 6;
261 Utils._ExportKey(_safeKeyHandle, blobType, rSACspObject);
262 return RSAObjectToStruct(rSACspObject);
269 [SecuritySafeCritical]
274 return Utils.ExportCspBlobHelper(includePrivateParameters, _parameters, _safeKeyHandle);
280 [SecuritySafeCritical]
283 if (_safeKeyHandle !=
null && !_safeKeyHandle.IsClosed)
285 _safeKeyHandle.Dispose();
286 _safeKeyHandle =
null;
288 RSACspObject cspObject = RSAStructToObject(parameters);
289 _safeKeyHandle = SafeKeyHandle.InvalidHandle;
290 if (IsPublic(parameters))
292 Utils._ImportKey(Utils.StaticProvHandle, 41984,
CspProviderFlags.NoFlags, cspObject, ref _safeKeyHandle);
295 if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
300 keyContainerPermission.
Demand();
302 if (_safeProvHandle ==
null)
304 _safeProvHandle = Utils.CreateProvHandle(_parameters, _randomKeyContainer);
306 Utils._ImportKey(_safeProvHandle, 41984, _parameters.
Flags, cspObject, ref _safeKeyHandle);
311 [SecuritySafeCritical]
315 Utils.ImportCspBlobHelper(CspAlgorithmType.Rsa, keyBlob, IsPublic(keyBlob), ref _parameters, _randomKeyContainer, ref _safeProvHandle, ref _safeKeyHandle);
326 int calgHash = Utils.ObjToAlgId(halg,
OidGroup.HashAlgorithm);
327 HashAlgorithm hashAlgorithm = Utils.ObjToHashAlgorithm(halg);
328 byte[] rgbHash = hashAlgorithm.
ComputeHash(inputStream);
340 int calgHash = Utils.ObjToAlgId(halg,
OidGroup.HashAlgorithm);
341 HashAlgorithm hashAlgorithm = Utils.ObjToHashAlgorithm(halg);
342 byte[] rgbHash = hashAlgorithm.
ComputeHash(buffer);
354 public byte[]
SignData(
byte[] buffer,
int offset,
int count,
object halg)
356 int calgHash = Utils.ObjToAlgId(halg,
OidGroup.HashAlgorithm);
357 HashAlgorithm hashAlgorithm = Utils.ObjToHashAlgorithm(halg);
358 byte[] rgbHash = hashAlgorithm.
ComputeHash(buffer, offset, count);
370 public bool VerifyData(
byte[] buffer,
object halg,
byte[] signature)
372 int calgHash = Utils.ObjToAlgId(halg,
OidGroup.HashAlgorithm);
373 HashAlgorithm hashAlgorithm = Utils.ObjToHashAlgorithm(halg);
374 byte[] rgbHash = hashAlgorithm.
ComputeHash(buffer);
375 return VerifyHash(rgbHash, calgHash, signature);
394 int calgHash = X509Utils.NameOrOidToAlgId(str,
OidGroup.HashAlgorithm);
398 [SecuritySafeCritical]
399 internal byte[]
SignHash(
byte[] rgbHash,
int calgHash)
407 keyContainerPermission.
Demand();
409 return Utils.SignValue(_safeKeyHandle, _parameters.
KeyNumber, 9216, calgHash, rgbHash);
420 public bool VerifyHash(
byte[] rgbHash,
string str,
byte[] rgbSignature)
426 if (rgbSignature ==
null)
430 int calgHash = X509Utils.NameOrOidToAlgId(str,
OidGroup.HashAlgorithm);
431 return VerifyHash(rgbHash, calgHash, rgbSignature);
434 [SecuritySafeCritical]
435 internal bool VerifyHash(
byte[] rgbHash,
int calgHash,
byte[] rgbSignature)
438 return Utils.VerifySign(_safeKeyHandle, 9216, calgHash, rgbHash, rgbSignature);
449 [SecuritySafeCritical]
458 EncryptKey(_safeKeyHandle, rgb, rgb.Length, fOAEP, JitHelpers.GetObjectHandleOnStack(ref o));
470 [SecuritySafeCritical]
487 keyContainerPermission.
Demand();
490 DecryptKey(_safeKeyHandle, rgb, rgb.Length, fOAEP, JitHelpers.GetObjectHandleOnStack(ref o));
512 private static RSAParameters RSAObjectToStruct(RSACspObject rsaCspObject)
515 result.
Exponent = rsaCspObject.Exponent;
516 result.
Modulus = rsaCspObject.Modulus;
517 result.
P = rsaCspObject.P;
518 result.
Q = rsaCspObject.Q;
519 result.
DP = rsaCspObject.DP;
520 result.
DQ = rsaCspObject.DQ;
521 result.
InverseQ = rsaCspObject.InverseQ;
522 result.
D = rsaCspObject.D;
526 private static RSACspObject RSAStructToObject(RSAParameters rsaParams)
528 RSACspObject rSACspObject =
new RSACspObject();
529 rSACspObject.Exponent = rsaParams.Exponent;
530 rSACspObject.Modulus = rsaParams.Modulus;
531 rSACspObject.P = rsaParams.P;
532 rSACspObject.Q = rsaParams.Q;
533 rSACspObject.DP = rsaParams.DP;
534 rSACspObject.DQ = rsaParams.DQ;
535 rSACspObject.InverseQ = rsaParams.InverseQ;
536 rSACspObject.D = rsaParams.D;
540 private static bool IsPublic(
byte[] keyBlob)
550 if (keyBlob[11] != 49 || keyBlob[10] != 65 || keyBlob[9] != 83 || keyBlob[8] != 82)
557 private static bool IsPublic(RSAParameters rsaParams)
559 return rsaParams.P ==
null;
562 [SecuritySafeCritical]
565 using (SafeHashHandle hHash = Utils.CreateHash(Utils.StaticProvHandle, GetAlgorithmId(hashAlgorithm)))
567 Utils.HashData(hHash, data, offset, count);
568 return Utils.EndHash(hHash);
572 [SecuritySafeCritical]
575 using (SafeHashHandle hHash = Utils.CreateHash(Utils.StaticProvHandle, GetAlgorithmId(hashAlgorithm)))
577 byte[] array =
new byte[4096];
581 num = data.Read(array, 0, array.Length);
584 Utils.HashData(hHash, array, 0, num);
588 return Utils.EndHash(hHash);
594 switch (hashAlgorithm.
Name)
631 return Encrypt(data, fOAEP:
false);
635 return Encrypt(data, fOAEP:
true);
637 throw PaddingModeNotSupported();
660 return Decrypt(data, fOAEP:
false);
664 return Decrypt(data, fOAEP:
true);
666 throw PaddingModeNotSupported();
687 if (
string.IsNullOrEmpty(hashAlgorithm.
Name))
689 throw RSA.HashAlgorithmNameNullOrEmpty();
697 throw PaddingModeNotSupported();
699 return SignHash(hash, GetAlgorithmId(hashAlgorithm));
722 if (signature ==
null)
726 if (
string.IsNullOrEmpty(hashAlgorithm.
Name))
728 throw RSA.HashAlgorithmNameNullOrEmpty();
736 throw PaddingModeNotSupported();
738 return VerifyHash(hash, GetAlgorithmId(hashAlgorithm), signature);
741 private static Exception PaddingModeNotSupported()
The exception that is thrown when an error occurs during a cryptographic operation.
override int KeySize
Gets the size of the current key.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Performs asymmetric encryption and decryption using the implementation of the T:System....
static bool? UseMachineKeyStore
Gets or sets a value indicating whether the key should be persisted in the computer's key store inste...
byte [] Modulus
Represents the Modulus parameter for the T:System.Security.Cryptography.RSA algorithm.
override byte [] HashData(Stream data, HashAlgorithmName hashAlgorithm)
When overridden in a derived class, computes the hash value of a specified binary stream by using a s...
void ImportCspBlob(byte[] keyBlob)
Imports a blob that represents RSA key information.
int KeyNumber
Specifies whether an asymmetric key is created as a signature key or an exchange key.
string Name
Gets the underlying string representation of the algorithm name.
byte [] Encrypt(byte[] rgb, bool fOAEP)
Encrypts data with the T:System.Security.Cryptography.RSA algorithm.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Specifies access rights for specific key containers. This class cannot be inherited.
CspProviderFlags
Specifies flags that modify the behavior of the cryptographic service providers (CSP).
byte [] D
Represents the D parameter for the T:System.Security.Cryptography.RSA algorithm.
bool VerifyHash(byte[] rgbHash, string str, byte[] rgbSignature)
Verifies that a digital signature is valid by determining the hash value in the signature using the p...
OidGroup
Identifies Windows cryptographic object identifier (OID) groups.
Controls the ability to access key containers. This class cannot be inherited.
override byte [] EncryptValue(byte[] rgb)
This method is not supported in the current version.
override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Security.Cryptography.AsymmetricAlgorithm class...
Defines methods that allow an T:System.Security.Cryptography.AsymmetricAlgorithm class to enumerate k...
RSACryptoServiceProvider(CspParameters parameters)
Initializes a new instance of the T:System.Security.Cryptography.RSACryptoServiceProvider class with ...
Specifies the name of a cryptographic hash algorithm.
bool PublicOnly
Gets a value that indicates whether the T:System.Security.Cryptography.RSACryptoServiceProvider objec...
bool PersistKeyInCsp
Gets or sets a value indicating whether the key should be persisted in the cryptographic service prov...
Specifies the padding mode and parameters to use with RSA encryption or decryption operations.
KeySizes [] LegalKeySizesValue
Specifies the key sizes that are supported by the asymmetric algorithm.
byte [] SignData(byte[] buffer, int offset, int count, object halg)
Computes the hash value of a subset of the specified byte array using the specified hash algorithm,...
override string SignatureAlgorithm
Gets the name of the signature algorithm available with this implementation of T:System....
Provides information about, and means to manipulate, the current environment and platform....
override byte [] Encrypt(byte[] data, RSAEncryptionPadding padding)
Encrypts data with the T:System.Security.Cryptography.RSA algorithm using the specified padding.
byte [] ExportCspBlob(bool includePrivateParameters)
Exports a blob containing the key information associated with an T:System.Security....
Represents the standard parameters for the T:System.Security.Cryptography.RSA algorithm.
static RSAEncryptionPadding OaepSHA1
Gets an object that represents the Optimal Asymmetric Encryption Padding (OAEP) encryption standard w...
RSACryptoServiceProvider(int dwKeySize, CspParameters parameters)
Initializes a new instance of the T:System.Security.Cryptography.RSACryptoServiceProvider class with ...
RSACryptoServiceProvider(int dwKeySize)
Initializes a new instance of the T:System.Security.Cryptography.RSACryptoServiceProvider class with ...
RSACryptoServiceProvider()
Initializes a new instance of the T:System.Security.Cryptography.RSACryptoServiceProvider class using...
override byte [] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Computes the signature for the specified hash value by encrypting it with the private key using the s...
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
byte [] DQ
Represents the DQ parameter for the T:System.Security.Cryptography.RSA algorithm.
byte [] SignHash(byte[] rgbHash, string str)
Computes the signature for the specified hash value by encrypting it with the private key.
int Add(KeyContainerPermissionAccessEntry accessEntry)
Adds a T:System.Security.Permissions.KeyContainerPermissionAccessEntry object to the collection.
KeyContainerPermissionAccessEntryCollection AccessEntries
Gets the collection of T:System.Security.Permissions.KeyContainerPermissionAccessEntry objects associ...
byte [] P
Represents the P parameter for the T:System.Security.Cryptography.RSA algorithm.
byte [] InverseQ
Represents the InverseQ parameter for the T:System.Security.Cryptography.RSA algorithm.
override byte [] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
When overridden in a derived class, computes the hash value of a specified portion of a byte array by...
override bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Verifies that a digital signature is valid by determining the hash value in the signature using the s...
Contains parameters that are passed to the cryptographic service provider (CSP) that performs cryptog...
Represents the base class from which all implementations of cryptographic hash algorithms must derive...
KeyContainerPermissionFlags
Specifies the type of key container access allowed.
Specifies the padding mode and parameters to use with RSA signature creation or verification operatio...
override byte [] Decrypt(byte[] data, RSAEncryptionPadding padding)
Decrypts data that was previously encrypted with the T:System.Security.Cryptography....
static RSASignaturePadding Pkcs1
Gets an object that uses the PKCS #1 v1.5 padding mode.
CharSet
Dictates which character set marshaled strings should use.
override byte [] DecryptValue(byte[] rgb)
This method is not supported in the current version.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
byte [] Q
Represents the Q parameter for the T:System.Security.Cryptography.RSA algorithm.
override RSAParameters ExportParameters(bool includePrivateParameters)
Exports the T:System.Security.Cryptography.RSAParameters.
byte [] SignData(Stream inputStream, object halg)
Computes the hash value of the specified input stream using the specified hash algorithm,...
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Represents the base class from which all implementations of the T:System.Security....
byte [] DP
Represents the DP parameter for the T:System.Security.Cryptography.RSA algorithm.
Provides additional information about a cryptographic key pair. This class cannot be inherited.
override void ImportParameters(RSAParameters parameters)
Imports the specified T:System.Security.Cryptography.RSAParameters.
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
bool VerifyData(byte[] buffer, object halg, byte[] signature)
Verifies that a digital signature is valid by determining the hash value in the signature using the p...
CspKeyContainerInfo CspKeyContainerInfo
Gets a T:System.Security.Cryptography.CspKeyContainerInfo object that describes additional informatio...
byte [] Decrypt(byte[] rgb, bool fOAEP)
Decrypts data with the T:System.Security.Cryptography.RSA algorithm.
override string KeyExchangeAlgorithm
Gets the name of the key exchange algorithm available with this implementation of T:System....
byte [] Exponent
Represents the Exponent parameter for the T:System.Security.Cryptography.RSA algorithm.
CspProviderFlags Flags
Represents the flags for T:System.Security.Cryptography.CspParameters that modify the behavior of the...
static RSAEncryptionPadding Pkcs1
Gets an object that represents the PKCS #1 encryption standard.
bool RandomlyGenerated
Gets a value indicating whether a key container was randomly generated by a managed cryptography clas...
byte [] ComputeHash(Stream inputStream)
Computes the hash value for the specified T:System.IO.Stream object.
byte [] SignData(byte[] buffer, object halg)
Computes the hash value of the specified byte array using the specified hash algorithm,...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....