@@ -13,7 +13,7 @@ namespace PCLCrypto.Formatters
1313 /// <summary>
1414 /// A base class for encoding and decoding RSA keys in various formats.
1515 /// </summary>
16- internal abstract class KeyFormatter
16+ public abstract class KeyFormatter
1717 {
1818 /// <summary>
1919 /// The PKCS1 key formatter.
@@ -67,7 +67,7 @@ internal abstract class KeyFormatter
6767 /// </summary>
6868 /// <param name="blobType">Type of the key blob.</param>
6969 /// <returns>An instance of <see cref="KeyFormatter"/></returns>
70- internal static KeyFormatter GetFormatter ( CryptographicPrivateKeyBlobType blobType )
70+ public static KeyFormatter GetFormatter ( CryptographicPrivateKeyBlobType blobType )
7171 {
7272 switch ( blobType )
7373 {
@@ -93,7 +93,7 @@ internal static KeyFormatter GetFormatter(CryptographicPrivateKeyBlobType blobTy
9393 /// </summary>
9494 /// <param name="blobType">Type of the key blob.</param>
9595 /// <returns>An instance of <see cref="KeyFormatter"/></returns>
96- internal static KeyFormatter GetFormatter ( CryptographicPublicKeyBlobType blobType )
96+ public static KeyFormatter GetFormatter ( CryptographicPublicKeyBlobType blobType )
9797 {
9898 switch ( blobType )
9999 {
@@ -112,12 +112,58 @@ internal static KeyFormatter GetFormatter(CryptographicPublicKeyBlobType blobTyp
112112 }
113113 }
114114
115+ #if ! WinRT && ( ! SILVERLIGHT || WINDOWS_PHONE ) // we just want SL5 excluded
116+
117+ /// <summary>
118+ /// Converts the PCLCrypto <see cref="RSAParameters"/> struct to the type
119+ /// offered by the .NET Framework.
120+ /// </summary>
121+ /// <param name="value">The PCLCrypto parameters.</param>
122+ /// <returns>The .NET Framework parameters.</returns>
123+ public static System . Security . Cryptography . RSAParameters ToPlatformParameters ( RSAParameters value )
124+ {
125+ return new System . Security . Cryptography . RSAParameters
126+ {
127+ D = value . D ,
128+ Q = value . Q ,
129+ P = value . P ,
130+ DP = value . DP ,
131+ DQ = value . DQ ,
132+ Exponent = value . Exponent ,
133+ InverseQ = value . InverseQ ,
134+ Modulus = value . Modulus ,
135+ } ;
136+ }
137+
138+ /// <summary>
139+ /// Converts the .NET Framework <see cref="RSAParameters"/> struct to the type
140+ /// offered by the PCLCrypto library.
141+ /// </summary>
142+ /// <param name="value">The .NET Framework parameters.</param>
143+ /// <returns>The PCLCrypto parameters.</returns>
144+ public static RSAParameters ToPCLParameters ( System . Security . Cryptography . RSAParameters value )
145+ {
146+ return new RSAParameters
147+ {
148+ D = value . D ,
149+ Q = value . Q ,
150+ P = value . P ,
151+ DP = value . DP ,
152+ DQ = value . DQ ,
153+ Exponent = value . Exponent ,
154+ InverseQ = value . InverseQ ,
155+ Modulus = value . Modulus ,
156+ } ;
157+ }
158+
159+ #endif
160+
115161 /// <summary>
116162 /// Writes a key to the specified stream.
117163 /// </summary>
118164 /// <param name="stream">The stream.</param>
119165 /// <param name="parameters">The parameters.</param>
120- internal void Write ( Stream stream , RSAParameters parameters )
166+ public void Write ( Stream stream , RSAParameters parameters )
121167 {
122168 this . Write ( stream , parameters , HasPrivateKey ( parameters ) ) ;
123169 }
@@ -128,7 +174,7 @@ internal void Write(Stream stream, RSAParameters parameters)
128174 /// <param name="stream">The stream.</param>
129175 /// <param name="parameters">The parameters.</param>
130176 /// <param name="includePrivateKey">if set to <c>true</c> the private key will be written as well; otherwise just the public key will be written.</param>
131- internal void Write ( Stream stream , RSAParameters parameters , bool includePrivateKey )
177+ public void Write ( Stream stream , RSAParameters parameters , bool includePrivateKey )
132178 {
133179 Requires . NotNull ( stream , "stream" ) ;
134180 Requires . Argument ( HasPrivateKey ( parameters ) || ! includePrivateKey , "parameters" , "No private key data included." ) ;
@@ -146,7 +192,7 @@ internal void Write(Stream stream, RSAParameters parameters, bool includePrivate
146192 /// </summary>
147193 /// <param name="parameters">The parameters.</param>
148194 /// <returns>The buffer with the serialized key.</returns>
149- internal byte [ ] Write ( RSAParameters parameters )
195+ public byte [ ] Write ( RSAParameters parameters )
150196 {
151197 return this . Write ( parameters , HasPrivateKey ( parameters ) ) ;
152198 }
@@ -157,7 +203,7 @@ internal byte[] Write(RSAParameters parameters)
157203 /// <param name="parameters">The parameters.</param>
158204 /// <param name="includePrivateKey">if set to <c>true</c> the private key will be written as well; otherwise just the public key will be written.</param>
159205 /// <returns>The buffer with the serialized key.</returns>
160- internal byte [ ] Write ( RSAParameters parameters , bool includePrivateKey )
206+ public byte [ ] Write ( RSAParameters parameters , bool includePrivateKey )
161207 {
162208 var ms = new MemoryStream ( ) ;
163209 this . Write ( ms , parameters , includePrivateKey ) ;
@@ -169,7 +215,7 @@ internal byte[] Write(RSAParameters parameters, bool includePrivateKey)
169215 /// </summary>
170216 /// <param name="stream">The stream.</param>
171217 /// <returns>The RSA key parameters.</returns>
172- internal RSAParameters Read ( Stream stream )
218+ public RSAParameters Read ( Stream stream )
173219 {
174220 var parameters = this . ReadCore ( stream ) ;
175221 return TrimLeadingZeros ( parameters ) ;
@@ -180,7 +226,7 @@ internal RSAParameters Read(Stream stream)
180226 /// </summary>
181227 /// <param name="keyBlob">The buffer containing the key data.</param>
182228 /// <returns>The RSA key parameters.</returns>
183- internal RSAParameters Read ( byte [ ] keyBlob )
229+ public RSAParameters Read ( byte [ ] keyBlob )
184230 {
185231 var ms = new MemoryStream ( keyBlob ) ;
186232 return this . Read ( ms ) ;
@@ -200,46 +246,6 @@ protected internal static RSAParameters PublicKeyFilter(RSAParameters value)
200246 } ;
201247 }
202248
203- /// <summary>
204- /// Tries to add/remove leading zeros as necessary in an attempt to make the parameters CAPI compatible.
205- /// </summary>
206- /// <param name="parameters">The parameters.</param>
207- /// <returns>The modified set of parameters.</returns>
208- /// <remarks>
209- /// The original parameters and their buffers are not modified.
210- /// </remarks>
211- protected internal static RSAParameters NegotiateSizes ( RSAParameters parameters )
212- {
213- if ( HasPrivateKey ( parameters ) )
214- {
215- if ( CapiKeyFormatter . IsCapiCompatible ( parameters ) )
216- {
217- // Don't change a thing. Everything is perfect.
218- return parameters ;
219- }
220-
221- parameters . Modulus = TrimLeadingZero ( parameters . Modulus ) ;
222- parameters . D = TrimLeadingZero ( parameters . D ) ;
223- int keyLength = Math . Max ( parameters . Modulus . Length , parameters . D ? . Length ?? 0 ) ;
224- parameters . Modulus = TrimOrPadZeroToLength ( parameters . Modulus , keyLength ) ;
225- parameters . D = TrimOrPadZeroToLength ( parameters . D , keyLength ) ;
226-
227- int halfKeyLength = ( keyLength + 1 ) / 2 ;
228- parameters . P = TrimOrPadZeroToLength ( parameters . P , halfKeyLength ) ;
229- parameters . Q = TrimOrPadZeroToLength ( parameters . Q , halfKeyLength ) ;
230- parameters . DP = TrimOrPadZeroToLength ( parameters . DP , halfKeyLength ) ;
231- parameters . DQ = TrimOrPadZeroToLength ( parameters . DQ , halfKeyLength ) ;
232- parameters . InverseQ = TrimOrPadZeroToLength ( parameters . InverseQ , halfKeyLength ) ;
233- }
234- else
235- {
236- parameters . Modulus = TrimLeadingZero ( parameters . Modulus ) ;
237- }
238-
239- parameters . Exponent = TrimLeadingZero ( parameters . Exponent ) ;
240- return parameters ;
241- }
242-
243249 /// <summary>
244250 /// Determines whether a set of RSA parameters includes a private key.
245251 /// </summary>
@@ -250,52 +256,6 @@ protected internal static bool HasPrivateKey(RSAParameters parameters)
250256 return parameters . P != null ;
251257 }
252258
253- #if ! WinRT && ( ! SILVERLIGHT || WINDOWS_PHONE ) // we just want SL5 excluded
254-
255- /// <summary>
256- /// Converts the PCLCrypto <see cref="RSAParameters"/> struct to the type
257- /// offered by the .NET Framework.
258- /// </summary>
259- /// <param name="value">The PCLCrypto parameters.</param>
260- /// <returns>The .NET Framework parameters.</returns>
261- protected internal static System . Security . Cryptography . RSAParameters ToPlatformParameters ( RSAParameters value )
262- {
263- return new System . Security . Cryptography . RSAParameters
264- {
265- D = value . D ,
266- Q = value . Q ,
267- P = value . P ,
268- DP = value . DP ,
269- DQ = value . DQ ,
270- Exponent = value . Exponent ,
271- InverseQ = value . InverseQ ,
272- Modulus = value . Modulus ,
273- } ;
274- }
275-
276- /// <summary>
277- /// Converts the .NET Framework <see cref="RSAParameters"/> struct to the type
278- /// offered by the PCLCrypto library.
279- /// </summary>
280- /// <param name="value">The .NET Framework parameters.</param>
281- /// <returns>The PCLCrypto parameters.</returns>
282- protected internal static RSAParameters ToPCLParameters ( System . Security . Cryptography . RSAParameters value )
283- {
284- return new RSAParameters
285- {
286- D = value . D ,
287- Q = value . Q ,
288- P = value . P ,
289- DP = value . DP ,
290- DQ = value . DQ ,
291- Exponent = value . Exponent ,
292- InverseQ = value . InverseQ ,
293- Modulus = value . Modulus ,
294- } ;
295- }
296-
297- #endif
298-
299259 /// <summary>
300260 /// Checks whether two buffers have equal contents.
301261 /// </summary>
0 commit comments