Skip to content

Commit f3b8fb3

Browse files
committed
#163 Support UTF-8 and special character encryption
Added `Example_6_Wokring_With_Special_Character_Text` to demonstrate encryption and decryption of text with special characters (e.g., emojis) using UTF-8 encoding. Updated `Program.cs` to include the new example and re-enable previously commented-out AES and RSA examples. Modified `BytesToString` and `StringToBytes` in `ExtShared.cs` to use UTF-8 encoding instead of ASCII for better handling of non-ASCII characters. Updated `migration.md` with a TODO note about encrypting strings with special characters and suggested using byte arrays for clarity.
1 parent 3a9be6e commit f3b8fb3

4 files changed

Lines changed: 42 additions & 38 deletions

File tree

CryptoNet.Examples/ExampleRsa.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ public static void Example_5_Export_Public_Key_For_X509_Certificate()
9292
Debug.Assert(!string.IsNullOrEmpty(publicKey));
9393
}
9494

95+
public static void Example_6_Wokring_With_Special_Character_Text()
96+
{
97+
string ConfidentialDummyDataWithSpecialText = "Top secret 😃😃"; //
98+
99+
ICryptoNetRsa cryptoNet = new CryptoNetRsa();
100+
var privateKey = cryptoNet.GetKey(true);
101+
var publicKey = cryptoNet.GetKey(false);
102+
103+
ICryptoNet encryptClient = new CryptoNetRsa(publicKey);
104+
var encrypted = encryptClient.EncryptFromBytes(Encoding.UTF8.GetBytes(ConfidentialDummyDataWithSpecialText));
105+
106+
ICryptoNet decryptClient = new CryptoNetRsa(privateKey);
107+
var decrypted = decryptClient.DecryptToBytes(encrypted);
108+
var decryptedString = Encoding.UTF8.GetString(decrypted);
109+
110+
Debug.Assert(ConfidentialDummyDataWithSpecialText == decryptedString);
111+
}
112+
95113
/// <summary>
96114
/// CryptoNet interact with .net 5/6 for customization, like import/export PEM
97115
/// Work in Progress, not finished

CryptoNet.Examples/Program.cs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
1-
//using CryptoNet;
2-
//using System.Diagnostics;
3-
4-
//const string ConfidentialDummyData = "Top secret 🤣🤣"; //
5-
6-
//ICryptoNetAes cryptoNet = new CryptoNetAes();
7-
//var key = cryptoNet.GetKey();
8-
9-
//ICryptoNet encryptClient = new CryptoNetAes(key);
10-
//var encrypted = encryptClient.EncryptFromBytes(ConfidentialDummyData);
11-
12-
//ICryptoNet decryptClient = new CryptoNetAes(key);
13-
//var decrypted = decryptClient.DecryptToString(encrypted);
14-
15-
//Debug.Assert(ConfidentialDummyData == decrypted);
16-
17-
using CryptoNet.Examples;
1+
using CryptoNet.Examples;
182

193
ExampleDsa.Example_1_Sign_Validate_Content_With_SelfGenerated_AsymmetricKey();
204
ExampleDsa.Example_2_SelfGenerated_And_Save_AsymmetricKey();
215

22-
//ExampleAes.Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_SymmetricKey();
23-
//ExampleAes.Example_2_SelfGenerated_And_Save_SymmetricKey();
24-
//ExampleAes.Example_3_Encrypt_Decrypt_Content_With_Own_SymmetricKey();
25-
//ExampleAes.Example_4_Encrypt_Decrypt_Content_With_Human_Readable_Key_Secret_SymmetricKey();
26-
//ExampleAes.Example_5_Encrypt_And_Decrypt_File_With_SymmetricKey_Test("TestFiles\\test.docx");
27-
//ExampleAes.Example_5_Encrypt_And_Decrypt_File_With_SymmetricKey_Test("TestFiles\\test.xlsx");
28-
//ExampleAes.Example_5_Encrypt_And_Decrypt_File_With_SymmetricKey_Test("TestFiles\\test.pdf");
29-
//ExampleAes.Example_5_Encrypt_And_Decrypt_File_With_SymmetricKey_Test("TestFiles\\test.png");
30-
31-
//ExampleRsa.Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_AsymmetricKey();
32-
//ExampleRsa.Example_2_SelfGenerated_And_Save_AsymmetricKey();
33-
//ExampleRsa.Example_3_Encrypt_With_PublicKey_Decrypt_With_PrivateKey_Of_Content();
34-
//ExampleRsa.Example_4_Using_X509_Certificate();
35-
//ExampleRsa.Example_5_Export_Public_Key_For_X509_Certificate();
36-
//ExampleRsa.Example_7_Customize();
6+
ExampleAes.Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_SymmetricKey();
7+
ExampleAes.Example_2_SelfGenerated_And_Save_SymmetricKey();
8+
ExampleAes.Example_3_Encrypt_Decrypt_Content_With_Own_SymmetricKey();
9+
ExampleAes.Example_4_Encrypt_Decrypt_Content_With_Human_Readable_Key_Secret_SymmetricKey();
10+
ExampleAes.Example_5_Encrypt_And_Decrypt_File_With_SymmetricKey_Test("TestFiles\\test.docx");
11+
ExampleAes.Example_5_Encrypt_And_Decrypt_File_With_SymmetricKey_Test("TestFiles\\test.xlsx");
12+
ExampleAes.Example_5_Encrypt_And_Decrypt_File_With_SymmetricKey_Test("TestFiles\\test.pdf");
13+
ExampleAes.Example_5_Encrypt_And_Decrypt_File_With_SymmetricKey_Test("TestFiles\\test.png");
14+
15+
ExampleRsa.Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_AsymmetricKey();
16+
ExampleRsa.Example_2_SelfGenerated_And_Save_AsymmetricKey();
17+
ExampleRsa.Example_3_Encrypt_With_PublicKey_Decrypt_With_PrivateKey_Of_Content();
18+
ExampleRsa.Example_4_Using_X509_Certificate();
19+
ExampleRsa.Example_5_Export_Public_Key_For_X509_Certificate();
20+
ExampleRsa.Example_6_Wokring_With_Special_Character_Text();
21+
ExampleRsa.Example_7_Customize();
3722

3823

CryptoNet.ExtShared/ExtShared.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,23 @@ public static RSAParameters GetParameters(X509Certificate2? certificate, KeyType
106106
}
107107

108108
/// <summary>
109-
/// Converts a byte array to an ASCII-encoded string.
109+
/// Converts a byte array to an UTF8-encoded string.
110110
/// </summary>
111111
/// <param name="bytes">The byte array to convert.</param>
112-
/// <returns>An ASCII-encoded string representation of the byte array.</returns>
112+
/// <returns>An UTF8-encoded string representation of the byte array.</returns>
113113
public static string BytesToString(byte[] bytes)
114114
{
115-
return Encoding.ASCII.GetString(bytes);
115+
return Encoding.UTF8.GetString(bytes);
116116
}
117117

118118
/// <summary>
119-
/// Converts an ASCII-encoded string to a byte array.
119+
/// Converts an UTF8-encoded string to a byte array.
120120
/// </summary>
121121
/// <param name="content">The string to convert.</param>
122-
/// <returns>A byte array representing the ASCII-encoded string.</returns>
122+
/// <returns>A byte array representing the UTF8-encoded string.</returns>
123123
public static byte[] StringToBytes(string content)
124124
{
125-
return Encoding.ASCII.GetBytes(content);
125+
return Encoding.UTF8.GetBytes(content);
126126
}
127127

128128
/// <summary>

docs/migration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This document describes the main breaking and behavioral changes for the **AES** and **RSA** examples when migrating from **v2.4.0** to **v3.4.3** of CryptoNet.
44

55
---
6+
TODO: Issue encrypting 🤣🤣 in case of string, use bytes. an example to clarify this.
67

78
## 1. Common Changes
89

0 commit comments

Comments
 (0)