PnPeople.Security 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package PnPeople.Security --version 1.0.0
NuGet\Install-Package PnPeople.Security -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PnPeople.Security" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PnPeople.Security --version 1.0.0
#r "nuget: PnPeople.Security, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PnPeople.Security as a Cake Addin
#addin nuget:?package=PnPeople.Security&version=1.0.0

// Install PnPeople.Security as a Cake Tool
#tool nuget:?package=PnPeople.Security&version=1.0.0

PnPeople.Security

SEED Cryptography Algorithm Library for .NET Standard 2.0+

How to use

You can use this library to convert your SEED encrypted private key into a .NET standard RSA instance.

  1. First read the .der file and the .key file respectively into a byte array.
  2. Create an instance of the System.Security.Cryptography.X509Certificates.X509Certificate2 class to read the public key data.
  3. Create an instance of the Mono.Security.Cryptography.PKCS8.EncryptedPrivateKeyInfo class to read the private key data.
  4. Create an instance of the PnPeople.Security.SHASEEDDecryptor class.
  5. Prepare the certificate password by inputting it from the user.
  6. When calling the Decrypt function of the SHASEEDDecryptor class, pass the secret key's algorithm, Salt, count of iterations, encrypted data, and certificate password.
  7. Call the DecodeRSA function of the Mono.Security.PKCS8.PrivateKeyInfo class to change the decrypted secret key to a standard RSA provider instance in .NET.

Here is the sample application code.

using Mono.Security.Cryptography;
using System;
using System.IO;
using System.Linq;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

var folder = Path.Combine(
    Environment.GetEnvironmentVariable("USERPROFILE"),
    "AppData", "LocalLow", "NPKI");

foreach (var eachProviderDirectory in Directory.GetDirectories(folder))
{
    Console.WriteLine($"[{Path.GetFileName(eachProviderDirectory)}]");

    foreach (var eachDirectory in Directory.GetDirectories(Path.Combine(eachProviderDirectory, "USER")))
    {
        Console.WriteLine($"{Path.GetFileName(eachDirectory)}");
        var certFile = Directory.GetFiles(eachDirectory, "*.der", SearchOption.TopDirectoryOnly).FirstOrDefault();

        X509Certificate cert = null;
        X509Certificate2 token = null;

        if (certFile != null && File.Exists(certFile))
        {
            cert = X509Certificate.CreateFromCertFile(certFile);
            token = new X509Certificate2(cert);

            Console.WriteLine("- IssuerName: " + token.Issuer);
            Console.WriteLine("- KeyAlgorithm: " + token.GetKeyAlgorithm());
            Console.WriteLine("- KeyAlgorithmParameters: " + token.GetKeyAlgorithmParametersString());
            Console.WriteLine("- Name: " + token.Subject);
            Console.WriteLine("- PublicKey: " + token.GetPublicKeyString());
            Console.WriteLine("- SerialNumber: " + token.GetSerialNumberString());
            Console.WriteLine("- HasPrivateKey: " + token.HasPrivateKey);

            var currentDateTime = DateTime.Now;
            if (currentDateTime <= token.NotBefore)
            {
                Console.WriteLine("- Certificate is not valid yet.");
                continue;
            }
            else if (token.NotAfter <= currentDateTime)
            {
                Console.WriteLine("- Certificate has expiered.");
                continue;
            }
        }

        var keyFile = Directory.GetFiles(eachDirectory, "*.key", SearchOption.TopDirectoryOnly).FirstOrDefault();

        if (keyFile == null || !File.Exists(keyFile))
            continue;

        var bytes = File.ReadAllBytes(keyFile);
        Console.WriteLine("- KeyType: " + PKCS8.GetType(bytes));

        var encInfo = new PKCS8.EncryptedPrivateKeyInfo(bytes);
        Console.WriteLine("- Algorithm: " + encInfo.Algorithm);

        Console.Write("- Type private key password: ");

        SHASEEDDecryptor p12 = new SHASEEDDecryptor();
        var passwd = ReadPasswordFromConsole();
        var decrypted = p12.Decrypt(encInfo.Algorithm, encInfo.Salt, encInfo.IterationCount, encInfo.EncryptedData, passwd);

        if (decrypted != null)
        {
            var keyInfo = new PKCS8.PrivateKeyInfo(decrypted);
            var provider = PKCS8.PrivateKeyInfo.DecodeRSA(keyInfo.PrivateKey);

            var randomString = string.Concat(Enumerable.Range(1, (int)(Math.Abs(DateTime.Now.Ticks) % 9)).Select(x => Guid.NewGuid().ToString("n")));
            var buffer = Encoding.Default.GetBytes(randomString);
            var signed = provider.SignData(buffer, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
            var result = provider.VerifyData(buffer, signed, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);

            Console.WriteLine($"- Signature Validation Result: {(result ? "Valid" : "Invalid")}");

            if (result && token != null)
            {
                var tokenWithPrivateKey = token.CopyWithPrivateKey(provider);
                var pfxData = tokenWithPrivateKey.Export(X509ContentType.Pfx, passwd);
                var directoryPath = Path.GetDirectoryName(certFile);
                var pfxPath = Path.Combine(directoryPath, "signCert.pfx");
                File.WriteAllBytes(pfxPath, pfxData);

                if (File.Exists(pfxPath))
                    Console.WriteLine($"- PFX Converted: {pfxPath}");
            }
        }
        else
        {
            Console.WriteLine($"- Cannot decrypt private key");
        }
    }
}

License

This project is licensed under the MIT License.

Original Source Code Excerpted From:

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on PnPeople.Security:

Repository Stars
yourtablecloth/TableCloth
식탁보 프로젝트
Version Downloads Last updated
1.1.0 1,710 3/8/2022
1.0.0 398 3/7/2022