Skip to content

Commit

Permalink
Update quick start docs
Browse files Browse the repository at this point in the history
  • Loading branch information
poulad committed Sep 7, 2018
1 parent 6a509e4 commit c43b59d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Quickstart/EncryptionKeyParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Quickstart
{
/// <summary>
/// Represents the standard parameters of RSA algorithm
/// </summary>
public struct EncryptionKeyParameters
{
public byte[] E;
Expand Down
20 changes: 17 additions & 3 deletions src/Quickstart/EncryptionKeyUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,31 @@

namespace Quickstart
{
/// <summary>
/// Provides utilities for serialization of an RSA key parameters
/// </summary>
public static class EncryptionKeyUtility
{
/// <summary>
/// Serializes parameters of an RSA key in PEM format to JSON
/// </summary>
/// <param name="key">RSA private key in PEM format</param>
/// <returns>JSON string representing RSA parameters of the <paramref name="key"/></returns>
public static string SerializeRsaParameters(string key)
{
PemReader pemReader = new PemReader(new StringReader(key));
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair) pemReader.ReadObject();
RSAParameters parameters = DotNetUtilities.ToRSAParameters(keyPair.Private as RsaPrivateCrtKeyParameters);
return JsonConvert.SerializeObject((EncryptionKeyParameters)parameters);
return JsonConvert.SerializeObject((EncryptionKeyParameters) parameters);
}

/// <summary>
/// Creates an instance of <see cref="RSA"/> from key parameters serialized in JSON.
/// Use <see cref="SerializeRsaParameters"/> method to generate the expected JSON string.
/// </summary>
/// <param name="json">JSON-serialized RSA key parameters</param>
/// <returns>Created RSA instance representing the encryption key</returns>
public static RSA GetRsaKeyFromJson(string json) =>
RSA.Create((RSAParameters)JsonConvert.DeserializeObject<EncryptionKeyParameters>(json));
RSA.Create((RSAParameters) JsonConvert.DeserializeObject<EncryptionKeyParameters>(json));
}
}

0 comments on commit c43b59d

Please sign in to comment.