Skip to content

Commit

Permalink
Add pem key feature about issues #27 #28
Browse files Browse the repository at this point in the history
  • Loading branch information
myloveCc committed Aug 30, 2019
1 parent 0185cef commit ae4de01
Show file tree
Hide file tree
Showing 5 changed files with 586 additions and 5 deletions.
107 changes: 102 additions & 5 deletions src/NETCore.Encrypt/EncryptProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,36 @@ public static byte[] DESDecrypt(byte[] data, string key)

#region RSA

/// <summary>
/// RSA Converter to pem
/// </summary>
/// <param name="isPKCS8"></param>
/// <returns></returns>
public static (string publicPem, string privatePem) RSAToPem(bool isPKCS8)
{
var rsaKey = CreateRsaKey();

using (RSA rsa = RSA.Create())
{
rsa.FromJsonString(rsaKey.PrivateKey);

var publicPem = RsaProvider.ToPem(rsa, false, isPKCS8);
var privatePem = RsaProvider.ToPem(rsa, true, isPKCS8);

return (publicPem, privatePem);
}
}

/// <summary>
/// RSA From pem
/// </summary>
/// <param name="pem"></param>
/// <returns></returns>
public static RSA RSAFromPem(string pem)
{
Check.Argument.IsNotEmpty(pem, nameof(pem));
return RsaProvider.FromPem(pem);
}

/// <summary>
/// RSA Sign
Expand Down Expand Up @@ -536,28 +566,51 @@ public static string RSAEncrypt(string publicKey, string srcString)
return encryptStr;
}

/// <summary>
/// RSA encrypt with pem key
/// </summary>
/// <param name="publicKey">pem public key</param>
/// <param name="scrString">src string</param>
/// <returns></returns>
public static string RSAEncryptWithPem(string publicKey, string srcString)
{
string encryptStr = RSAEncrypt(publicKey, srcString, RSAEncryptionPadding.Pkcs1, true);
return encryptStr;
}

/// <summary>
/// RSA encrypt
/// </summary>
/// <param name="publicKey">public key</param>
/// <param name="srcString">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 string RSAEncrypt(string publicKey, string srcString, RSAEncryptionPadding padding)
public static string RSAEncrypt(string publicKey, string srcString, RSAEncryptionPadding padding, bool isPemKey = false)
{
Check.Argument.IsNotEmpty(publicKey, nameof(publicKey));
Check.Argument.IsNotEmpty(srcString, nameof(srcString));
Check.Argument.IsNotNull(padding, nameof(padding));

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

using (rsa)
{
var maxLength = GetMaxRsaEncryptLength(rsa, padding);
var rawBytes = Encoding.UTF8.GetBytes(srcString);

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

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

/// <summary>
/// RSA decrypt with pem key
/// </summary>
/// <param name="privateKey">pem private key</param>
/// <param name="scrString">src string</param>
/// <returns></returns>
public static string RSADecryptWithPem(string privateKey, string srcString)
{
string decryptStr = RSADecrypt(privateKey, srcString, RSAEncryptionPadding.Pkcs1, true);
return decryptStr;
}

/// <summary>
/// RSA encrypt
/// </summary>
/// <param name="publicKey">public key</param>
/// <param name="srcString">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 string RSADecrypt(string privateKey, string srcString, RSAEncryptionPadding padding)
public static string RSADecrypt(string privateKey, string srcString, RSAEncryptionPadding padding, bool isPemKey = false)
{
Check.Argument.IsNotEmpty(privateKey, nameof(privateKey));
Check.Argument.IsNotEmpty(srcString, nameof(srcString));
Check.Argument.IsNotNull(padding, nameof(padding));

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

using (rsa)
{
byte[] srcBytes = srcString.ToBytes();
byte[] decryptBytes = rsa.Decrypt(srcBytes, padding);
return Encoding.UTF8.GetString(decryptBytes);
Expand Down Expand Up @@ -637,6 +713,27 @@ public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
}
}

/// <summary>
/// Create an RSA key
/// </summary>
/// <param name="rsa">rsa</param>
/// <returns></returns>
public static RSAKey CreateRsaKey(RSA rsa)
{
Check.Argument.IsNotNull(rsa, nameof(rsa));

string publicKey = rsa.ToJsonString(false);
string privateKey = rsa.ToJsonString(true);

return new RSAKey()
{
PublicKey = publicKey,
PrivateKey = privateKey,
Exponent = rsa.ExportParameters(false).Exponent.ToHexString(),
Modulus = rsa.ExportParameters(false).Modulus.ToHexString()
};
}

/// <summary>
/// Get rsa encrypt max length
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions src/NETCore.Encrypt/Extensions/Internal/ArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NETCore.Encrypt.Extensions.Internal
{
internal static class ArrayExtensions
{
/// <summary>
/// sub datas from array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arr"></param>
/// <param name="start"></param>
/// <param name="count"></param>
/// <returns></returns>
internal static T[] Sub<T>(this T[] arr, int start, int count)
{
T[] val = new T[count];
for (var i = 0; i < count; i++)
{
val[i] = arr[start + i];
}
return val;
}
}
}
20 changes: 20 additions & 0 deletions src/NETCore.Encrypt/Extensions/Internal/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace NETCore.Encrypt.Extensions.Internal
{
internal static class StreamExtensions
{
/// <summary>
/// Stream write all bytes
/// </summary>
/// <param name="stream"></param>
/// <param name="byts"></param>
static public void WriteAll(this Stream stream, byte[] byts)
{
stream.Write(byts, 0, byts.Length);
}
}
}
Loading

0 comments on commit ae4de01

Please sign in to comment.