Skip to content

Commit

Permalink
#23 Major Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
maythamfahmi committed May 21, 2022
1 parent 35b98b0 commit 9c83d49
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 58 deletions.
68 changes: 57 additions & 11 deletions CryptoNet.Cli/ExampleRsa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
// <summary>part of CryptoNet project</summary>

using System.Diagnostics;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using CryptoNet.Models;
using CryptoNet.Utils;

Expand All @@ -28,7 +30,7 @@ public static void Test()
Example_3_Encrypt_With_PublicKey_Decrypt_With_PrivateKey_Of_Content();
Example_4_Using_X509_Certificate();
Example_5_Export_Public_Key_For_X509_Certificate();
Example_7_Customize();
//Example_7_Customize();
}

public static void Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_AsymmetricKey()
Expand Down Expand Up @@ -110,26 +112,70 @@ public static void Example_7_Customize()
{
X509Certificate2? cert = CryptoNetUtils.GetCertificateFromStore("CN=Maytham");

var pubKeyPem = CryptoNetUtils.ExportPemKey(cert!, false);
var priKeyPem = CryptoNetUtils.ExportPemKey(cert!);
var pubKeyPem = ExportPemKey(cert!, false);
var priKeyPem = ExportPemKey(cert!);

var password = "password";
var encryptedPriKeyBytes = CryptoNetUtils.ExportPemKeyWithPassword(cert!, password);
ICryptoNet cryptoNet1 = CryptoNetUtils.ImportPemKeyWithPassword(encryptedPriKeyBytes, password);
var encryptedPriKeyBytes = ExportPemKeyWithPassword(cert!, password);

ICryptoNet cryptoNet1 = ImportPemKeyWithPassword(encryptedPriKeyBytes, password);
var encrypt1 = cryptoNet1.EncryptFromString(ConfidentialDummyData);

ICryptoNet cryptoNet2 = CryptoNetUtils.ImportPemKey(pubKeyPem);
ICryptoNet cryptoNet2 = ImportPemKey(pubKeyPem);
var encrypt2 = cryptoNet2.EncryptFromString(ConfidentialDummyData);

ICryptoNet cryptoNet3 = CryptoNetUtils.ImportPemKey(priKeyPem);
ICryptoNet cryptoNet3 = ImportPemKey(priKeyPem);
var decrypt2 = cryptoNet3.DecryptToString(encrypt2);

Debug.Assert(ConfidentialDummyData == decrypt2);

var decrypt1 = cryptoNet3.DecryptToString(encrypt1);

Debug.Assert(ConfidentialDummyData == decrypt1);
}

public static char[] ExportPemCertificate(X509Certificate2 cert)
{
byte[] certBytes = cert!.RawData;
char[] certPem = PemEncoding.Write("CERTIFICATE", certBytes);
return certPem;
}

public static char[] ExportPemKey(X509Certificate2 cert, bool privateKey = true)
{
AsymmetricAlgorithm rsa = cert.GetRSAPrivateKey()!;

if (privateKey)
{
byte[] priKeyBytes = rsa.ExportPkcs8PrivateKey();
return PemEncoding.Write("PRIVATE KEY", priKeyBytes);
}

byte[] pubKeyBytes = rsa.ExportSubjectPublicKeyInfo();
return PemEncoding.Write("PUBLIC KEY", pubKeyBytes);
}

public static ICryptoNet ImportPemKey(char[] key)
{
ICryptoNet cryptoNet = new CryptoNetRsa();
cryptoNet.Info.RsaDetail!.Rsa?.ImportFromPem(key);
return cryptoNet;
}

public static byte[] ExportPemKeyWithPassword(X509Certificate2 cert, string password)
{
AsymmetricAlgorithm rsa = cert.GetRSAPrivateKey()!;
byte[] pass = Encoding.UTF8.GetBytes(password);
byte[] encryptedPrivateKey = rsa.ExportEncryptedPkcs8PrivateKey(pass,
new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, iterationCount: 100_000));
return encryptedPrivateKey;
}

public static ICryptoNet ImportPemKeyWithPassword(byte[] encryptedPrivateKey, string password)
{
ICryptoNet cryptoNet = new CryptoNetRsa();
cryptoNet.Info.RsaDetail?.Rsa?.ImportEncryptedPkcs8PrivateKey(password, encryptedPrivateKey, out _);
return cryptoNet;
}

}
10 changes: 7 additions & 3 deletions CryptoNet/CryptoNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Company>NextBix</Company>
<Authors>Maytham Fahmi</Authors>
<MinClientVersion>1.0.0</MinClientVersion>
<Version>1.5.0</Version>
<Version>2.0.0</Version>
<Title>CryptoNet</Title>
<Product>CryptoNet</Product>
<RootNamespace>CryptoNet</RootNamespace>
Expand All @@ -39,15 +39,19 @@
</Target>

<ItemGroup>
<Content Include="CryptoNetLogo-icon.ico" />
<Content Include="CryptoNetLogo-icon.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\img\CryptoNetLogo-icon.png" Pack="true" PackagePath="\" />
<None Include="..\img\CryptoNetLogo-icon.png" Pack="true" PackagePath="\">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions CryptoNet/CryptoNetAes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// <date>17-12-2021 12:18:44</date>
// <summary>part of CryptoNet project</summary>

using System;
using System.IO;
using System.Security.Cryptography;
using CryptoNet.Models;
using CryptoNet.Utils;
Expand Down
Binary file added CryptoNet/CryptoNetLogo-icon.ico
Binary file not shown.
2 changes: 2 additions & 0 deletions CryptoNet/CryptoNetRsa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// <date>17-12-2021 12:18:44</date>
// <summary>part of CryptoNet project</summary>

using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using CryptoNet.Models;
Expand Down
1 change: 1 addition & 0 deletions CryptoNet/ICryptoNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// <date>17-12-2021 12:18:44</date>
// <summary>part of CryptoNet project</summary>

using System.IO;
using CryptoNet.Models;

namespace CryptoNet
Expand Down
1 change: 1 addition & 0 deletions CryptoNet/Models/CryptoNetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// <date>17-12-2021 12:18:44</date>
// <summary>part of CryptoNet project</summary>

using System;
using System.ComponentModel;
using System.Security.Cryptography;

Expand Down
46 changes: 2 additions & 44 deletions CryptoNet/Utils/CryptoNetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// <date>17-12-2021 12:18:44</date>
// <summary>part of CryptoNet project</summary>

using System;
using System.ComponentModel;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
Expand Down Expand Up @@ -63,50 +65,6 @@ public static RSAParameters GetParameters(X509Certificate2? certificate, KeyType
}
}

public static char[] ExportPemCertificate(X509Certificate2 cert)
{
byte[] certBytes = cert!.RawData;
char[] certPem = PemEncoding.Write("CERTIFICATE", certBytes);
return certPem;
}

public static char[] ExportPemKey(X509Certificate2 cert, bool privateKey = true)
{
AsymmetricAlgorithm rsa = cert.GetRSAPrivateKey()!;

if (privateKey)
{
byte[] priKeyBytes = rsa.ExportPkcs8PrivateKey();
return PemEncoding.Write("PRIVATE KEY", priKeyBytes);
}

byte[] pubKeyBytes = rsa.ExportSubjectPublicKeyInfo();
return PemEncoding.Write("PUBLIC KEY", pubKeyBytes);
}

public static ICryptoNet ImportPemKey(char[] key)
{
ICryptoNet cryptoNet = new CryptoNetRsa();
cryptoNet.Info.RsaDetail!.Rsa?.ImportFromPem(key);
return cryptoNet;
}

public static byte[] ExportPemKeyWithPassword(X509Certificate2 cert, string password)
{
AsymmetricAlgorithm rsa = cert.GetRSAPrivateKey()!;
byte[] pass = Encoding.UTF8.GetBytes(password);
byte[] encryptedPrivateKey = rsa.ExportEncryptedPkcs8PrivateKey(pass,
new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, iterationCount: 100_000));
return encryptedPrivateKey;
}

public static ICryptoNet ImportPemKeyWithPassword(byte[] encryptedPrivateKey, string password)
{
ICryptoNet cryptoNet = new CryptoNetRsa();
cryptoNet.Info.RsaDetail?.Rsa?.ImportEncryptedPkcs8PrivateKey(password, encryptedPrivateKey, out _);
return cryptoNet;
}

public static string BytesToString(byte[] bytes)
{
return Encoding.ASCII.GetString(bytes);
Expand Down

0 comments on commit 9c83d49

Please sign in to comment.