2 // System.Security.Cryptography.RSA.cs
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot (sebastien@ximian.com)
9 // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2006,2008 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
33 using System.Runtime.InteropServices;
38 namespace System.Security.Cryptography {
41 public abstract class RSA : AsymmetricAlgorithm {
43 public static new RSA Create ()
46 return new System.Security.Cryptography.RSACryptoServiceProvider ();
48 return Create ("System.Security.Cryptography.RSA");
52 public static new RSA Create (string algName)
54 return (RSA) CryptoConfig.CreateFromName (algName);
61 public abstract byte[] EncryptValue (byte[] rgb);
63 public abstract byte[] DecryptValue (byte[] rgb);
65 public abstract RSAParameters ExportParameters (bool includePrivateParameters);
67 public abstract void ImportParameters (RSAParameters parameters);
69 internal void ZeroizePrivateKey (RSAParameters parameters)
71 if (parameters.P != null)
72 Array.Clear (parameters.P, 0, parameters.P.Length);
73 if (parameters.Q != null)
74 Array.Clear (parameters.Q, 0, parameters.Q.Length);
75 if (parameters.DP != null)
76 Array.Clear (parameters.DP, 0, parameters.DP.Length);
77 if (parameters.DQ != null)
78 Array.Clear (parameters.DQ, 0, parameters.DQ.Length);
79 if (parameters.InverseQ != null)
80 Array.Clear (parameters.InverseQ, 0, parameters.InverseQ.Length);
81 if (parameters.D != null)
82 Array.Clear (parameters.D, 0, parameters.D.Length);
85 public override void FromXmlString (string xmlString)
87 if (xmlString == null)
88 throw new ArgumentNullException ("xmlString");
90 RSAParameters rsaParams = new RSAParameters ();
92 rsaParams.P = GetNamedParam (xmlString, "P");
93 rsaParams.Q = GetNamedParam (xmlString, "Q");
94 rsaParams.D = GetNamedParam (xmlString, "D");
95 rsaParams.DP = GetNamedParam (xmlString, "DP");
96 rsaParams.DQ = GetNamedParam (xmlString, "DQ");
97 rsaParams.InverseQ = GetNamedParam (xmlString, "InverseQ");
98 rsaParams.Exponent = GetNamedParam (xmlString, "Exponent");
99 rsaParams.Modulus = GetNamedParam (xmlString, "Modulus");
100 ImportParameters (rsaParams);
102 catch (Exception e) {
103 ZeroizePrivateKey (rsaParams);
104 throw new CryptographicException (
105 Locale.GetText ("Couldn't decode XML"), e);
108 ZeroizePrivateKey (rsaParams);
112 public override string ToXmlString (bool includePrivateParameters)
114 StringBuilder sb = new StringBuilder ();
115 RSAParameters rsaParams = ExportParameters (includePrivateParameters);
117 sb.Append ("<RSAKeyValue>");
119 sb.Append ("<Modulus>");
120 sb.Append (Convert.ToBase64String (rsaParams.Modulus));
121 sb.Append ("</Modulus>");
123 sb.Append ("<Exponent>");
124 sb.Append (Convert.ToBase64String (rsaParams.Exponent));
125 sb.Append ("</Exponent>");
127 if (includePrivateParameters) {
128 // we want an ArgumentNullException is only the D is missing, but a
129 // CryptographicException if other parameters (CRT) are missings
130 if (rsaParams.D == null) {
131 string msg = Locale.GetText ("Missing D parameter for the private key.");
132 throw new ArgumentNullException (msg);
133 } else if ((rsaParams.P == null) || (rsaParams.Q == null) || (rsaParams.DP == null) ||
134 (rsaParams.DQ == null) || (rsaParams.InverseQ == null)) {
135 // note: we can import a private key, using FromXmlString,
136 // without the CRT parameters but we export it using ToXmlString!
137 string msg = Locale.GetText ("Missing some CRT parameters for the private key.");
138 throw new CryptographicException (msg);
142 sb.Append (Convert.ToBase64String (rsaParams.P));
146 sb.Append (Convert.ToBase64String (rsaParams.Q));
150 sb.Append (Convert.ToBase64String (rsaParams.DP));
154 sb.Append (Convert.ToBase64String (rsaParams.DQ));
157 sb.Append ("<InverseQ>");
158 sb.Append (Convert.ToBase64String (rsaParams.InverseQ));
159 sb.Append ("</InverseQ>");
162 sb.Append (Convert.ToBase64String (rsaParams.D));
166 sb.Append ("</RSAKeyValue>");
169 ZeroizePrivateKey (rsaParams);
173 return sb.ToString ();