Skip to content

Commit

Permalink
Rsa加密解密新增方法,提供byte[]数据加密解密,同时返回byte[]数据。 #32
Browse files Browse the repository at this point in the history
  • Loading branch information
myloveCc committed Apr 22, 2020
1 parent 978326b commit 7f9556c
Showing 1 changed file with 126 additions and 1 deletion.
127 changes: 126 additions & 1 deletion src/NETCore.Encrypt/EncryptProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public static void RijndaelEncrypt(string data, string key)
rijndael.Mode = CipherMode.ECB;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.KeySize = 256;

}


Expand Down Expand Up @@ -690,6 +690,8 @@ public static string RSAEncrypt(string publicKey, string srcString)
return encryptStr;
}



/// <summary>
/// RSA encrypt with pem key
/// </summary>
Expand All @@ -702,6 +704,8 @@ public static string RSAEncryptWithPem(string publicKey, string srcString)
return encryptStr;
}



/// <summary>
/// RSA encrypt
/// </summary>
Expand Down Expand Up @@ -742,6 +746,70 @@ public static string RSAEncrypt(string publicKey, string srcString, RSAEncryptio
}
}

/// <summary>
/// RSA encrypt
/// </summary>
/// <param name="publicKey">public key</param>
/// <param name="data">data byte[]</param>
/// <returns>encrypted byte[]</returns>
public static byte[] RSAEncrypt(string publicKey, byte[] data)
{
byte[] encryptBytes = RSAEncrypt(publicKey, data, RSAEncryptionPadding.OaepSHA512);
return encryptBytes;
}

/// <summary>
/// RSA encrypt with pem key
/// </summary>
/// <param name="publicKey">pem public key</param>
/// <param name="data">data byte[]</param>
/// <returns></returns>
public static byte[] RSAEncryptWithPem(string publicKey, byte[] data)
{
byte[] encryptBytes = RSAEncrypt(publicKey, data, RSAEncryptionPadding.Pkcs1, true);
return encryptBytes;
}

/// <summary>
/// RSA encrypt
/// </summary>
/// <param name="publicKey">public key</param>
/// <param name="data">data byte[]</param>
/// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
/// <param name="isPemKey">set key is pem format,default is false</param>
/// <returns>encrypted byte[]</returns>
public static byte[] RSAEncrypt(string publicKey, byte[] data, RSAEncryptionPadding padding, bool isPemKey = false)
{
Check.Argument.IsNotEmpty(publicKey, nameof(publicKey));
Check.Argument.IsNotNull(data, nameof(data));
Check.Argument.IsNotNull(padding, nameof(padding));

RSA rsa;
if (isPemKey)
{
rsa = RsaProvider.FromPem(publicKey);
}
else
{
rsa = RSA.Create();
rsa.FromJsonString(publicKey);
}

using (rsa)
{
var maxLength = GetMaxRsaEncryptLength(rsa, padding);
var rawBytes = data;

if (rawBytes.Length > maxLength)
{
throw new OutofMaxlengthException($"data is out of max encrypt length {maxLength}", maxLength, rsa.KeySize, padding);
}

byte[] encryptBytes = rsa.Encrypt(rawBytes, padding);
return encryptBytes;
}
}

/// <summary>
/// RSA decrypt
/// </summary>
Expand Down Expand Up @@ -799,6 +867,63 @@ public static string RSADecrypt(string privateKey, string srcString, RSAEncrypti
}
}

/// <summary>
/// RSA decrypt
/// </summary>
/// <param name="privateKey">private key</param>
/// <param name="data">encrypted byte[]</param>
/// <returns>Decrypted string</returns>
public static byte[] RSADecrypt(string privateKey, byte[] data)
{
byte[] decryptBytes = RSADecrypt(privateKey, data, RSAEncryptionPadding.OaepSHA512);
return decryptBytes;
}

/// <summary>
/// RSA decrypt with pem key
/// </summary>
/// <param name="privateKey">pem private key</param>
/// <param name="data">encrypted byte[]</param>
/// <returns></returns>
public static byte[] RSADecryptWithPem(string privateKey, byte[] data)
{
byte[] decryptBytes = RSADecrypt(privateKey, data, RSAEncryptionPadding.Pkcs1, true);
return decryptBytes;
}

/// <summary>
/// RSA encrypt
/// </summary>
/// <param name="publicKey">public key</param>
/// <param name="data">src string</param>
/// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
/// <param name="isPemKey">set key is pem format,default is false</param>
/// <returns>encrypted string</returns>
public static byte[] RSADecrypt(string privateKey, byte[] data, RSAEncryptionPadding padding, bool isPemKey = false)
{
Check.Argument.IsNotEmpty(privateKey, nameof(privateKey));
Check.Argument.IsNotNull(data, nameof(data));
Check.Argument.IsNotNull(padding, nameof(padding));

RSA rsa;
if (isPemKey)
{
rsa = RsaProvider.FromPem(privateKey);
}
else
{
rsa = RSA.Create();
rsa.FromJsonString(privateKey);
}

using (rsa)
{
byte[] srcBytes = data;
byte[] decryptBytes = rsa.Decrypt(srcBytes, padding);
return decryptBytes;
}
}

/// <summary>
/// RSA from json string
/// </summary>
Expand Down

0 comments on commit 7f9556c

Please sign in to comment.