Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/stulzq/RSAUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
stulzq committed Feb 21, 2019
2 parents 2c04aec + 605cb88 commit b7fd0c2
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 55 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

Thanks for onovotny's [bc-csharp](https://github.com/onovotny/bc-csharp "bc-csharp")

[![Latest version](https://img.shields.io/nuget/v/XC.Framework.Security.RSAUtil.svg)](https://www.nuget.org/packages/XC.Framework.Security.RSAUtil/)
[![Latest version](https://img.shields.io/nuget/v/XC.RSAUtil.svg?style=flat-square)](https://www.nuget.org/packages/XC.RSAUtil/)

# Install

````shell
Install-Package XC.Framework.Security.RSAUtil -Version 1.0.1
Install-Package XC.RSAUtil
````

> The old package name is `XC.Framework.Security.RSAUtil`. Now renamed `XC.RSAUtil` and will continue to use.
# Doc

### Generate the key
Expand Down Expand Up @@ -89,6 +91,17 @@ var publicKey = keyList[1];
>Use class `RsaPemFormatHelper`.
- Format Pkcs1 format private key: `RsaPemFormatHelper.Pkcs1PrivateKeyFormat()`

- Remove the Pkcs1 format private key format: `RsaPemFormatHelper.Pkcs1PrivateKeyFormatRemove()`

- Format Pkcs8 format private key: `RsaPemFormatHelper.Pkcs8PrivateKeyFormat()`

- Remove the Pkcs8 format private key format: `RsaPemFormatHelper.Pkcs8PrivateKeyFormatRemove()`

## Reference component

[bc-csharp](https://github.com/onovotny/bc-csharp "bc-csharp") - onovotny

## Cases

[dotnetrsa](https://github.com/stulzq/dotnetrsa) - DotnetRSA is a .NET Core Global Tool.Dotnet RSA Tool can help you generate xml pkcs1, pkcs8 three kinds of format keys, and supports three types of mutual conversion.
14 changes: 12 additions & 2 deletions README_Chinese.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# RSAUtil
.NET Core RSA算法使用帮助工具,支持数据加密,解密,签名和验证签名,支持xml,pkcs1,pkcs8三种密钥格式,支持这三种格式的密钥转换。最后还支持pem格式化。

[![Latest version](https://img.shields.io/nuget/v/XC.Framework.Security.RSAUtil.svg)](https://www.nuget.org/packages/XC.Framework.Security.RSAUtil/)
[![Latest version](https://img.shields.io/nuget/v/XC.RSAUtil.svg?style=flat-square)](https://www.nuget.org/packages/XC.RSAUtil/)


# Nuget安装
````shell
Install-Package XC.Framework.Security.RSAUtil -Version 1.0.1
Install-Package XC.RSAUtil
````

> 旧的Nuget包名为 `XC.Framework.Security.RSAUtil`,它因为太长已经被废弃了. 现在重命名为 `XC.RSAUtil` 并将一直使用。
# 文档

### 生成密钥
Expand Down Expand Up @@ -90,3 +92,11 @@ var publicKey = keyList [1];
- 删除Pkcs1格式私钥格式:`RsaPemFormatHelper.Pkcs1PrivateKeyFormatRemove()`
- 格式化Pkcs8格式私钥:`RsaPemFormatHelper.Pkcs8PrivateKeyFormat()`
- 删除Pkcs8格式的私钥格式:`RsaPemFormatHelper.Pkcs8PrivateKeyFormatRemove()`

## 使用的开源组件

[bc-csharp](https://github.com/onovotny/bc-csharp "bc-csharp") - onovotny

## 案例

[dotnetrsa](https://github.com/stulzq/dotnetrsa) - DotnetRSA 是一个利用 .NET Core 2.1 开发的 .NET Global Tool,是可以想npm全局安装一样,安装在你的系统中,只需敲一行命令便可以快速生成RSA加密算法所需的秘钥,目前支持三种格式的秘钥,分别为:xml、pkcs1、pkcs8。它还支持三种格式秘钥的想换转换。
4 changes: 2 additions & 2 deletions XC.Framework.Security.sln → RSAUtil.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XC.Framework.Security.Test", "XC.Framework.Security.Test\XC.Framework.Security.Test.csproj", "{939349EF-A767-49C5-9925-6C2705D6D2F0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XC.RSAUtil.Test", "XC.RSAUtil.Test\XC.RSAUtil.Test.csproj", "{939349EF-A767-49C5-9925-6C2705D6D2F0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XC.Framework.Security.RSAUtil", "XC.Framework.Security.RSAUtil\XC.Framework.Security.RSAUtil.csproj", "{1EBBF9AB-132C-4B9B-A951-31618B713003}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XC.RSAUtil", "XC.RSAUtil\XC.RSAUtil.csproj", "{1EBBF9AB-132C-4B9B-A951-31618B713003}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XC.BouncyCastle.Crypto", "BouncyCastle.Crypto\XC.BouncyCastle.Crypto.csproj", "{0E7A4128-66AF-4111-A835-51D9BF4371B6}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using XC.Framework.Security.RSAUtil;

namespace XC.Framework.Security.Test
namespace XC.RSAUtil.Test
{
class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\XC.Framework.Security.RSAUtil\XC.Framework.Security.RSAUtil.csproj" />
<ProjectReference Include="..\XC.RSAUtil\XC.RSAUtil.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Security.Cryptography;
using System.Text;

namespace XC.Framework.Security.RSAUtil
namespace XC.RSAUtil
{
public abstract class RSAUtilBase
{
Expand Down Expand Up @@ -53,24 +53,36 @@ public string Decrypt(string data, RSAEncryptionPadding padding)
/// <returns></returns>
public string SignData(string data, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding)
{
if (PrivateRsa == null)
{
throw new ArgumentException("private key can not null");
}
var dataBytes = DataEncoding.GetBytes(data);
var res = PrivateRsa.SignData(dataBytes, hashAlgorithmName, padding);
return Convert.ToBase64String(res);
var res = SignDataGetBytes(data, hashAlgorithmName, padding);
return Convert.ToBase64String(res);
}

/// <summary>
/// Use public key to verify data signature
/// </summary>
/// <param name="data">Need to verify the signature data</param>
/// <param name="sign">sign</param>
/// <param name="hashAlgorithmName">Signed hash algorithm name</param>
/// <param name="padding">Signature padding algorithm</param>
/// <returns></returns>
public bool VerifyData(string data, string sign, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding)
/// <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>Sign bytes</returns>
public byte[] SignDataGetBytes(string data, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding)
{
if (PrivateRsa == null)
{
throw new ArgumentException("private key can not null");
}
var dataBytes = DataEncoding.GetBytes(data);
return PrivateRsa.SignData(dataBytes, hashAlgorithmName, padding);
}

/// <summary>
/// Use public key to verify data signature
/// </summary>
/// <param name="data">Need to verify the signature data</param>
/// <param name="sign">sign</param>
/// <param name="hashAlgorithmName">Signed hash algorithm name</param>
/// <param name="padding">Signature padding algorithm</param>
/// <returns></returns>
public bool VerifyData(string data, string sign, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding)
{
if (PublicRsa == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;

namespace XC.Framework.Security.RSAUtil
namespace XC.RSAUtil
{
/// <summary>
/// RSA Key Convert Class
Expand Down Expand Up @@ -44,7 +44,7 @@ public static string PublicKeyPemToXml(string publicKey)
}

/// <summary>
/// Public Key Convert xml->xml
/// Public Key Convert xml->pem
/// </summary>
/// <param name="publicKey"></param>
/// <returns></returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;

namespace XC.Framework.Security.RSAUtil
namespace XC.RSAUtil
{
/// <summary>
/// Rsa Key Generator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace XC.Framework.Security.RSAUtil
namespace XC.RSAUtil
{
public class RsaPemFormatHelper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;

namespace XC.Framework.Security.RSAUtil
namespace XC.RSAUtil
{
/// <summary>
/// RSA pkcs1 format key helper class
/// Author:Zhiqiang Li
/// </summary>
public class RsaPkcs1Util:RSAUtilBase
public class RsaPkcs1Util : RSAUtilBase
{
public RsaPkcs1Util(Encoding encoding, string publicKey, string privateKey = null, int keySize = 2048)
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public RsaPkcs1Util(Encoding encoding, string publicKey, string privateKey = nul
};
PublicRsa.ImportParameters(pubRasp);
}

}

if (!string.IsNullOrEmpty(publicKey))
Expand All @@ -52,14 +52,14 @@ public RsaPkcs1Util(Encoding encoding, string publicKey, string privateKey = nul
PublicRsa.ImportParameters(CreateRsapFromPublicKey(publicKey));
}

DataEncoding = encoding;
DataEncoding = encoding ?? Encoding.UTF8;
}
/// <summary>
/// Create an RSA parameter based on the xml format public key
/// </summary>
/// <param name="publicKey"></param>
/// <returns></returns>
protected sealed override RSAParameters CreateRsapFromPublicKey(string publicKey)
/// <summary>
/// Create an RSA parameter based on the xml format public key
/// </summary>
/// <param name="publicKey"></param>
/// <returns></returns>
protected sealed override RSAParameters CreateRsapFromPublicKey(string publicKey)
{
publicKey = RsaPemFormatHelper.PublicKeyFormat(publicKey);

Expand All @@ -75,12 +75,12 @@ protected sealed override RSAParameters CreateRsapFromPublicKey(string publicKey
return rsap;
}

/// <summary>
/// Create an RSA parameter based on the xml format private key
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
protected sealed override RSAParameters CreateRsapFromPrivateKey(string privateKey)
/// <summary>
/// Create an RSA parameter based on the xml format private key
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
protected sealed override RSAParameters CreateRsapFromPrivateKey(string privateKey)
{
privateKey = RsaPemFormatHelper.Pkcs1PrivateKeyFormat(privateKey);

Expand All @@ -90,7 +90,7 @@ protected sealed override RSAParameters CreateRsapFromPrivateKey(string privateK
throw new Exception("Private key format is incorrect");
}
RsaPrivateCrtKeyParameters rsaPrivateCrtKeyParameters =
(RsaPrivateCrtKeyParameters) PrivateKeyFactory.CreateKey(
(RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(
PrivateKeyInfoFactory.CreatePrivateKeyInfo(asymmetricCipherKeyPair.Private));
var rsap = new RSAParameters();
rsap.Modulus = rsaPrivateCrtKeyParameters.Modulus.ToByteArrayUnsigned();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;

namespace XC.Framework.Security.RSAUtil
namespace XC.RSAUtil
{
/// <summary>
/// RSA pkcs8 format key helper class
Expand Down Expand Up @@ -47,7 +47,7 @@ public RsaPkcs8Util(Encoding dataEncoding, string publicKey, string privateKey =
PublicRsa.ImportParameters(pubRsap);
}

DataEncoding = dataEncoding;
DataEncoding = dataEncoding ?? Encoding.UTF8;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Text;
using System.Xml.Linq;

namespace XC.Framework.Security.RSAUtil
namespace XC.RSAUtil
{
/// <summary>
/// .net core xml format The RSA key helper classes are compatible with the xml formatted keys used by the .NET Framework
Expand Down Expand Up @@ -59,7 +59,7 @@ public RsaXmlUtil(Encoding dataEncoding,string publicKey, string privateKey = nu
}


DataEncoding = dataEncoding;
DataEncoding = dataEncoding ?? Encoding.UTF8;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<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.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit b7fd0c2

Please sign in to comment.