mscorlib(4.0.0.0) API with additions
RSA.cs
1 using System.IO;
3 using System.Security.Util;
4 using System.Text;
5 
7 {
9  [ComVisible(true)]
10  public abstract class RSA : AsymmetricAlgorithm
11  {
14  public override string KeyExchangeAlgorithm => "RSA";
15 
18  public override string SignatureAlgorithm => "RSA";
19 
22  public new static RSA Create()
23  {
24  return Create("System.Security.Cryptography.RSA");
25  }
26 
30  public new static RSA Create(string algName)
31  {
32  return (RSA)CryptoConfig.CreateFromName(algName);
33  }
34 
40  public virtual byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
41  {
42  throw DerivedClassMustOverride();
43  }
44 
50  public virtual byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
51  {
52  throw DerivedClassMustOverride();
53  }
54 
61  public virtual byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
62  {
63  throw DerivedClassMustOverride();
64  }
65 
74  public virtual bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
75  {
76  throw DerivedClassMustOverride();
77  }
78 
86  protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
87  {
88  throw DerivedClassMustOverride();
89  }
90 
96  protected virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
97  {
98  throw DerivedClassMustOverride();
99  }
100 
111  public byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
112  {
113  if (data == null)
114  {
115  throw new ArgumentNullException("data");
116  }
117  return SignData(data, 0, data.Length, hashAlgorithm, padding);
118  }
119 
136  public virtual byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
137  {
138  if (data == null)
139  {
140  throw new ArgumentNullException("data");
141  }
142  if (offset < 0 || offset > data.Length)
143  {
144  throw new ArgumentOutOfRangeException("offset");
145  }
146  if (count < 0 || count > data.Length - offset)
147  {
148  throw new ArgumentOutOfRangeException("count");
149  }
150  if (string.IsNullOrEmpty(hashAlgorithm.Name))
151  {
152  throw HashAlgorithmNameNullOrEmpty();
153  }
154  if (padding == null)
155  {
156  throw new ArgumentNullException("padding");
157  }
158  byte[] hash = HashData(data, offset, count, hashAlgorithm);
159  return SignHash(hash, hashAlgorithm, padding);
160  }
161 
172  public virtual byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
173  {
174  if (data == null)
175  {
176  throw new ArgumentNullException("data");
177  }
178  if (string.IsNullOrEmpty(hashAlgorithm.Name))
179  {
180  throw HashAlgorithmNameNullOrEmpty();
181  }
182  if (padding == null)
183  {
184  throw new ArgumentNullException("padding");
185  }
186  byte[] hash = HashData(data, hashAlgorithm);
187  return SignHash(hash, hashAlgorithm, padding);
188  }
189 
203  public bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
204  {
205  if (data == null)
206  {
207  throw new ArgumentNullException("data");
208  }
209  return VerifyData(data, 0, data.Length, signature, hashAlgorithm, padding);
210  }
211 
231  public virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
232  {
233  if (data == null)
234  {
235  throw new ArgumentNullException("data");
236  }
237  if (offset < 0 || offset > data.Length)
238  {
239  throw new ArgumentOutOfRangeException("offset");
240  }
241  if (count < 0 || count > data.Length - offset)
242  {
243  throw new ArgumentOutOfRangeException("count");
244  }
245  if (signature == null)
246  {
247  throw new ArgumentNullException("signature");
248  }
249  if (string.IsNullOrEmpty(hashAlgorithm.Name))
250  {
251  throw HashAlgorithmNameNullOrEmpty();
252  }
253  if (padding == null)
254  {
255  throw new ArgumentNullException("padding");
256  }
257  byte[] hash = HashData(data, offset, count, hashAlgorithm);
258  return VerifyHash(hash, signature, hashAlgorithm, padding);
259  }
260 
274  public bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
275  {
276  if (data == null)
277  {
278  throw new ArgumentNullException("data");
279  }
280  if (signature == null)
281  {
282  throw new ArgumentNullException("signature");
283  }
284  if (string.IsNullOrEmpty(hashAlgorithm.Name))
285  {
286  throw HashAlgorithmNameNullOrEmpty();
287  }
288  if (padding == null)
289  {
290  throw new ArgumentNullException("padding");
291  }
292  byte[] hash = HashData(data, hashAlgorithm);
293  return VerifyHash(hash, signature, hashAlgorithm, padding);
294  }
295 
296  private static Exception DerivedClassMustOverride()
297  {
298  return new NotImplementedException(Environment.GetResourceString("NotSupported_SubclassOverride"));
299  }
300 
301  internal static Exception HashAlgorithmNameNullOrEmpty()
302  {
303  return new ArgumentException(Environment.GetResourceString("Cryptography_HashAlgorithmNameNullOrEmpty"), "hashAlgorithm");
304  }
305 
310  public virtual byte[] DecryptValue(byte[] rgb)
311  {
312  throw new NotSupportedException(Environment.GetResourceString("NotSupported_Method"));
313  }
314 
319  public virtual byte[] EncryptValue(byte[] rgb)
320  {
321  throw new NotSupportedException(Environment.GetResourceString("NotSupported_Method"));
322  }
323 
328  public override void FromXmlString(string xmlString)
329  {
330  if (xmlString == null)
331  {
332  throw new ArgumentNullException("xmlString");
333  }
334  RSAParameters parameters = default(RSAParameters);
335  Parser parser = new Parser(xmlString);
336  SecurityElement topElement = parser.GetTopElement();
337  string text = topElement.SearchForTextOfLocalName("Modulus");
338  if (text == null)
339  {
340  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "RSA", "Modulus"));
341  }
342  parameters.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text));
343  string text2 = topElement.SearchForTextOfLocalName("Exponent");
344  if (text2 == null)
345  {
346  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "RSA", "Exponent"));
347  }
348  parameters.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text2));
349  string text3 = topElement.SearchForTextOfLocalName("P");
350  if (text3 != null)
351  {
352  parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text3));
353  }
354  string text4 = topElement.SearchForTextOfLocalName("Q");
355  if (text4 != null)
356  {
357  parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text4));
358  }
359  string text5 = topElement.SearchForTextOfLocalName("DP");
360  if (text5 != null)
361  {
362  parameters.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text5));
363  }
364  string text6 = topElement.SearchForTextOfLocalName("DQ");
365  if (text6 != null)
366  {
367  parameters.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text6));
368  }
369  string text7 = topElement.SearchForTextOfLocalName("InverseQ");
370  if (text7 != null)
371  {
372  parameters.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text7));
373  }
374  string text8 = topElement.SearchForTextOfLocalName("D");
375  if (text8 != null)
376  {
377  parameters.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text8));
378  }
379  ImportParameters(parameters);
380  }
381 
386  public override string ToXmlString(bool includePrivateParameters)
387  {
388  RSAParameters rSAParameters = ExportParameters(includePrivateParameters);
389  StringBuilder stringBuilder = new StringBuilder();
390  stringBuilder.Append("<RSAKeyValue>");
391  stringBuilder.Append("<Modulus>" + Convert.ToBase64String(rSAParameters.Modulus) + "</Modulus>");
392  stringBuilder.Append("<Exponent>" + Convert.ToBase64String(rSAParameters.Exponent) + "</Exponent>");
393  if (includePrivateParameters)
394  {
395  stringBuilder.Append("<P>" + Convert.ToBase64String(rSAParameters.P) + "</P>");
396  stringBuilder.Append("<Q>" + Convert.ToBase64String(rSAParameters.Q) + "</Q>");
397  stringBuilder.Append("<DP>" + Convert.ToBase64String(rSAParameters.DP) + "</DP>");
398  stringBuilder.Append("<DQ>" + Convert.ToBase64String(rSAParameters.DQ) + "</DQ>");
399  stringBuilder.Append("<InverseQ>" + Convert.ToBase64String(rSAParameters.InverseQ) + "</InverseQ>");
400  stringBuilder.Append("<D>" + Convert.ToBase64String(rSAParameters.D) + "</D>");
401  }
402  stringBuilder.Append("</RSAKeyValue>");
403  return stringBuilder.ToString();
404  }
405 
410  public abstract RSAParameters ExportParameters(bool includePrivateParameters);
411 
414  public abstract void ImportParameters(RSAParameters parameters);
415  }
416 }
The exception that is thrown when an error occurs during a cryptographic operation.
Converts a base data type to another base data type.
Definition: Convert.cs:10
override void FromXmlString(string xmlString)
Initializes an T:System.Security.Cryptography.RSA object from the key information from an XML string.
Definition: RSA.cs:328
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual byte [] EncryptValue(byte[] rgb)
When overridden in a derived class, encrypts the input data using the public key.
Definition: RSA.cs:319
virtual 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...
Definition: RSA.cs:96
static new RSA Create(string algName)
Creates an instance of the specified implementation of T:System.Security.Cryptography....
Definition: RSA.cs:30
byte [] SignData(byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Computes the hash value of the specified byte array using the specified hash algorithm and padding mo...
Definition: RSA.cs:111
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
byte [] Modulus
Represents the Modulus parameter for the T:System.Security.Cryptography.RSA algorithm.
string Name
Gets the underlying string representation of the algorithm name.
virtual byte [] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Computes the hash value of a portion of the specified byte array using the specified hash algorithm a...
Definition: RSA.cs:136
static object CreateFromName(string name, params object[] args)
Creates a new instance of the specified cryptographic object with the specified arguments.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
byte [] D
Represents the D parameter for the T:System.Security.Cryptography.RSA algorithm.
bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Verifies that a digital signature is valid by calculating the hash value of the specified stream usin...
Definition: RSA.cs:274
Specifies the name of a cryptographic hash algorithm.
static string ToBase64String(byte[] inArray)
Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded ...
Definition: Convert.cs:4413
Specifies the padding mode and parameters to use with RSA encryption or decryption operations.
virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Verifies that a digital signature is valid by calculating the hash value of the data in a portion of ...
Definition: RSA.cs:231
virtual 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...
Definition: RSA.cs:74
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual byte [] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
When overridden in a derived class, computes the signature for the specified hash value by encrypting...
Definition: RSA.cs:61
abstract RSAParameters ExportParameters(bool includePrivateParameters)
When overridden in a derived class, exports the T:System.Security.Cryptography.RSAParameters.
Represents the standard parameters for the T:System.Security.Cryptography.RSA algorithm.
Definition: RSAParameters.cs:8
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
static new RSA Create()
Creates an instance of the default implementation of the T:System.Security.Cryptography....
Definition: RSA.cs:22
override string ToXmlString(bool includePrivateParameters)
Creates and returns an XML string containing the key of the current T:System.Security....
Definition: RSA.cs:386
override string SignatureAlgorithm
Gets the name of the signature algorithm available with this implementation of T:System....
Definition: RSA.cs:18
Accesses the cryptography configuration information.
Definition: CryptoConfig.cs:17
Represents the XML object model for encoding security objects. This class cannot be inherited.
byte [] DQ
Represents the DQ parameter for the T:System.Security.Cryptography.RSA algorithm.
virtual byte [] Encrypt(byte[] data, RSAEncryptionPadding padding)
When overridden in a derived class, encrypts the input data using the specified padding mode.
Definition: RSA.cs:40
override string KeyExchangeAlgorithm
Gets the name of the key exchange algorithm available with this implementation of T:System....
Definition: RSA.cs:14
byte [] P
Represents the P parameter for the T:System.Security.Cryptography.RSA algorithm.
Represents the abstract base class from which all implementations of asymmetric algorithms must inher...
virtual byte [] Decrypt(byte[] data, RSAEncryptionPadding padding)
When overridden in a derived class, decrypts the input data using the specified padding mode.
Definition: RSA.cs:50
byte [] InverseQ
Represents the InverseQ parameter for the T:System.Security.Cryptography.RSA algorithm.
virtual byte [] SignData(Stream data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Computes the hash value of the specified stream using the specified hash algorithm and padding mode,...
Definition: RSA.cs:172
Specifies the padding mode and parameters to use with RSA signature creation or verification operatio...
virtual 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...
Definition: RSA.cs:86
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
The exception that is thrown when one of the arguments provided to a method is not valid.
abstract void ImportParameters(RSAParameters parameters)
When overridden in a derived class, imports the specified T:System.Security.Cryptography....
bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Verifies that a digital signature is valid by calculating the hash value of the specified data using ...
Definition: RSA.cs:203
byte [] Q
Represents the Q parameter for the T:System.Security.Cryptography.RSA algorithm.
static unsafe byte [] FromBase64String(string s)
Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit un...
Definition: Convert.cs:4692
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
Represents the base class from which all implementations of the T:System.Security....
Definition: RSA.cs:10
byte [] DP
Represents the DP parameter for the T:System.Security.Cryptography.RSA algorithm.
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
The exception that is thrown when a requested method or operation is not implemented.
byte [] Exponent
Represents the Exponent parameter for the T:System.Security.Cryptography.RSA algorithm.
virtual byte [] DecryptValue(byte[] rgb)
When overridden in a derived class, decrypts the input data using the private key.
Definition: RSA.cs:310
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16