1+ using System ;
2+ using System . Security . Cryptography ;
3+ using System . Text ;
4+ using Cosmos . Encryption . Core ;
5+ using Cosmos . Encryption . Core . Internals . Extensions ;
6+
7+ // ReSharper disable once CheckNamespace
8+ namespace Cosmos . Encryption {
9+ // ReSharper disable once InconsistentNaming
10+ public static partial class RSAEncryptionProvider {
11+
12+ #region Extensions for export and import
13+
14+ /// <summary>
15+ /// Export RSA private key
16+ /// </summary>
17+ /// <param name="rsa"></param>
18+ /// <param name="type"></param>
19+ /// <param name="usePemFormat"></param>
20+ /// <returns></returns>
21+ public static string ExportPrivateKey ( this RSA rsa , RSAKeyTypes type , bool usePemFormat = false ) {
22+ var key = type switch {
23+ RSAKeyTypes . XML => rsa . ToLvccXmlString ( true ) ,
24+ RSAKeyTypes . JSON => rsa . ToJsonString ( true ) ,
25+ #if NETCOREAPP3_1 || NETSTANDARD2_1
26+ RSAKeyTypes . Pkcs1 => Base64ConvertProvider. ToBase64String ( rsa . ExportRSAPrivateKey ( ) ) ,
27+ RSAKeyTypes. Pkcs8 => Base64ConvertProvider. ToBase64String ( rsa . ExportPkcs8PrivateKey ( ) ) ,
28+ #else
29+ RSAKeyTypes . Pkcs1 => rsa. ToPkcs1PrivateString ( ) ,
30+ RSAKeyTypes. Pkcs8 => rsa. ToPkcs8PrivateString ( ) ,
31+ #endif
32+ _ => throw new NotSupportedException( "Unknown RSA key type." )
33+ } ;
34+
35+ if ( usePemFormat ) {
36+ key = type switch {
37+ RSAKeyTypes . XML => key ,
38+ RSAKeyTypes . JSON => key ,
39+ RSAKeyTypes . Pkcs1 => RSAPemFormatHelper . Pkcs1PrivateKeyFormat ( key ) ,
40+ RSAKeyTypes . Pkcs8 => RSAPemFormatHelper . Pkcs8PrivateKeyFormat ( key ) ,
41+ _ => throw new NotSupportedException ( "Unknown RSA key type." )
42+ } ;
43+ }
44+
45+ return key;
46+ }
47+
48+ /// <summary>
49+ /// Export RSA public key
50+ /// </summary>
51+ /// <param name="rsa"></param>
52+ /// <param name="type"></param>
53+ /// <param name="usePemFormat"></param>
54+ /// <returns></returns>
55+ public static string ExportPublicKey( this RSA rsa, RSAKeyTypes type , bool usePemFormat = false) {
56+ var key = type switch {
57+ RSAKeyTypes . XML => rsa . ToLvccXmlString ( false ) ,
58+ RSAKeyTypes . JSON => rsa . ToJsonString ( false ) ,
59+ #if NETCOREAPP3_1 || NETSTANDARD2_1
60+ RSAKeyTypes . Pkcs1 => Base64ConvertProvider. ToBase64String ( rsa . ExportRSAPublicKey ( ) ) ,
61+ RSAKeyTypes. Pkcs8 => Base64ConvertProvider. ToBase64String ( rsa . ExportRSAPublicKey ( ) ) ,
62+ #else
63+ RSAKeyTypes . Pkcs1 => rsa. ToPkcs1PublicString ( ) ,
64+ RSAKeyTypes. Pkcs8 => rsa. ToPkcs8PublicString ( ) ,
65+ #endif
66+ _ => throw new NotSupportedException( "Unknown RSA key type." )
67+ } ;
68+
69+ if ( usePemFormat ) {
70+ key = type switch {
71+ RSAKeyTypes . XML => key ,
72+ RSAKeyTypes . JSON => key ,
73+ RSAKeyTypes . Pkcs1 => RSAPemFormatHelper . Pkcs1PublicKeyFormat ( key ) ,
74+ RSAKeyTypes . Pkcs8 => RSAPemFormatHelper . Pkcs8PublicKeyFormat ( key ) ,
75+ _ => throw new NotSupportedException ( "Unknown RSA key type." )
76+ } ;
77+ }
78+
79+ return key;
80+ }
81+
82+ /// <summary>
83+ /// Import RSA private key
84+ /// </summary>
85+ /// <param name="rsa"></param>
86+ /// <param name="type"></param>
87+ /// <param name="privateKey"></param>
88+ /// <param name="isPem"></param>
89+ public static void ImportPrivateKey( this RSA rsa, RSAKeyTypes type , string privateKey , bool isPem = false) {
90+ if ( isPem ) {
91+ privateKey = type switch {
92+ RSAKeyTypes . XML => privateKey ,
93+ RSAKeyTypes . JSON => privateKey ,
94+ RSAKeyTypes . Pkcs1 => RSAPemFormatHelper . Pkcs1PrivateKeyFormatRemove ( privateKey ) ,
95+ RSAKeyTypes . Pkcs8 => RSAPemFormatHelper . Pkcs8PrivateKeyFormatRemove ( privateKey ) ,
96+ _ => throw new NotSupportedException ( "Unknown RSA key type." )
97+ } ;
98+ }
99+
100+ switch ( type ) {
101+ case RSAKeyTypes. XML :
102+ rsa. FromLvccXmlString ( privateKey ) ;
103+ break ;
104+
105+ case RSAKeyTypes. JSON :
106+ rsa. FromJsonString ( privateKey ) ;
107+ break ;
108+
109+ case RSAKeyTypes. Pkcs1 :
110+ #if NETCOREAPP3_1 || NETSTANDARD2_1
111+ rsa. ImportRSAPrivateKey ( Convert . FromBase64String ( privateKey ) , out _ ) ;
112+ #else
113+ rsa. FromPkcs1PrivateString ( privateKey , out _ ) ;
114+ #endif
115+ break ;
116+
117+ case RSAKeyTypes. Pkcs8 :
118+ #if NETCOREAPP3_1 || NETSTANDARD2_1
119+ rsa. ImportPkcs8PrivateKey ( Convert . FromBase64String ( privateKey ) , out _ ) ;
120+ #else
121+ rsa. FromPkcs8PrivateString ( privateKey , out _ ) ;
122+ #endif
123+ break ;
124+ }
125+ }
126+
127+ /// <summary>
128+ /// Import RSA public key
129+ /// </summary>
130+ /// <param name="rsa"></param>
131+ /// <param name="type"></param>
132+ /// <param name="publicKey"></param>
133+ /// <param name="isPem"></param>
134+ public static void ImportPublicKey( this RSA rsa, RSAKeyTypes type , string publicKey , bool isPem = false) {
135+ if ( isPem ) {
136+ publicKey = type switch {
137+ RSAKeyTypes . XML => publicKey ,
138+ RSAKeyTypes . JSON => publicKey ,
139+ RSAKeyTypes . Pkcs1 => RSAPemFormatHelper . Pkcs1PublicKeyFormatRemove ( publicKey ) ,
140+ RSAKeyTypes . Pkcs8 => RSAPemFormatHelper . Pkcs8PublicKeyFormatRemove ( publicKey ) ,
141+ _ => throw new NotSupportedException ( "Unknown RSA key type." )
142+ } ;
143+ }
144+
145+ switch ( type ) {
146+ case RSAKeyTypes. XML :
147+ rsa. FromLvccXmlString ( publicKey ) ;
148+ break ;
149+
150+ case RSAKeyTypes. JSON :
151+ rsa. FromJsonString ( publicKey ) ;
152+ break ;
153+
154+ case RSAKeyTypes. Pkcs1 :
155+ #if NETCOREAPP3_1 || NETSTANDARD2_1
156+ rsa. ImportRSAPublicKey ( Convert . FromBase64String ( publicKey ) , out _ ) ;
157+ break ;
158+ #else
159+ rsa. FromPkcs1PublicString ( publicKey , out _ ) ;
160+ break ;
161+ #endif
162+
163+ case RSAKeyTypes. Pkcs8 :
164+ #if NETCOREAPP3_1 || NETSTANDARD2_1
165+ rsa. ImportRSAPublicKey ( Convert . FromBase64String ( publicKey ) , out _ ) ;
166+ break ;
167+ #else
168+ rsa. FromPkcs8PublicString ( publicKey , out _ ) ;
169+ break ;
170+ #endif
171+ }
172+ }
173+
174+ #endregion
175+
176+ #region Extensions for touching RSA utils.
177+
178+ private static RSABase TouchRsaUtilFromPublicKey( RSAKeyTypes keyType , Encoding encoding , string publicKey , RSAKeySizeTypes sizeType ) {
179+ RSABase rsa = keyType switch {
180+ RSAKeyTypes . XML => new RSAXmlUtil ( encoding , publicKey , keySize : ( int ) sizeType ) ,
181+ RSAKeyTypes . JSON => new RSAJsonUtil ( encoding , publicKey , keySize : ( int ) sizeType ) ,
182+ RSAKeyTypes . Pkcs1 => new RSAPkcs1Util ( encoding , publicKey , keySize : ( int ) sizeType ) ,
183+ RSAKeyTypes . Pkcs8 => new RSAPkcs8Util ( encoding , publicKey , keySize : ( int ) sizeType ) ,
184+ _ => throw new NotSupportedException ( "Unknown RSA key type." )
185+ } ;
186+
187+ return rsa;
188+ }
189+
190+ private static RSABase TouchRsaUtilFromPrivateKey( RSAKeyTypes keyType , Encoding encoding , string privateKey , RSAKeySizeTypes sizeType ) {
191+ RSABase rsa = keyType switch {
192+ RSAKeyTypes . XML => new RSAXmlUtil ( encoding , null , privateKey , ( int ) sizeType ) ,
193+ RSAKeyTypes . JSON => new RSAXmlUtil ( encoding , null , privateKey , ( int ) sizeType ) ,
194+ RSAKeyTypes . Pkcs1 => new RSAPkcs1Util ( encoding , null , privateKey , ( int ) sizeType ) ,
195+ RSAKeyTypes . Pkcs8 => new RSAPkcs8Util ( encoding , null , privateKey , ( int ) sizeType ) ,
196+ _ => throw new NotSupportedException ( "Unknown RSA key type." ) ,
197+ } ;
198+
199+ return rsa;
200+ }
201+
202+ #endregion
203+
204+ }
205+ }
0 commit comments