Here’s an example of a class in C# that provides methods to encrypt and decrypt a given string into a combination of numbers and characters with dashes:
using System;
using System.Text;
public class StringEncryptor
{
public string EncryptString(string input)
{
StringBuilder encryptedBuilder = new StringBuilder();
foreach (char c in input)
{
int charValue = (int)c;
string encryptedPart = charValue.ToString() + "-";
encryptedBuilder.Append(encryptedPart);
}
string encryptedString = encryptedBuilder.ToString().TrimEnd('-');
return encryptedString;
}
public string DecryptString(string encryptedString)
{
string[] parts = encryptedString.Split('-');
StringBuilder decryptedBuilder = new StringBuilder();
foreach (string part in parts)
{
int charValue;
if (int.TryParse(part, out charValue))
{
char decryptedChar = (char)charValue;
decryptedBuilder.Append(decryptedChar);
}
}
string decryptedString = decryptedBuilder.ToString();
return decryptedString;
}
}
Here’s an example usage of the class:
StringEncryptor encryptor = new StringEncryptor();
string originalString = "Hello World!";
string encryptedString = encryptor.EncryptString(originalString);
Console.WriteLine("Encrypted: " + encryptedString);
string decryptedString = encryptor.DecryptString(encryptedString);
Console.WriteLine("Decrypted: " + decryptedString);
In this example, the EncryptString
method converts each character in the input string to its corresponding ASCII value (cast as an integer) and appends a dash to separate each encrypted part. The resulting encrypted string consists of a combination of numbers and dashes.
The DecryptString
method takes the encrypted string, splits it by dashes, and converts each part back to its corresponding ASCII value. It then builds the decrypted string by converting the ASCII values back to their original characters.
Please note that this encryption method is very basic and not intended for secure encryption purposes. It is simply a demonstration of converting strings to a different representation using ASCII values. For more robust encryption algorithms, consider using established encryption libraries or methods.
Here’s another example of a class in C# that can encrypt and decrypt a given string by converting it to a number combined with dashes for every fifth character:
public class StringEncryptor
{
public string Encrypt(string input)
{
string encryptedString = string.Empty;
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
int charValue = (int)c;
encryptedString += charValue.ToString();
// Add a dash every fifth character except for the last character
if ((i + 1) % 5 == 0 && i != input.Length - 1)
{
encryptedString += "-";
}
}
return encryptedString;
}
public string Decrypt(string input)
{
string decryptedString = string.Empty;
string[] parts = input.Split('-');
foreach (string part in parts)
{
int charValue = int.Parse(part);
char c = (char)charValue;
decryptedString += c;
}
return decryptedString;
}
}
Here’s an example usage of the class:
StringEncryptor encryptor = new StringEncryptor();
string originalString = "HelloWorld123";
string encryptedString = encryptor.Encrypt(originalString);
Console.WriteLine("Encrypted String: " + encryptedString);
string decryptedString = encryptor.Decrypt(encryptedString);
Console.WriteLine("Decrypted String: " + decryptedString);
In this example, the Encrypt
method takes an input string and converts each character into its ASCII value, which is then appended to the encryptedString
. A dash is added every fifth character (except for the last character) to separate the groups of numbers.
The Decrypt
method reverses the process by splitting the input string at each dash, parsing each part back into an integer, and converting it back to a character. The resulting characters are concatenated to form the decryptedString
.
Please note that this encryption method is very basic and should not be considered secure for sensitive data. It is provided as an illustrative example, and for stronger encryption requirements, more advanced encryption algorithms should be used.