diff --git a/CryptoNet.Cli/ExampleRsa.cs b/CryptoNet.Cli/ExampleRsa.cs index ac25eec..9b2fcb5 100644 --- a/CryptoNet.Cli/ExampleRsa.cs +++ b/CryptoNet.Cli/ExampleRsa.cs @@ -6,7 +6,9 @@ // part of CryptoNet project using System.Diagnostics; +using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; +using System.Text; using CryptoNet.Models; using CryptoNet.Utils; @@ -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() @@ -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; + } + } diff --git a/CryptoNet/CryptoNet.csproj b/CryptoNet/CryptoNet.csproj index 66cea0d..e739e54 100644 --- a/CryptoNet/CryptoNet.csproj +++ b/CryptoNet/CryptoNet.csproj @@ -15,7 +15,7 @@ NextBix Maytham Fahmi 1.0.0 - 1.5.0 + 2.0.0 CryptoNet CryptoNet CryptoNet @@ -39,7 +39,9 @@ - + + Never + @@ -47,7 +49,9 @@ - + + Never + diff --git a/CryptoNet/CryptoNetAes.cs b/CryptoNet/CryptoNetAes.cs index 415292f..8ff4ad7 100644 --- a/CryptoNet/CryptoNetAes.cs +++ b/CryptoNet/CryptoNetAes.cs @@ -5,6 +5,8 @@ // 17-12-2021 12:18:44 // part of CryptoNet project +using System; +using System.IO; using System.Security.Cryptography; using CryptoNet.Models; using CryptoNet.Utils; diff --git a/CryptoNet/CryptoNetLogo-icon.ico b/CryptoNet/CryptoNetLogo-icon.ico new file mode 100644 index 0000000..5c8fde4 Binary files /dev/null and b/CryptoNet/CryptoNetLogo-icon.ico differ diff --git a/CryptoNet/CryptoNetRsa.cs b/CryptoNet/CryptoNetRsa.cs index d7ae2ad..45fd88b 100644 --- a/CryptoNet/CryptoNetRsa.cs +++ b/CryptoNet/CryptoNetRsa.cs @@ -5,6 +5,8 @@ // 17-12-2021 12:18:44 // part of CryptoNet project +using System; +using System.IO; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using CryptoNet.Models; diff --git a/CryptoNet/ICryptoNet.cs b/CryptoNet/ICryptoNet.cs index 0153445..4e72d5f 100644 --- a/CryptoNet/ICryptoNet.cs +++ b/CryptoNet/ICryptoNet.cs @@ -5,6 +5,7 @@ // 17-12-2021 12:18:44 // part of CryptoNet project +using System.IO; using CryptoNet.Models; namespace CryptoNet diff --git a/CryptoNet/Models/CryptoNetInfo.cs b/CryptoNet/Models/CryptoNetInfo.cs index d756398..c683852 100644 --- a/CryptoNet/Models/CryptoNetInfo.cs +++ b/CryptoNet/Models/CryptoNetInfo.cs @@ -5,6 +5,7 @@ // 17-12-2021 12:18:44 // part of CryptoNet project +using System; using System.ComponentModel; using System.Security.Cryptography; diff --git a/CryptoNet/Utils/CryptoNetUtils.cs b/CryptoNet/Utils/CryptoNetUtils.cs index cac1102..bc7f9e8 100644 --- a/CryptoNet/Utils/CryptoNetUtils.cs +++ b/CryptoNet/Utils/CryptoNetUtils.cs @@ -5,7 +5,9 @@ // 17-12-2021 12:18:44 // part of CryptoNet project +using System; using System.ComponentModel; +using System.IO; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -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); diff --git a/README.md b/README.md index 1aafce8..bd742be 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ https://maythamfahmi.github.io/CryptoNet [![Nuget](https://img.shields.io/nuget/v/cryptonet?style=social)](https://www.nuget.org/packages/CryptoNet/) is latest version and are maintained. -#### [![Nuget](https://img.shields.io/badge/nuget-v2.0.0-blue?style=social)](https://www.nuget.org/packages/CryptoNet/2.0.0) [![Release%20Code](https://img.shields.io/badge/release%20code-v2.0.0-blue?style=social)](https://github.com/maythamfahmi/CryptoNet/releases/tag/v2.0.0) +#### [![Nuget](https://img.shields.io/badge/nuget-v2.1.0-blue?style=social)](https://www.nuget.org/packages/CryptoNet/2.1.0) [![Release%20Code](https://img.shields.io/badge/release%20code-v2.1.0-blue?style=social)](https://github.com/maythamfahmi/CryptoNet/releases/tag/v2.1.0) - !!!Breaking change!!! - Refactoring RSA asymmetric encryption. - Introducing AES symmetric encryption.