Honoo.BouncyCastle.NetStyles 2.0.1

dotnet add package Honoo.BouncyCastle.NetStyles --version 2.0.1
NuGet\Install-Package Honoo.BouncyCastle.NetStyles -Version 2.0.1
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="Honoo.BouncyCastle.NetStyles" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Honoo.BouncyCastle.NetStyles --version 2.0.1
#r "nuget: Honoo.BouncyCastle.NetStyles, 2.0.1"
#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 Honoo.BouncyCastle.NetStyles as a Cake Addin
#addin nuget:?package=Honoo.BouncyCastle.NetStyles&version=2.0.1

// Install Honoo.BouncyCastle.NetStyles as a Cake Tool
#tool nuget:?package=Honoo.BouncyCastle.NetStyles&version=2.0.1

Honoo.BouncyCastle.NetStyles

INTRODUCTION

BouncyCastle's helpers. Refactoring by System.Security.Cryptography code styles.

USAGE

NuGet

https://www.nuget.org/packages/Honoo.BouncyCastle.NetStyles/

Namespace


using Honoo.BouncyCastle.NetStyles;

Hash


private static void Demo1()
{
    SHA1 sha1 = new SHA1();
    _ = sha1.ComputeFinal(_input);

    HashAlgorithm sha256 = HashAlgorithm.Create(HashAlgorithmName.SHA256);
    sha256.Update(_input);
    _ = sha256.ComputeFinal();
}

HMAC


private static void Demo2()
{
    HMAC hmac1 = HMAC.Create(HMACName.HMAC_SM3);
    byte[] key = new byte[66]; // Any length.
    Buffer.BlockCopy(_keyExchangePms, 0, key, 0, key.Length);
    hmac1.ImportParameters(key);
    _ = hmac1.ComputeFinal(_input);
}

CMAC


private static void Demo3()
{
    CMAC cmac1 = CMAC.Create(CMACName.AES_CMAC);
    byte[] key = new byte[192 / 8]; // 192 = AES legal key size bits.
    Buffer.BlockCopy(_keyExchangePms, 0, key, 0, key.Length);
    cmac1.ImportParameters(key);
    _ = cmac1.ComputeFinal(_input);
}

MAC


private static void Demo4()
{
    MAC mac1 = MAC.Create(MACName.Rijndael224_MAC);
    mac1.Mode = SymmetricCipherMode.CBC;
    mac1.Padding = SymmetricPaddingMode.TBC;
    byte[] key = new byte[160 / 8];  // 160 = Rijndael224 legal key size bits.
    byte[] iv = new byte[224 / 8];   // 224 = CBC mode limit same as Rijndael224 block size bits.
    Buffer.BlockCopy(_keyExchangePms, 0, key, 0, key.Length);
    Buffer.BlockCopy(_keyExchangePms, 0, iv, 0, iv.Length);
    mac1.ImportParameters(key, iv);
    _ = mac1.ComputeFinal(_input);
}

Symmetric encryption


private static void Demo1()
{
    SymmetricAlgorithm alg1 = SymmetricAlgorithm.Create(SymmetricAlgorithmName.Rijndael224);
    alg1.Mode = SymmetricCipherMode.CTR;
    alg1.Padding = SymmetricPaddingMode.TBC;
    Rijndael alg2 = new Rijndael(224) { Mode = SymmetricCipherMode.CTR, Padding = SymmetricPaddingMode.TBC };
    byte[] key = new byte[160 / 8];  // 160 = Rijndael224 legal key size bits.
    byte[] iv = new byte[224 / 8];   // 224 = CTR mode limit same as Rijndael block size bits.
    Buffer.BlockCopy(_keyExchangePms, 0, key, 0, key.Length);
    Buffer.BlockCopy(_keyExchangePms, 0, iv, 0, iv.Length);
    alg1.ImportParameters(key, iv);
    alg2.ImportParameters(key, iv);
    byte[] enc = alg1.EncryptFinal(_input);
    _ = alg2.DecryptFinal(enc);
}

private static void Demo3()
{
    SymmetricAlgorithm alg1 = SymmetricAlgorithm.Create(SymmetricAlgorithmName.HC128);
    HC128 alg2 = new HC128();
    byte[] key = new byte[128 / 8];  // 128 = HC128 legal key size bits.
    byte[] iv = new byte[128 / 8];   // 256 = HC128 legal iv size bits.
    Buffer.BlockCopy(_keyExchangePms, 0, key, 0, key.Length);
    Buffer.BlockCopy(_keyExchangePms, 0, iv, 0, iv.Length);
    alg1.ImportParameters(key, iv);
    alg2.ImportParameters(key, iv);
    byte[] enc = alg1.EncryptFinal(_input);
    _ = alg2.DecryptFinal(enc);
}

Asymmetric encryption


private static void Demo1()
{
    RSA rsa1 = new RSA();
    string publicKeyPem = rsa1.ExportPem(false);

    RSA rsa2 = (RSA)AsymmetricAlgorithm.Create(AsymmetricAlgorithmName.RSA);
    rsa2.ImportPem(publicKeyPem);

    byte[] enc = rsa2.Encrypt(_input);
    byte[] dec = rsa1.Decrypt(enc);
}

Signature


private static void Demo()
{
    ECDSA alg1 = (ECDSA)AsymmetricAlgorithm.Create(SignatureAlgorithmName.SHA256withECDSA);
    string publicKeyPem = alg1.ExportPem(false);
    byte[] signature = alg1.SignFinal(_input);

    if (SignatureAlgorithmName.TryGetAlgorithmName("sha256withecdsa", out SignatureAlgorithmName name))
    {
        ISignatureAlgorithm alg2 = AsymmetricAlgorithm.Create(name);
        alg2.ImportPem(publicKeyPem);

        alg2.VerifyUpdate(_input);
        bool same = alg2.VerifyFinal(signature);
    }
}

Certificate


private static void CreateCACert()
{
    //
    // Issuer work, Create CA private key and self sign certificate.
    //
    _issuerSignatureAlgorithm = SignatureAlgorithmName.SHA256withECDSA;
    ISignatureAlgorithm issuerAlgorithm = AsymmetricAlgorithm.Create(_issuerSignatureAlgorithm);
    byte[] issuerPrivateKeyInfo = issuerAlgorithm.ExportKeyInfo(true);
    X509CertificateRequestGenerator issuerCsrGenerator = new X509CertificateRequestGenerator(_issuerSignatureAlgorithm, issuerPrivateKeyInfo);
    issuerCsrGenerator.SubjectDN.Add(X509NameLabel.C, "CN");
    issuerCsrGenerator.SubjectDN.Add(X509NameLabel.CN, "Test CA");
    string issuerCsr = issuerCsrGenerator.GeneratePem();
    X509CertificateV3Generator v3Generator = new X509CertificateV3Generator(_issuerSignatureAlgorithm, issuerPrivateKeyInfo);
    v3Generator.IssuerDN.Add(X509NameLabel.C, "CN");
    v3Generator.IssuerDN.Add(X509NameLabel.CN, "Test CA");
    v3Generator.SetCertificateRequest(issuerCsr);
    _issuerCer = v3Generator.Generate(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(365));
    _issuerPrivateKeyInfo = issuerPrivateKeyInfo;
}

private static void CreateUserCert()
{
    //
    // Issuer work, Create key for subject.
    //
    SignatureAlgorithmName userSignatureAlgorithm = SignatureAlgorithmName.SM3withSM2; //Issuer define of allow user specify.
    ISignatureAlgorithm issuerCreateAlgorithmForSubject = AsymmetricAlgorithm.Create(userSignatureAlgorithm);
    string algorithmMechanism = userSignatureAlgorithm.Oid; // Send to user
    byte[] userPrivateKeyInfo = issuerCreateAlgorithmForSubject.ExportKeyInfo(true); // Send to user
    Org.BouncyCastle.Crypto.AsymmetricKeyParameter userPublicKey = issuerCreateAlgorithmForSubject.ExportParameters(false);

    //
    // User work, Create certificate request.
    //
    SignatureAlgorithmName.TryGetAlgorithmName(algorithmMechanism, out SignatureAlgorithmName userAlgorithmName);
    X509CertificateRequestGenerator userCreateCsr = new X509CertificateRequestGenerator(userAlgorithmName, userPrivateKeyInfo);
    userCreateCsr.SubjectDN.Add(new X509NameEntity(X509NameLabel.C, "CN"));
    userCreateCsr.SubjectDN.Add(new X509NameEntity(X509NameLabel.CN, "Test Subject Porject Name"));
    userCreateCsr.SubjectDN.Add(new X509NameEntity(X509NameLabel.EmailAddress, "abc999@test111222.com"));
    Asn1Encodable asn1 = new BasicConstraints(false);
    userCreateCsr.Extensions.Add(new X509ExtensionEntity(X509ExtensionLabel.BasicConstraints, true, asn1));
    byte[] csrPem = userCreateCsr.GenerateDer(); // Send to issuer

    //
    // Issuer work, Load certificate request and create certificate.
    //
    X509CertificateV3Generator v3generator = new X509CertificateV3Generator(_issuerSignatureAlgorithm, _issuerPrivateKeyInfo);
    v3generator.IssuerDN.CopyFromSubjectDN(_issuerCer);
    Asn1Encodable asn2 = new KeyUsage(KeyUsage.KeyCertSign | KeyUsage.DataEncipherment);
    v3generator.Extensions.Add(new X509ExtensionEntity(X509ExtensionLabel.KeyUsage, false, asn2));
    v3generator.SetCertificateRequest(csrPem);
    if (v3generator.CertificateRequest.Verify(userPublicKey))
    {
        v3generator.CertificateRequest.SubjectDN.Remove(X509NameLabel.EmailAddress);
    }
    byte[] userCer = v3generator.GenerateDer(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(99));
    //
    // User work, Verify.
    //
    File.WriteAllBytes("userCer.cer", userCer);
    var userCerBC = new Org.BouncyCastle.X509.X509Certificate(userCer);
    var userCerNET = new System.Security.Cryptography.X509Certificates.X509Certificate2(userCer); 
    try
    {
        userCerBC.Verify(_issuerCer.GetPublicKey());
        Console.WriteLine($"Verify user certificate by CA certificate - true");
    }
    catch (Exception)
    {
        Console.WriteLine($"Verify user certificate by CA certificate - false");
    }
    Console.WriteLine();
    Console.WriteLine(userCerBC);
    Console.WriteLine();
    Console.WriteLine(userCerNET);
    Console.ReadKey(true);
}

ECDH


private static void Demo()
{
    IKeyExchangeTerminalA keA = new ECDH().GetTerminalA();
    IKeyExchangeTerminalB keB = new ECDH().GetTerminalB();

    // Alice work
    keA.GenerateParameters(384);
    byte[] p = keA.P;
    byte[] g = keA.G;
    byte[] publicKeyA = keA.PublicKeyA;

    // Bob work
    keB.GenerateParameters(p, g, publicKeyA);
    byte[] pmsB = keB.DeriveKeyMaterial(true);
    byte[] publicKeyB = keB.PublicKeyB;

    // Alice work
    byte[] pmsA = keA.DeriveKeyMaterial(publicKeyB, true);

    //
    bool same = pmsA.SequenceEqual(pmsB);
    Console.WriteLine($"ECDH {same}");
    Console.WriteLine(BitConverter.ToString(pmsA).Replace("-", ""));
    Console.WriteLine(BitConverter.ToString(pmsB).Replace("-", ""));
}

LICENSE

This project based on MIT license.

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 net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 206 3/28/2023
2.0.0 181 3/27/2023