Skip to content

Commit

Permalink
v1.3.0 Support long string encryption and decryption.
Browse files Browse the repository at this point in the history
  • Loading branch information
stulzq committed Jul 9, 2019
1 parent e5d84ee commit 9e5d930
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 19 deletions.
9 changes: 9 additions & 0 deletions XC.RSAUtil.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Security.Cryptography;
using System.Text;

namespace XC.RSAUtil.Test
{
Expand All @@ -19,6 +21,13 @@ static void Main(string[] args)
Console.WriteLine("public key pkcs1->xml:");
Console.WriteLine(RsaKeyConvert.PublicKeyPemToXml(publicKey));

var bigDataRsa=new RsaPkcs1Util(Encoding.UTF8, publicKey,privateKey,2048);
var str = bigDataRsa.EncryptBigData("abcdefg", 3, "$", RSAEncryptionPadding.Pkcs1);
Console.WriteLine("Big Data Encrypt:");
Console.WriteLine(str);
Console.WriteLine("Big Data Decrypt:");
Console.WriteLine(string.Join("", bigDataRsa.DecryptBigData(str, "$", RSAEncryptionPadding.Pkcs1)));

Console.ReadKey();
}
}
Expand Down
94 changes: 79 additions & 15 deletions XC.RSAUtil/RSAUtilBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,52 @@ public string Encrypt(string data, RSAEncryptionPadding padding)
return Convert.ToBase64String(resBytes);
}

/// <summary>
/// RSA private key is decrypted
/// </summary>
/// <param name="data">Need to decrypt the data</param>
/// <param name="padding">Padding algorithm</param>
/// <returns></returns>
public string Decrypt(string data, RSAEncryptionPadding padding)
/// <summary>
/// [Not recommended] RSA public key split encryption
/// <para>RSA encryption does not support too large data. In this case, symmetric encryption should be used, and RSA is used to encrypt symmetrically encrypted passwords.</para>
/// </summary>
/// <param name="data">Need to encrypt data</param>
/// <param name="splitLength">data split length</param>
/// <param name="connChar">Encrypted result link character</param>
/// <param name="padding">Padding algorithm</param>
/// <returns></returns>
public string EncryptBigData(string data,int splitLength,string connChar, RSAEncryptionPadding padding)
{
var sb=new StringBuilder();
if (splitLength >= data.Length)
{
throw new ArgumentOutOfRangeException(nameof(splitLength), "Split length cannot exceed the data length.");
}

var splitsNumber = Convert.ToInt32(Math.Ceiling(data.Length * 1.0 / splitLength));

var pointer = 0;
for (int i = 0; i < splitsNumber; i++)
{
string currentStr;
if (data.Length < pointer + splitLength)
{
currentStr = data.Substring(pointer, data.Length-pointer);
}
else
{
currentStr = data.Substring(pointer, splitLength);
}

sb.Append(Encrypt(currentStr, padding)+connChar);
pointer += splitLength;
}

return sb.ToString();
}

/// <summary>
/// RSA private key decrypted
/// </summary>
/// <param name="data">Need to decrypt the data</param>
/// <param name="padding">Padding algorithm</param>
/// <returns></returns>
public string Decrypt(string data, RSAEncryptionPadding padding)
{
if (PrivateRsa == null)
{
Expand All @@ -44,14 +83,39 @@ public string Decrypt(string data, RSAEncryptionPadding padding)
return DataEncoding.GetString(resBytes);
}

/// <summary>
/// Use private key for data signing
/// </summary>
/// <param name="data">Need to sign data</param>
/// <param name="hashAlgorithmName">Signed hash algorithm name</param>
/// <param name="padding">Signature padding algorithm</param>
/// <returns></returns>
public string SignData(string data, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding)
/// <summary>
/// [Not recommended] RSA private key split decrypted
/// <para>RSA encryption does not support too large data. In this case, symmetric encryption should be used, and RSA is used to encrypt symmetrically encrypted passwords.</para>
/// </summary>
/// <param name="connChar">Encrypted result link character</param>
/// <param name="data">Need to decrypt the data</param>
/// <param name="padding">Padding algorithm</param>
/// <returns></returns>
public string[] DecryptBigData(string data,string connChar, RSAEncryptionPadding padding)
{
if (PrivateRsa == null)
{
throw new ArgumentException("private key can not null");
}

var splitsData = data.Split(new[] {connChar}, StringSplitOptions.RemoveEmptyEntries);
var result = new string[splitsData.Length];
for (int i = 0; i < splitsData.Length; i++)
{
result[i] = Decrypt(splitsData[i], padding);
}

return result;
}

/// <summary>
/// Use private key for data signing
/// </summary>
/// <param name="data">Need to sign data</param>
/// <param name="hashAlgorithmName">Signed hash algorithm name</param>
/// <param name="padding">Signature padding algorithm</param>
/// <returns></returns>
public string SignData(string data, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding)
{
var res = SignDataGetBytes(data, hashAlgorithmName, padding);
return Convert.ToBase64String(res);
Expand Down
7 changes: 3 additions & 4 deletions XC.RSAUtil/XC.RSAUtil.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -10,9 +10,8 @@
<PackageLicenseUrl>https://github.com/stulzq/RSAUtil/blob/master/LICENSE</PackageLicenseUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageTags>RSA,RSA Xml,RSA Pkcs1,RSA Pkcs8,RSA Convert,RSA Format,Pem,RSAUtil,RSAHelper</PackageTags>
<Version>1.2.0</Version>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
<Version>1.3.0</Version>
<PackageReleaseNotes>Support long string encryption and decryption.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 9e5d930

Please sign in to comment.