**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Security.Cryptography / RSA.cs
blob593c3cf566b71f740512c2fb11591c8e05e2949f
1 //
2 // System.Security.Cryptography.RSA.cs
3 //
4 // Authors:
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // (C) 2002
9 // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // (C) 2004 Novell (http://www.novell.com)
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 //
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 //
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System;
37 using System.Globalization;
38 using System.Text;
40 using Mono.Xml;
42 namespace System.Security.Cryptography {
44 public abstract class RSA : AsymmetricAlgorithm {
46 public static new RSA Create ()
48 return Create ("System.Security.Cryptography.RSA");
51 public static new RSA Create (string algName)
53 return (RSA) CryptoConfig.CreateFromName (algName);
56 public RSA ()
60 public abstract byte[] EncryptValue (byte[] rgb);
62 public abstract byte[] DecryptValue (byte[] rgb);
64 public abstract RSAParameters ExportParameters (bool include);
66 public abstract void ImportParameters (RSAParameters parameters);
68 internal void ZeroizePrivateKey (RSAParameters parameters)
70 if (parameters.P != null)
71 Array.Clear (parameters.P, 0, parameters.P.Length);
72 if (parameters.Q != null)
73 Array.Clear (parameters.Q, 0, parameters.Q.Length);
74 if (parameters.DP != null)
75 Array.Clear (parameters.DP, 0, parameters.DP.Length);
76 if (parameters.DQ != null)
77 Array.Clear (parameters.DQ, 0, parameters.DQ.Length);
78 if (parameters.InverseQ != null)
79 Array.Clear (parameters.InverseQ, 0, parameters.InverseQ.Length);
80 if (parameters.D != null)
81 Array.Clear (parameters.D, 0, parameters.D.Length);
84 private byte[] GetNamedParam (SecurityElement se, string param)
86 SecurityElement sep = se.SearchForChildByTag (param);
87 if (sep == null)
88 return null;
89 return Convert.FromBase64String (sep.Text);
92 public override void FromXmlString (string xmlString)
94 if (xmlString == null)
95 throw new ArgumentNullException ("xmlString");
97 RSAParameters rsaParams = new RSAParameters ();
98 try {
99 SecurityParser sp = new SecurityParser ();
100 sp.LoadXml (xmlString);
101 SecurityElement se = sp.ToXml ();
103 rsaParams.P = GetNamedParam (se, "P");
104 rsaParams.Q = GetNamedParam (se, "Q");
105 rsaParams.D = GetNamedParam (se, "D");
106 rsaParams.DP = GetNamedParam (se, "DP");
107 rsaParams.DQ = GetNamedParam (se, "DQ");
108 rsaParams.InverseQ = GetNamedParam (se, "InverseQ");
109 rsaParams.Exponent = GetNamedParam (se, "Exponent");
110 rsaParams.Modulus = GetNamedParam (se, "Modulus");
111 ImportParameters (rsaParams);
113 catch (Exception e) {
114 ZeroizePrivateKey (rsaParams);
115 throw new CryptographicException (
116 Locale.GetText ("Couldn't decode XML"), e);
118 finally {
119 ZeroizePrivateKey (rsaParams);
123 public override string ToXmlString (bool includePrivateParameters)
125 StringBuilder sb = new StringBuilder ();
126 RSAParameters rsaParams = ExportParameters (includePrivateParameters);
127 try {
128 sb.Append ("<RSAKeyValue>");
130 sb.Append ("<Modulus>");
131 sb.Append (Convert.ToBase64String (rsaParams.Modulus));
132 sb.Append ("</Modulus>");
134 sb.Append ("<Exponent>");
135 sb.Append (Convert.ToBase64String (rsaParams.Exponent));
136 sb.Append ("</Exponent>");
138 if (includePrivateParameters)
140 sb.Append ("<P>");
141 sb.Append (Convert.ToBase64String (rsaParams.P));
142 sb.Append ("</P>");
144 sb.Append ("<Q>");
145 sb.Append (Convert.ToBase64String (rsaParams.Q));
146 sb.Append ("</Q>");
148 sb.Append ("<DP>");
149 sb.Append (Convert.ToBase64String (rsaParams.DP));
150 sb.Append ("</DP>");
152 sb.Append ("<DQ>");
153 sb.Append (Convert.ToBase64String (rsaParams.DQ));
154 sb.Append ("</DQ>");
156 sb.Append ("<InverseQ>");
157 sb.Append (Convert.ToBase64String (rsaParams.InverseQ));
158 sb.Append ("</InverseQ>");
160 sb.Append ("<D>");
161 sb.Append (Convert.ToBase64String (rsaParams.D));
162 sb.Append ("</D>");
165 sb.Append ("</RSAKeyValue>");
167 catch {
168 ZeroizePrivateKey (rsaParams);
169 throw;
172 return sb.ToString ();