Vigenere Cipher C # with "-"

I am doing a Vigenere encryption program in C #, but I have a problem. I don’t have “Ñ” that I would like to encrypt, as it does in the Vigenere cipher, but with “Ñ”, how do I add the letter “Ñ” to this code? so that both keys and s remain so: a = 0 b = 1 ... n = 13 - = 14 ... z = 26 after n is moved

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
    static void VigenereEncrypt(ref StringBuilder s, string key)
    {
        for (int i = 0; i < s.Length; i++) s[i] = Char.ToUpper(s[i]);
        key = key.ToUpper();
        int j = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (Char.IsLetter(s[i]))
            {
                s[i] = (char)(s[i] + key[j] - 'A');
                if (s[i] > 'Z') s[i] = (char)(s[i] - 'Z' + 'A' - 1);
            }
            j = j + 1 == key.Length ? 0 : j + 1;
        }
    }

    static void VigenereDecrypt(ref StringBuilder s, string key)
    {
        for (int i = 0; i < s.Length; i++) s[i] = Char.ToUpper(s[i]);
        key = key.ToUpper();
        int j = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (Char.IsLetter(s[i]))
            {
                s[i] = s[i] >= key[j] ?
                          (char)(s[i] - key[j] + 'A') :
                          (char)('A' + ('Z' - key[j] + s[i] - 'A') + 1);
            }
            j = j + 1 == key.Length ? 0 : j + 1;
        }
    }

    public static void Main()
    {
        while (true)
        {
            StringBuilder s = new StringBuilder(Console.ReadLine());
            string key = Console.ReadLine();
            VigenereEncrypt(ref s, key);
            Console.WriteLine(s);
            VigenereDecrypt(ref s, key);
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }

 }
}
-3
source share
1 answer

Your code:

if (Char.IsLetter(s[i]))
{
        s[i] = (char)(s[i] + key[j] - 'A');
        if (s[i] > 'Z') s[i] = (char)(s[i] - 'Z' + 'A' - 1);
}

, U + 0041 U + 005A , , *. ( , , , Ñ , ). , UCS, .

. - .

string spanishAlphabet = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
string englishAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string irishAlphabet = "ABCDEFGHILMNOPRSTU";
string danishAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ";
string norwegianAlphabet = danishAlphabet;

, UCS, :

static void VigenereEncrypt(StringBuilder s, string key, string alphabet)
{
  for (int i = 0; i < s.Length; i++) s[i] = Char.ToUpper(s[i]);
  key = key.ToUpper();
  int j = 0;
  for (int i = 0; i < s.Length; i++)
  {
    if(alphabet.Contains(s[i]))
      s[i] = alphabet[(alphabet.IndexOf(s[i]) + alphabet.IndexOf(key[j])) % alphabet.Length];
    j = (j + 1) % key.Length;
  }
}

static void VigenereDecrypt(StringBuilder s, string key, string alphabet)
{
  for (int i = 0; i < s.Length; i++) s[i] = Char.ToUpper(s[i]);
  key = key.ToUpper();
  int j = 0;
  for (int i = 0; i < s.Length; i++)
  {
    if(alphabet.Contains(s[i]))
    {
      s[i] = alphabet[(alphabet.IndexOf(s[i]) - alphabet.IndexOf(key[j]) + alphabet.Length) % alphabet.Length];
      j = (j + 1) % key.Length;
    }
  }
}

( , , , , , , ).

ref, StringBuilder , , . , :

static string VigenereEncrypt(string s, string key, string alphabet)
{
  s = s.ToUpper();
  key = key.ToUpper();
  int j = 0;
  StringBuilder ret = new StringBuilder(s.Length);
  for (int i = 0; i < s.Length; i++)
  {
    if(alphabet.Contains(s[i]))
      ret.Append(alphabet[(alphabet.IndexOf(s[i]) + alphabet.IndexOf(key[j])) % alphabet.Length]);
    else
      ret.Append(s[i]);
    j = (j + 1) % key.Length;
  }
  return ret.ToString();
}

static string VigenereDecrypt(string s, string key, string alphabet)
{
  s = s.ToUpper();
  key = key.ToUpper();
  int j = 0;
  StringBuilder ret = new StringBuilder(s.Length);
  for (int i = 0; i < s.Length; i++)
  {
    if(alphabet.Contains(s[i]))
      ret.Append(alphabet[(alphabet.IndexOf(s[i]) - alphabet.IndexOf(key[j]) + alphabet.Length) % alphabet.Length]);
    else
      ret.Append(s[i]);
    j = (j + 1) % key.Length;
  }
  return ret.ToString();
}

, Unicode , . IJ † . - , ‡, , . , , , U + FFFE.

, (, Ñ ), . , , Vigenère, , , . - , :

public static IEnumerable<char> RemoveDiacriticsEnum(string src, string alphabet)
{
  foreach(char c in src.Normalize(NormalizationForm.FormD))
    if(alphabet.Contains(c))  // Catch e.g. Ñ in Spanish, considered letter in own right
      yield return c;
    else
      switch(CharUnicodeInfo.GetUnicodeCategory(c))
      {
        case UnicodeCategory.NonSpacingMark:
        case UnicodeCategory.SpacingCombiningMark:
        case UnicodeCategory.EnclosingMark:
          //do nothing
          break;
        default:
          yield return customFolding(c);
          break;
      }
}

, foreach(char c in RemoveDiacriticsEnum(s, alphabet)), c, s[i]. , . fooobar.com/questions/29054/... .

:

string spanishAlphabet = "AÁBCDEÉFGHIÍJKLMNÑOÓPQRSTUÚÜVWXYZ";

* , , , Ð, Ȝ Þ, , , A,B,C,D,[Ð],E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,[Ȝ],Z,[Þ], , , Ð, , , , D E .. ( ), ; A,B,C,D,E,F,G,H,I,L,M,N,O,P,R,S,T,U, V , J,K,Q,V,W,X,Y,Z - , A,B,C,D,E,F,G,H,I,[J],[K],L,M,N,O,P,[Q],R,S,T,U,[V],[W],[X],[Y],[Z], , , J I L, , J, . , Vigenère, , V , vótaí.

† UCS IJ U + 0132, . IJ, - IJ IJ, , IJ.

‡ , 19- .

+6

Source: https://habr.com/ru/post/1616590/


All Articles