Skip to content

Commit 7f9556c

Browse files
committed
Rsa加密解密新增方法,提供byte[]数据加密解密,同时返回byte[]数据。 #32
1 parent 978326b commit 7f9556c

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

src/NETCore.Encrypt/EncryptProvider.cs

+126-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public static void RijndaelEncrypt(string data, string key)
328328
rijndael.Mode = CipherMode.ECB;
329329
rijndael.Padding = PaddingMode.PKCS7;
330330
rijndael.KeySize = 256;
331-
331+
332332
}
333333

334334

@@ -690,6 +690,8 @@ public static string RSAEncrypt(string publicKey, string srcString)
690690
return encryptStr;
691691
}
692692

693+
694+
693695
/// <summary>
694696
/// RSA encrypt with pem key
695697
/// </summary>
@@ -702,6 +704,8 @@ public static string RSAEncryptWithPem(string publicKey, string srcString)
702704
return encryptStr;
703705
}
704706

707+
708+
705709
/// <summary>
706710
/// RSA encrypt
707711
/// </summary>
@@ -742,6 +746,70 @@ public static string RSAEncrypt(string publicKey, string srcString, RSAEncryptio
742746
}
743747
}
744748

749+
/// <summary>
750+
/// RSA encrypt
751+
/// </summary>
752+
/// <param name="publicKey">public key</param>
753+
/// <param name="data">data byte[]</param>
754+
/// <returns>encrypted byte[]</returns>
755+
public static byte[] RSAEncrypt(string publicKey, byte[] data)
756+
{
757+
byte[] encryptBytes = RSAEncrypt(publicKey, data, RSAEncryptionPadding.OaepSHA512);
758+
return encryptBytes;
759+
}
760+
761+
/// <summary>
762+
/// RSA encrypt with pem key
763+
/// </summary>
764+
/// <param name="publicKey">pem public key</param>
765+
/// <param name="data">data byte[]</param>
766+
/// <returns></returns>
767+
public static byte[] RSAEncryptWithPem(string publicKey, byte[] data)
768+
{
769+
byte[] encryptBytes = RSAEncrypt(publicKey, data, RSAEncryptionPadding.Pkcs1, true);
770+
return encryptBytes;
771+
}
772+
773+
/// <summary>
774+
/// RSA encrypt
775+
/// </summary>
776+
/// <param name="publicKey">public key</param>
777+
/// <param name="data">data byte[]</param>
778+
/// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
779+
/// <param name="isPemKey">set key is pem format,default is false</param>
780+
/// <returns>encrypted byte[]</returns>
781+
public static byte[] RSAEncrypt(string publicKey, byte[] data, RSAEncryptionPadding padding, bool isPemKey = false)
782+
{
783+
Check.Argument.IsNotEmpty(publicKey, nameof(publicKey));
784+
Check.Argument.IsNotNull(data, nameof(data));
785+
Check.Argument.IsNotNull(padding, nameof(padding));
786+
787+
RSA rsa;
788+
if (isPemKey)
789+
{
790+
rsa = RsaProvider.FromPem(publicKey);
791+
}
792+
else
793+
{
794+
rsa = RSA.Create();
795+
rsa.FromJsonString(publicKey);
796+
}
797+
798+
using (rsa)
799+
{
800+
var maxLength = GetMaxRsaEncryptLength(rsa, padding);
801+
var rawBytes = data;
802+
803+
if (rawBytes.Length > maxLength)
804+
{
805+
throw new OutofMaxlengthException($"data is out of max encrypt length {maxLength}", maxLength, rsa.KeySize, padding);
806+
}
807+
808+
byte[] encryptBytes = rsa.Encrypt(rawBytes, padding);
809+
return encryptBytes;
810+
}
811+
}
812+
745813
/// <summary>
746814
/// RSA decrypt
747815
/// </summary>
@@ -799,6 +867,63 @@ public static string RSADecrypt(string privateKey, string srcString, RSAEncrypti
799867
}
800868
}
801869

870+
/// <summary>
871+
/// RSA decrypt
872+
/// </summary>
873+
/// <param name="privateKey">private key</param>
874+
/// <param name="data">encrypted byte[]</param>
875+
/// <returns>Decrypted string</returns>
876+
public static byte[] RSADecrypt(string privateKey, byte[] data)
877+
{
878+
byte[] decryptBytes = RSADecrypt(privateKey, data, RSAEncryptionPadding.OaepSHA512);
879+
return decryptBytes;
880+
}
881+
882+
/// <summary>
883+
/// RSA decrypt with pem key
884+
/// </summary>
885+
/// <param name="privateKey">pem private key</param>
886+
/// <param name="data">encrypted byte[]</param>
887+
/// <returns></returns>
888+
public static byte[] RSADecryptWithPem(string privateKey, byte[] data)
889+
{
890+
byte[] decryptBytes = RSADecrypt(privateKey, data, RSAEncryptionPadding.Pkcs1, true);
891+
return decryptBytes;
892+
}
893+
894+
/// <summary>
895+
/// RSA encrypt
896+
/// </summary>
897+
/// <param name="publicKey">public key</param>
898+
/// <param name="data">src string</param>
899+
/// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
900+
/// <param name="isPemKey">set key is pem format,default is false</param>
901+
/// <returns>encrypted string</returns>
902+
public static byte[] RSADecrypt(string privateKey, byte[] data, RSAEncryptionPadding padding, bool isPemKey = false)
903+
{
904+
Check.Argument.IsNotEmpty(privateKey, nameof(privateKey));
905+
Check.Argument.IsNotNull(data, nameof(data));
906+
Check.Argument.IsNotNull(padding, nameof(padding));
907+
908+
RSA rsa;
909+
if (isPemKey)
910+
{
911+
rsa = RsaProvider.FromPem(privateKey);
912+
}
913+
else
914+
{
915+
rsa = RSA.Create();
916+
rsa.FromJsonString(privateKey);
917+
}
918+
919+
using (rsa)
920+
{
921+
byte[] srcBytes = data;
922+
byte[] decryptBytes = rsa.Decrypt(srcBytes, padding);
923+
return decryptBytes;
924+
}
925+
}
926+
802927
/// <summary>
803928
/// RSA from json string
804929
/// </summary>

0 commit comments

Comments
 (0)