77using NETCore . Encrypt . Extensions ;
88using NETCore . Encrypt . Internal ;
99using NETCore . Encrypt . Extensions . Internal ;
10-
1110namespace NETCore . Encrypt
1211{
1312 public class EncryptProvider
@@ -311,30 +310,6 @@ public static string AESDecrypt(string data, string key)
311310
312311 #endregion
313312
314- #region Rijndael
315- /// <summary>
316- /// AES Rijndael
317- /// </summary>
318- public static void RijndaelEncrypt ( string data , string key )
319- {
320- //https://blog.csdn.net/yupu56/article/details/72236950
321-
322- using ( Rijndael rijndael = Rijndael . Create ( ) )
323- {
324- Check . Argument . IsNotEmpty ( data , nameof ( data ) ) ;
325- Check . Argument . IsNotEmpty ( key , nameof ( key ) ) ;
326- Check . Argument . IsNotOutOfRange ( key . Length , 32 , 32 , nameof ( key ) ) ;
327-
328- rijndael . Mode = CipherMode . ECB ;
329- rijndael . Padding = PaddingMode . PKCS7 ;
330- rijndael . KeySize = 256 ;
331-
332- }
333-
334-
335- }
336- #endregion
337-
338313 #region DES
339314
340315 /// <summary>
@@ -576,15 +551,19 @@ private static byte[] DESDecrypt(byte[] data, string key, CipherMode cipherMode,
576551 /// <summary>
577552 /// RSA Converter to pem
578553 /// </summary>
579- /// <param name="isPKCS8"></param>
554+ /// <param name="isPKCS8">true:PKCS8 false:PKCS1</param>
555+ /// <param name="keySize">Rsa key size ,default is 2048, min value is 2048</param>
580556 /// <returns></returns>
581- public static ( string publicPem , string privatePem ) RSAToPem ( bool isPKCS8 )
557+ public static ( string publicPem , string privatePem ) RSAToPem ( bool isPKCS8 , int keySize = 2048 )
582558 {
583- var rsaKey = CreateRsaKey ( ) ;
559+ if ( keySize < 2048 )
560+ {
561+ throw new ArgumentException ( $ " Key size min value is 2048!") ;
562+ }
584563
585564 using ( RSA rsa = RSA . Create ( ) )
586565 {
587- rsa . FromJsonString ( rsaKey . PrivateKey ) ;
566+ rsa . KeySize = keySize ;
588567
589568 var publicPem = RsaProvider . ToPem ( rsa , false , isPKCS8 ) ;
590569 var privatePem = RsaProvider . ToPem ( rsa , true , isPKCS8 ) ;
@@ -604,6 +583,116 @@ public static RSA RSAFromPem(string pem)
604583 return RsaProvider . FromPem ( pem ) ;
605584 }
606585
586+ /// <summary>
587+ /// Export Rsa PKCS1 key
588+ /// </summary>
589+ /// <param name="keySize"></param>
590+ /// <returns></returns>
591+ public static ( string publckPkcs1 , string privatePkcs1 ) RsaToPkcs1 ( int keySize = 2048 )
592+ {
593+ if ( keySize < 2048 )
594+ {
595+ throw new ArgumentException ( $ " Key size min value is 2048!") ;
596+ }
597+
598+ using ( RSA rsa = RSA . Create ( ) )
599+ {
600+ rsa . KeySize = keySize ;
601+ var publicKey = Convert . ToBase64String ( rsa . ExportRSAPublicKey ( ) ) ;
602+ var privateKey = Convert . ToBase64String ( rsa . ExportRSAPrivateKey ( ) ) ;
603+
604+ return ( publicKey , privateKey ) ;
605+ }
606+ }
607+
608+ /// <summary>
609+ /// Export Rsa PKCS8 key
610+ /// </summary>
611+ /// <param name="keySize"></param>
612+ /// <returns></returns>
613+ public static ( string publckPkcs8 , string privatePkcs8 ) RsaToPkcs8 ( int keySize = 2048 )
614+ {
615+ if ( keySize < 2048 )
616+ {
617+ throw new ArgumentException ( $ " Key size min value is 2048!") ;
618+ }
619+
620+ using ( RSA rsa = RSA . Create ( ) )
621+ {
622+ rsa . KeySize = keySize ;
623+
624+ var publicKey = Convert . ToBase64String ( rsa . ExportRSAPublicKey ( ) ) ;
625+ var privateKey = Convert . ToBase64String ( rsa . ExportPkcs8PrivateKey ( ) ) ;
626+
627+ return ( publicKey , privateKey ) ;
628+ }
629+ }
630+
631+ /// <summary>
632+ /// RSA From pkcs public key
633+ /// </summary>
634+ /// <param name="pkcsKey"></param>
635+ /// <returns></returns>
636+ public static RSA RSAFromPublicPkcs ( string pkcsKey )
637+ {
638+ return RSAFromPkcs ( pkcsKey , false ) ;
639+ }
640+
641+ /// <summary>
642+ /// RSA From pkcs #1 private key
643+ /// </summary>
644+ /// <param name="pkcsKey"></param>
645+ /// <returns></returns>
646+ public static RSA RSAFromPrivatePkcs1 ( string pkcsKey )
647+ {
648+ return RSAFromPkcs ( pkcsKey , true ) ;
649+ }
650+
651+ /// <summary>
652+ /// RSA From pkcs #8 private key
653+ /// </summary>
654+ /// <param name="pkcsKey"></param>
655+ /// <returns></returns>
656+ public static RSA RSAFromPrivatePkcs8 ( string pkcsKey )
657+ {
658+ return RSAFromPkcs ( pkcsKey , true , true ) ;
659+ }
660+
661+ /// <summary>
662+ /// RSA From pkcs#1 or pkcs#8
663+ /// </summary>
664+ /// <param name="pkcsKey">Pkcs #1 or Pkcs #8</param>
665+ /// <param name="isPrivateKey">true:privateKey,false:publicKey</param>
666+ /// <param name="isPKCS8">true:PKCS8 false:PKCS1</param>
667+ /// <returns></returns>
668+ public static RSA RSAFromPkcs ( string pkcsKey , bool isPrivateKey , bool isPKCS8 = false )
669+ {
670+ Check . Argument . IsNotEmpty ( pkcsKey , nameof ( pkcsKey ) ) ;
671+
672+ RSA rsa = RSA . Create ( ) ;
673+
674+ var keySource = Convert . FromBase64String ( pkcsKey ) ;
675+
676+ if ( ! isPrivateKey )
677+ {
678+ rsa . ImportRSAPublicKey ( keySource , out _ ) ;
679+ }
680+ else
681+ {
682+ if ( isPKCS8 )
683+ {
684+ rsa . ImportPkcs8PrivateKey ( keySource , out _ ) ;
685+ }
686+ else
687+ {
688+ rsa . ImportRSAPrivateKey ( keySource , out _ ) ;
689+ }
690+ }
691+
692+ return rsa ;
693+
694+ }
695+
607696 /// <summary>
608697 /// RSA Sign
609698 /// </summary>
@@ -690,22 +779,18 @@ public static string RSAEncrypt(string publicKey, string srcString)
690779 return encryptStr ;
691780 }
692781
693-
694-
695782 /// <summary>
696783 /// RSA encrypt with pem key
697784 /// </summary>
698785 /// <param name="publicKey">pem public key</param>
699- /// <param name="scrString ">src string</param>
786+ /// <param name="srcString ">src string</param>
700787 /// <returns></returns>
701788 public static string RSAEncryptWithPem ( string publicKey , string srcString )
702789 {
703790 string encryptStr = RSAEncrypt ( publicKey , srcString , RSAEncryptionPadding . Pkcs1 , true ) ;
704791 return encryptStr ;
705792 }
706793
707-
708-
709794 /// <summary>
710795 /// RSA encrypt
711796 /// </summary>
@@ -929,6 +1014,7 @@ public static byte[] RSADecrypt(string privateKey, byte[] data, RSAEncryptionPad
9291014 /// </summary>
9301015 /// <param name="rsaKey">rsa json string</param>
9311016 /// <returns></returns>
1017+ [ Obsolete ( "This method is obsoleted,please use RSAFromJson method!" ) ]
9321018 public static RSA RSAFromString ( string rsaKey )
9331019 {
9341020 Check . Argument . IsNotEmpty ( rsaKey , nameof ( rsaKey ) ) ;
@@ -938,6 +1024,20 @@ public static RSA RSAFromString(string rsaKey)
9381024 return rsa ;
9391025 }
9401026
1027+ /// <summary>
1028+ /// RSA from json string
1029+ /// </summary>
1030+ /// <param name="rsaKey">rsa json key</param>
1031+ /// <returns></returns>
1032+ public static RSA RSAFromJson ( string rsaKey )
1033+ {
1034+ Check . Argument . IsNotEmpty ( rsaKey , nameof ( rsaKey ) ) ;
1035+ RSA rsa = RSA . Create ( ) ;
1036+
1037+ rsa . FromJsonString ( rsaKey ) ;
1038+ return rsa ;
1039+ }
1040+
9411041 /// <summary>
9421042 /// Create an RSA key
9431043 /// </summary>
@@ -974,7 +1074,6 @@ public static RSAKey CreateRsaKey(RSA rsa, bool includePrivate = true)
9741074
9751075 string publicKey = rsa . ToJsonString ( false ) ;
9761076
977-
9781077 var rsaKey = new RSAKey ( )
9791078 {
9801079 PublicKey = publicKey ,
0 commit comments