1

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?

jira
  • 3,890
  • 3
  • 22
  • 32

1 Answers1

0

You are using the Encoding.Convert function wrong. It should be

        var newbytes = Encoding.Convert(win1251, utf8, bytes);
        retVal += utf8.GetString(newbytes);

The function signature is Convert(srcEncoding, dstEncoding, bytes) and you had the encodings in opposite order.And then you called GetString on the win1251 instead the utf32. I also used utf8 instead of utf32 as I think it is a better option.

jira
  • 3,890
  • 3
  • 22
  • 32
  • After adding these corrections the output is the same as in the last block of my question, with whitespaces between characters. Is there any way to get rid of those? – Iwakura Lain May 13 '20 at 20:39
  • @IwakuraLain I'm not having this problem. I'm guessing it has something to do with your local setting. Can you try [this Fiddle](https://dotnetfiddle.net/SG60l9)? – jira May 14 '20 at 06:18
  • Yeah, this code works perfectly in browser, but in my Visual Studio it still displays extra spaces. What settings do you think might be at fault? – Iwakura Lain May 14 '20 at 12:02
  • What is your console encoding? Can you set it to unicode? – jira May 14 '20 at 12:08
  • It says it's code page is 866. I tried setting it to unicode but failed, I might have used wrong methods though. – Iwakura Lain May 14 '20 at 13:00
  • [This answer](https://stackoverflow.com/questions/57131654/using-utf-8-encoding-chcp-65001-in-command-prompt-windows-powershell-window) worked for me. – jira May 14 '20 at 13:28