Skip to content

Commit ae4de01

Browse files
committed
Add pem key feature about issues #27 #28
1 parent 0185cef commit ae4de01

File tree

5 files changed

+586
-5
lines changed

5 files changed

+586
-5
lines changed

src/NETCore.Encrypt/EncryptProvider.cs

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,36 @@ public static byte[] DESDecrypt(byte[] data, string key)
449449

450450
#region RSA
451451

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+
}
452482

453483
/// <summary>
454484
/// RSA Sign
@@ -536,28 +566,51 @@ public static string RSAEncrypt(string publicKey, string srcString)
536566
return encryptStr;
537567
}
538568

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+
539581
/// <summary>
540582
/// RSA encrypt
541583
/// </summary>
542584
/// <param name="publicKey">public key</param>
543585
/// <param name="srcString">src string</param>
544586
/// <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>
545588
/// <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)
547590
{
548591
Check.Argument.IsNotEmpty(publicKey, nameof(publicKey));
549592
Check.Argument.IsNotEmpty(srcString, nameof(srcString));
550593
Check.Argument.IsNotNull(padding, nameof(padding));
551594

552-
using (RSA rsa = RSA.Create())
595+
RSA rsa;
596+
if (isPemKey)
597+
{
598+
rsa = RsaProvider.FromPem(publicKey);
599+
}
600+
else
553601
{
602+
rsa = RSA.Create();
554603
rsa.FromJsonString(publicKey);
604+
}
605+
606+
using (rsa)
607+
{
555608
var maxLength = GetMaxRsaEncryptLength(rsa, padding);
556609
var rawBytes = Encoding.UTF8.GetBytes(srcString);
557610

558611
if (rawBytes.Length > maxLength)
559612
{
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);
561614
}
562615

563616
byte[] encryptBytes = rsa.Encrypt(rawBytes, padding);
@@ -577,22 +630,45 @@ public static string RSADecrypt(string privateKey, string srcString)
577630
return decryptStr;
578631
}
579632

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+
580645
/// <summary>
581646
/// RSA encrypt
582647
/// </summary>
583648
/// <param name="publicKey">public key</param>
584649
/// <param name="srcString">src string</param>
585650
/// <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>
586652
/// <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)
588654
{
589655
Check.Argument.IsNotEmpty(privateKey, nameof(privateKey));
590656
Check.Argument.IsNotEmpty(srcString, nameof(srcString));
591657
Check.Argument.IsNotNull(padding, nameof(padding));
592658

593-
using (RSA rsa = RSA.Create())
659+
RSA rsa;
660+
if (isPemKey)
661+
{
662+
rsa = RsaProvider.FromPem(privateKey);
663+
}
664+
else
594665
{
666+
rsa = RSA.Create();
595667
rsa.FromJsonString(privateKey);
668+
}
669+
670+
using (rsa)
671+
{
596672
byte[] srcBytes = srcString.ToBytes();
597673
byte[] decryptBytes = rsa.Decrypt(srcBytes, padding);
598674
return Encoding.UTF8.GetString(decryptBytes);
@@ -637,6 +713,27 @@ public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
637713
}
638714
}
639715

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+
640737
/// <summary>
641738
/// Get rsa encrypt max length
642739
/// </summary>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NETCore.Encrypt.Extensions.Internal
6+
{
7+
internal static class ArrayExtensions
8+
{
9+
/// <summary>
10+
/// sub datas from array
11+
/// </summary>
12+
/// <typeparam name="T"></typeparam>
13+
/// <param name="arr"></param>
14+
/// <param name="start"></param>
15+
/// <param name="count"></param>
16+
/// <returns></returns>
17+
internal static T[] Sub<T>(this T[] arr, int start, int count)
18+
{
19+
T[] val = new T[count];
20+
for (var i = 0; i < count; i++)
21+
{
22+
val[i] = arr[start + i];
23+
}
24+
return val;
25+
}
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace NETCore.Encrypt.Extensions.Internal
7+
{
8+
internal static class StreamExtensions
9+
{
10+
/// <summary>
11+
/// Stream write all bytes
12+
/// </summary>
13+
/// <param name="stream"></param>
14+
/// <param name="byts"></param>
15+
static public void WriteAll(this Stream stream, byte[] byts)
16+
{
17+
stream.Write(byts, 0, byts.Length);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)