I'm writing a Caesar cipher program but there is a problem with encoding. Here's the code:
using System;
using System.Text;
public class CaesarCipher
{
private string Encode(string text, int k)
{
var retVal = "";
Encoding win1251 = Encoding.GetEncoding("windows-1251");
Encoding utf32 = Encoding.GetEncoding("utf-32");
byte[] text1251 = win1251.GetBytes(text);
for (int i = 0; i < text1251.Length; i++)
{
var c = text1251[i];
var index = (int)c;
Console.Write(index + "/");
var codeIndex = (index + k) % 256;
byte[] bytes = BitConverter.GetBytes(codeIndex);
byte[] newbytes = Encoding.Convert(utf32, win1251, bytes);
Console.Write(codeIndex + "\n");
retVal += win1251.GetString(newbytes);
}
return retVal;
}
private string Decode(string text, int k)
{
var retVal = "";
Encoding win1251 = Encoding.GetEncoding("windows-1251");
Encoding utf32 = Encoding.GetEncoding("utf-32");
byte[] text1251 = win1251.GetBytes(text);
for (int i = 0; i < text1251.Length; i++)
{
var c = text1251[i];
var index = (int)c;
Console.Write(index + "/");
var codeIndex = (index - k) % 256;
byte[] bytes = BitConverter.GetBytes(codeIndex);
byte[] newbytes = Encoding.Convert(utf32, win1251, bytes);
Console.Write(codeIndex + "\n");
retVal += win1251.GetString(newbytes);
}
return retVal;
}
public string Encrypt(string plainMessage, int key)
=> Encode(plainMessage, key);
public string Decrypt(string encryptedMessage, int key)
=> Decode(encryptedMessage, key);
}
class Program
{
static void Main(string[] args)
{
var cipher = new CaesarCipher();
Console.Write("Enter the text: ");
var message = Console.ReadLine();
Console.Write("Enter the key: ");
var secretKey = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter 1 (Encrypt) or 2 (Decrypt): ");
var message1 = Convert.ToInt32(Console.ReadLine());
if (message1 == 1)
{
var encryptedText = cipher.Encrypt(message, secretKey);
Console.WriteLine("Encrypted text: {0}", encryptedText);
}
if (message1 == 2)
{
var encryptedText = cipher.Decrypt(message, secretKey);
Console.WriteLine("Decrypted Text: {0}", encryptedText);
}
Console.ReadLine();
}
}
First of all, I need to have character count of 256, so I guessed that using Win-1251 encoding is necessary. I have a problem with converting to and form Win-1251 encoding. If I run this code, I get incorrect conversion with cyrillic characters:
Enter the text: абвгд
Enter the key: 3
Enter 1 (Encrypt) or 2 (Decrypt): 1
224/227
225/228
226/229
227/230
228/231
Encrypted text: aaa?c
If I remove the "byte[] newbytes = Encoding.Convert(utf32, win1251, bytes);" line and change "retVal += win1251.GetString(newbytes);" to "retVal += win1251.GetString(bytes);", then I get the following output:
Enter the text: абвгд
Enter the key: 3
Enter 1 (Encrypt) or 2 (Decrypt): 1
224/227
225/228
226/229
227/230
228/231
Encrypted text: г д е ж з
This output is correct, however it puts these whitespaces between characters, which shouldn't be there. Is there any way to have correct decryption without whitespaces?