@@ -449,6 +449,36 @@ public static byte[] DESDecrypt(byte[] data, string key)
449
449
450
450
#region RSA
451
451
452
+ /// <summary>
453
+ /// RSA Converter to pem
454
+ /// </summary>
455
+ /// <param name="isPKCS8"></param>
456
+ /// <returns></returns>
457
+ public static ( string publicPem , string privatePem ) RSAToPem ( bool isPKCS8 )
458
+ {
459
+ var rsaKey = CreateRsaKey ( ) ;
460
+
461
+ using ( RSA rsa = RSA . Create ( ) )
462
+ {
463
+ rsa . FromJsonString ( rsaKey . PrivateKey ) ;
464
+
465
+ var publicPem = RsaProvider . ToPem ( rsa , false , isPKCS8 ) ;
466
+ var privatePem = RsaProvider . ToPem ( rsa , true , isPKCS8 ) ;
467
+
468
+ return ( publicPem , privatePem ) ;
469
+ }
470
+ }
471
+
472
+ /// <summary>
473
+ /// RSA From pem
474
+ /// </summary>
475
+ /// <param name="pem"></param>
476
+ /// <returns></returns>
477
+ public static RSA RSAFromPem ( string pem )
478
+ {
479
+ Check . Argument . IsNotEmpty ( pem , nameof ( pem ) ) ;
480
+ return RsaProvider . FromPem ( pem ) ;
481
+ }
452
482
453
483
/// <summary>
454
484
/// RSA Sign
@@ -536,28 +566,51 @@ public static string RSAEncrypt(string publicKey, string srcString)
536
566
return encryptStr ;
537
567
}
538
568
569
+ /// <summary>
570
+ /// RSA encrypt with pem key
571
+ /// </summary>
572
+ /// <param name="publicKey">pem public key</param>
573
+ /// <param name="scrString">src string</param>
574
+ /// <returns></returns>
575
+ public static string RSAEncryptWithPem ( string publicKey , string srcString )
576
+ {
577
+ string encryptStr = RSAEncrypt ( publicKey , srcString , RSAEncryptionPadding . Pkcs1 , true ) ;
578
+ return encryptStr ;
579
+ }
580
+
539
581
/// <summary>
540
582
/// RSA encrypt
541
583
/// </summary>
542
584
/// <param name="publicKey">public key</param>
543
585
/// <param name="srcString">src string</param>
544
586
/// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
587
+ /// <param name="isPemKey">set key is pem format,default is false</param>
545
588
/// <returns>encrypted string</returns>
546
- public static string RSAEncrypt ( string publicKey , string srcString , RSAEncryptionPadding padding )
589
+ public static string RSAEncrypt ( string publicKey , string srcString , RSAEncryptionPadding padding , bool isPemKey = false )
547
590
{
548
591
Check . Argument . IsNotEmpty ( publicKey , nameof ( publicKey ) ) ;
549
592
Check . Argument . IsNotEmpty ( srcString , nameof ( srcString ) ) ;
550
593
Check . Argument . IsNotNull ( padding , nameof ( padding ) ) ;
551
594
552
- using ( RSA rsa = RSA . Create ( ) )
595
+ RSA rsa ;
596
+ if ( isPemKey )
597
+ {
598
+ rsa = RsaProvider . FromPem ( publicKey ) ;
599
+ }
600
+ else
553
601
{
602
+ rsa = RSA . Create ( ) ;
554
603
rsa . FromJsonString ( publicKey ) ;
604
+ }
605
+
606
+ using ( rsa )
607
+ {
555
608
var maxLength = GetMaxRsaEncryptLength ( rsa , padding ) ;
556
609
var rawBytes = Encoding . UTF8 . GetBytes ( srcString ) ;
557
610
558
611
if ( rawBytes . Length > maxLength )
559
612
{
560
- throw new OutofMaxlengthException ( maxLength , $ "'{ srcString } ' is out of max length" ) ;
613
+ throw new OutofMaxlengthException ( $ "'{ srcString } ' is out of max encrypt length { maxLength } " , maxLength , rsa . KeySize , padding ) ;
561
614
}
562
615
563
616
byte [ ] encryptBytes = rsa . Encrypt ( rawBytes , padding ) ;
@@ -577,22 +630,45 @@ public static string RSADecrypt(string privateKey, string srcString)
577
630
return decryptStr ;
578
631
}
579
632
633
+ /// <summary>
634
+ /// RSA decrypt with pem key
635
+ /// </summary>
636
+ /// <param name="privateKey">pem private key</param>
637
+ /// <param name="scrString">src string</param>
638
+ /// <returns></returns>
639
+ public static string RSADecryptWithPem ( string privateKey , string srcString )
640
+ {
641
+ string decryptStr = RSADecrypt ( privateKey , srcString , RSAEncryptionPadding . Pkcs1 , true ) ;
642
+ return decryptStr ;
643
+ }
644
+
580
645
/// <summary>
581
646
/// RSA encrypt
582
647
/// </summary>
583
648
/// <param name="publicKey">public key</param>
584
649
/// <param name="srcString">src string</param>
585
650
/// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
651
+ /// <param name="isPemKey">set key is pem format,default is false</param>
586
652
/// <returns>encrypted string</returns>
587
- public static string RSADecrypt ( string privateKey , string srcString , RSAEncryptionPadding padding )
653
+ public static string RSADecrypt ( string privateKey , string srcString , RSAEncryptionPadding padding , bool isPemKey = false )
588
654
{
589
655
Check . Argument . IsNotEmpty ( privateKey , nameof ( privateKey ) ) ;
590
656
Check . Argument . IsNotEmpty ( srcString , nameof ( srcString ) ) ;
591
657
Check . Argument . IsNotNull ( padding , nameof ( padding ) ) ;
592
658
593
- using ( RSA rsa = RSA . Create ( ) )
659
+ RSA rsa ;
660
+ if ( isPemKey )
661
+ {
662
+ rsa = RsaProvider . FromPem ( privateKey ) ;
663
+ }
664
+ else
594
665
{
666
+ rsa = RSA . Create ( ) ;
595
667
rsa . FromJsonString ( privateKey ) ;
668
+ }
669
+
670
+ using ( rsa )
671
+ {
596
672
byte [ ] srcBytes = srcString . ToBytes ( ) ;
597
673
byte [ ] decryptBytes = rsa . Decrypt ( srcBytes , padding ) ;
598
674
return Encoding . UTF8 . GetString ( decryptBytes ) ;
@@ -637,6 +713,27 @@ public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
637
713
}
638
714
}
639
715
716
+ /// <summary>
717
+ /// Create an RSA key
718
+ /// </summary>
719
+ /// <param name="rsa">rsa</param>
720
+ /// <returns></returns>
721
+ public static RSAKey CreateRsaKey ( RSA rsa )
722
+ {
723
+ Check . Argument . IsNotNull ( rsa , nameof ( rsa ) ) ;
724
+
725
+ string publicKey = rsa . ToJsonString ( false ) ;
726
+ string privateKey = rsa . ToJsonString ( true ) ;
727
+
728
+ return new RSAKey ( )
729
+ {
730
+ PublicKey = publicKey ,
731
+ PrivateKey = privateKey ,
732
+ Exponent = rsa . ExportParameters ( false ) . Exponent . ToHexString ( ) ,
733
+ Modulus = rsa . ExportParameters ( false ) . Modulus . ToHexString ( )
734
+ } ;
735
+ }
736
+
640
737
/// <summary>
641
738
/// Get rsa encrypt max length
642
739
/// </summary>
0 commit comments