@@ -9,39 +9,30 @@ namespace GodSharp.Encryption
99 /// <summary>
1010 /// Asymmetric/RSA encryption.
1111 /// </summary>
12- public class RSA
12+ public static class RSA
1313 {
1414 /// <summary>
15- /// The string format of public key .
15+ /// Gets or sets the open SSL .
1616 /// </summary>
17- private static string publicKeyFormat = @"<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>" ;
18-
19- /// <summary>
20- /// The string format of private key.
21- /// </summary>
22- private static string privateKeyFormat = @"<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>" ;
17+ /// <value>
18+ /// The open SSL.
19+ /// </value>
20+ public static RSAOpenSsl OpenSsl => RSAOpenSslSingletonContainer . Instance ;
2321
2422 #region Get public and private key of xml format.
2523 /// <summary>
2624 /// Get public and private key of xml format.
2725 /// </summary>
2826 /// <param name="xmlPrivateKey">The private key of xml format.</param>
2927 /// <param name="xmlPublicKey">The public key of xml format.</param>
30- /// <param name="dwKeySize ">The size of the key to use in bits</param>
31- public static void CreateKey ( out string xmlPrivateKey , out string xmlPublicKey , int dwKeySize = 1024 )
28+ /// <param name="size ">The size of the key to use in bits</param>
29+ public static void CreateXmlKey ( out string xmlPrivateKey , out string xmlPublicKey , RSASizeTypes size = RSASizeTypes . L2048 )
3230 {
33- try
31+ using ( RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ( ( int ) size ) )
3432 {
35- xmlPrivateKey = null ;
36- xmlPublicKey = null ;
37- RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ( dwKeySize ) ;
3833 xmlPrivateKey = rsa . ToXmlString ( true ) ;
3934 xmlPublicKey = rsa . ToXmlString ( false ) ;
4035 }
41- catch ( Exception ex )
42- {
43- throw ex ;
44- }
4536 }
4637 #endregion
4738
@@ -54,14 +45,9 @@ public static void CreateKey(out string xmlPrivateKey, out string xmlPublicKey,
5445 /// <returns>String private key of xml format.</returns>
5546 public static string GetPrivateKey ( string certFile , string password )
5647 {
57- if ( ! File . Exists ( certFile ) )
58- {
59- throw new FileNotFoundException ( nameof ( certFile ) ) ;
60- }
48+ X509Certificate2 cert = GetX509Certificate2 ( certFile ) ;
6149
62- X509Certificate2 cert = new X509Certificate2 ( certFile , password , X509KeyStorageFlags . Exportable ) ;
63- string privateKey = cert . PrivateKey . ToXmlString ( true ) ;
64- return privateKey ;
50+ return cert . PrivateKey . ToXmlString ( true ) ;
6551 }
6652
6753 /// <summary>
@@ -71,14 +57,22 @@ public static string GetPrivateKey(string certFile, string password)
7157 /// <returns>String public key of xml format.</returns>
7258 public static string GetPublicKey ( string certFile )
7359 {
74- if ( ! File . Exists ( certFile ) )
75- {
76- throw new FileNotFoundException ( nameof ( certFile ) ) ;
77- }
60+ X509Certificate2 cert = GetX509Certificate2 ( certFile ) ;
7861
79- X509Certificate2 cert = new X509Certificate2 ( certFile ) ;
80- string publicKey = cert . PublicKey . Key . ToXmlString ( false ) ;
81- return publicKey ;
62+ return cert . PublicKey . Key . ToXmlString ( false ) ;
63+ }
64+
65+ /// <summary>
66+ /// Gets the X509 certificate2.
67+ /// </summary>
68+ /// <param name="certFile">The cert file.</param>
69+ /// <returns></returns>
70+ /// <exception cref="FileNotFoundException">certFile</exception>
71+ private static X509Certificate2 GetX509Certificate2 ( string certFile )
72+ {
73+ if ( ! File . Exists ( certFile ) ) throw new FileNotFoundException ( nameof ( certFile ) ) ;
74+
75+ return new X509Certificate2 ( certFile ) ;
8276 }
8377 #endregion
8478
@@ -92,12 +86,9 @@ public static string GetPublicKey(string certFile)
9286 /// <returns>The encrypted data.</returns>
9387 public static string Encrypt ( string data , string xmlPublicKey , Encoding encoding = null )
9488 {
95- using ( RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ( ) )
96- {
97- rsa . FromXmlString ( xmlPublicKey ) ;
98- byte [ ] bytes = rsa . Encrypt ( encoding . GetBytes ( data ) , false ) ;
99- return Convert . ToBase64String ( bytes ) ;
100- }
89+ if ( encoding == null ) encoding = Encoding . UTF8 ;
90+
91+ return Encrypt ( encoding . GetBytes ( data ) , xmlPublicKey ) ;
10192 }
10293
10394 /// <summary>
@@ -125,13 +116,7 @@ public static string Encrypt(byte[] dataBytes, string xmlPublicKey)
125116 /// <returns>The decrypted data.</returns>
126117 public static string Decrypt ( string data , string xmlPrivateKey , Encoding encoding = null )
127118 {
128- using ( RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ( ) )
129- {
130- byte [ ] dataBytes = Convert . FromBase64String ( data ) ;
131- rsa . FromXmlString ( xmlPrivateKey ) ;
132- byte [ ] bytes = rsa . Decrypt ( dataBytes , false ) ;
133- return encoding . GetString ( bytes ) ;
134- }
119+ return Decrypt ( Convert . FromBase64String ( data ) , xmlPrivateKey , encoding ) ;
135120 }
136121
137122 /// <summary>
@@ -143,6 +128,8 @@ public static string Decrypt(string data, string xmlPrivateKey, Encoding encodin
143128 /// <returns>The decrypted data.</returns>
144129 public static string Decrypt ( byte [ ] dataBytes , string xmlPrivateKey , Encoding encoding = null )
145130 {
131+ if ( encoding == null ) encoding = Encoding . UTF8 ;
132+
146133 using ( RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ( ) )
147134 {
148135 rsa . FromXmlString ( xmlPrivateKey ) ;
@@ -151,102 +138,5 @@ public static string Decrypt(byte[] dataBytes, string xmlPrivateKey, Encoding en
151138 }
152139 }
153140 #endregion
154-
155- #region Get hash sign
156- /// <summary>
157- /// Get hash sign.
158- /// </summary>
159- /// <param name="data"></param>
160- /// <param name="hash"></param>
161- /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
162- /// <returns></returns>
163- public bool GetHash ( string data , ref byte [ ] hash , Encoding encoding = null )
164- {
165- try
166- {
167- if ( encoding == null )
168- {
169- encoding = Encoding . UTF8 ;
170- }
171- HashAlgorithm MD5 = HashAlgorithm . Create ( "MD5" ) ;
172- hash = MD5 . ComputeHash ( encoding . GetBytes ( data ) ) ;
173- return true ;
174- }
175- catch ( Exception ex )
176- {
177- throw ex ;
178- }
179- }
180-
181- /// <summary>
182- /// Get hash sign.
183- /// </summary>
184- /// <param name="data"></param>
185- /// <param name="hash"></param>
186- /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
187- /// <returns></returns>
188- public bool GetHash ( string data , ref string hash , Encoding encoding = null )
189- {
190- try
191- {
192- if ( encoding == null )
193- {
194- encoding = Encoding . UTF8 ;
195- }
196-
197- HashAlgorithm MD5 = HashAlgorithm . Create ( "MD5" ) ;
198- byte [ ] buffer = MD5 . ComputeHash ( encoding . GetBytes ( data ) ) ;
199- hash = Convert . ToBase64String ( buffer ) ;
200- return true ;
201- }
202- catch ( Exception ex )
203- {
204- throw ex ;
205- }
206- }
207-
208- /// <summary>
209- /// Get hash sign.
210- /// </summary>
211- /// <param name="fs"></param>
212- /// <param name="hash"></param>
213- /// <returns></returns>
214- public bool GetHash ( FileStream fs , ref byte [ ] hash )
215- {
216- try
217- {
218- HashAlgorithm MD5 = HashAlgorithm . Create ( "MD5" ) ;
219- hash = MD5 . ComputeHash ( fs ) ;
220- fs . Close ( ) ;
221- return true ;
222- }
223- catch ( Exception ex )
224- {
225- throw ex ;
226- }
227- }
228-
229- /// <summary>
230- /// Get hash sign.
231- /// </summary>
232- /// <param name="fs"></param>
233- /// <param name="hash"></param>
234- /// <returns></returns>
235- public bool GetHash ( FileStream fs , ref string hash )
236- {
237- try
238- {
239- HashAlgorithm MD5 = HashAlgorithm . Create ( "MD5" ) ;
240- byte [ ] buffer = MD5 . ComputeHash ( fs ) ;
241- fs . Close ( ) ;
242- hash = Convert . ToBase64String ( buffer ) ;
243- return true ;
244- }
245- catch ( Exception ex )
246- {
247- throw ex ;
248- }
249- }
250- #endregion
251141 }
252142}
0 commit comments