簡體   English   中英

使用C#的替換密碼區分大小寫

[英]substitution cipher case sensitive using c#

我想使加密方法可以接受純文本變量(例如“ Hello World”)中的大寫和小寫字母,它僅接受小寫字母,請幫助我,我想使用僅小寫字母作為輸入,請使其工作正常,請幫助我

            using System;

        class SubstitutionCipher
        {
           static void Main()
           {
              string key = "jfkgotmyvhspcandxlrwebquiz";
              string plainText = "the quick brown fox jumps over the lazy dog";
              string cipherText = Encrypt(plainText, key);
              string decryptedText = Decrypt(cipherText, key);
              Console.WriteLine("Plain     : {0}", plainText);
              Console.WriteLine("Encrypted : {0}", cipherText);
              Console.WriteLine("Decrypted : {0}", plainText);
              Console.ReadKey();
           }
    //encryption method 
           static string Encrypt(string plainText, string key)
           {
              char[] chars = new char[plainText.Length];
              for(int i = 0; i < plainText.Length; i++)
              {
                 if (plainText[i] == ' ')
                 {
                    chars[i] = ' ';
                 }   
                 else
                 {
                    int j = plainText[i] - 97;
                    chars[i] = key[j];
                 }
              }
              return new string(chars);
           }
//decryption method
           static string Decrypt(string cipherText, string key)
           {
              char[] chars = new char[cipherText.Length];
              for(int i = 0; i < cipherText.Length; i++)
              {
                 if (cipherText[i] == ' ')
                 {
                    chars[i] = ' ';
                 }   
                 else
                 {
                    int j = key.IndexOf(cipherText[i]) - 97;
                    chars[i] = (char)j;
                 }
              }
              return new string(chars);
           } 
        }

您要減去“ a”,這僅適用於小寫字符。 您應該檢查它是大寫還是小寫,然后分別減去A或a。 另外,您可以直接使用'a'或'A'來獲得與字符等效的int

我的意思是將此用於您的else Decrypt和Encrypt語句:

    static string Encrypt(string plainText, string key)
    {
        char[] chars = new char[plainText.Length];
        for (int i = 0; i < plainText.Length; i++)
        {
            if (plainText[i] == ' ')
            {
                chars[i] = ' ';
            }
            else if (Char.IsUpper(plainText[i]))
            {
                int j = plainText[i] - 'A';
                chars[i] = key[j];
            }
            else
            {
                int j = plainText[i] - 'a';
                chars[i] = key[j];
            }
        }
        return new string(chars);

    static string Decrypt(string cipherText, string key)
    {
        char[] chars = new char[cipherText.Length];
        for (int i = 0; i < cipherText.Length; i++)
        {
            if (cipherText[i] == ' ')
            {
                chars[i] = ' ';
            }
            else if (Char.IsUpper(cipherText[i]))
            {
                int j = key.IndexOf(cipherText[i]) - 'A';
                chars[i] = (char)j;
            }
            else
            {
                int j = key.IndexOf(cipherText[i]) - 'a';
                chars[i] = (char)j;
            }
        }
        return new string(chars);
    }

或分別為加密和解密的簡短版本:

            else 
            {
                int j = Char.IsUpper(plainText[i]) ? plainText[i] - 'A' : plainText[i] - 'a';
                chars[i] = key[j];
            }

            else
            {
                int j = Char.IsUpper(cipherText[i]) ? key.IndexOf(cipherText[i]) - 'A' :  key.IndexOf(cipherText[i]) - 'a';
                chars[i] = (char)j;
            }

編輯:

好的,我認為該算法已經完成,並且我已經修復了可能導致異常的部分。 但是,您是:

a)不考慮哪個字母大寫,並且b)沒有有效的解密器。 (我認為這是可行的,但隨后您的主要方法將純文本結果作為解密的消息)。

要修復您的解密器,您需要在加密器上添加一種存儲大寫字母的方法。 您可以對此進行更復雜的介紹,但是現在,我們僅將其加密為密文中的大寫字母。

    static string Encrypt(string plainText, string key)
    {
        char[] chars = new char[plainText.Length];
        for (int i = 0; i < plainText.Length; i++)
        {
            if (plainText[i] == ' ')
            {
                chars[i] = ' ';
            }
            else 
            {
                int j = Char.IsUpper(plainText[i]) ? plainText[i] - 'A' : plainText[i] - 'a';
                chars[i] = Char.IsUpper(plainText[i]) ? Char.ToUpper(key[j]) : key[j];
            }
        }
        return new string(chars);

要解密,您應該在密鑰上找到它為小寫字符,但輸出為大寫字符。

    static string Decrypt(string cipherText, string key)
    {
        char[] chars = new char[cipherText.Length];
        for (int i = 0; i < cipherText.Length; i++)
        {
            if (cipherText[i] == ' ')
            {
                chars[i] = ' ';
            }
            else
            {
                int j = Char.IsUpper(cipherText[i]) ? key.IndexOf(Char.ToLower((char)cipherText[i])) + 'a' : key.IndexOf(cipherText[i]) + 'a';
                chars[i] = Char.IsUpper(cipherText[i]) ? Char.ToUpper((char)j) : (char)j;
            }
        }
        return new string(chars);
    }

這是一個有趣的問題,尤其是在一般情況下 所以我們有

  1. 關鍵字符,這些字符應被加密(例如aj
  2. 不在鍵中的字符,應跳過這些字符(例如space

為了提高效率,我們將Encryptor \\ Decryptor設計為Dictionary<Char, Char> ,為了做到這一點,我們應該對密鑰中的字符進行排序並使其對應。 例如

  key        = "jfkgo"
  sorted key = "fgjko"

對將是

  {j, f} // when encrypting "j" becomes "f", on decryption "f" turns into "j"
  {f, g}
  {k, j}
  {g, k}
  {o, o}

從左到右讀取時我們有一個加密器從右到左讀取時有一個解密器

private static Dictionary<Char, Char> Encryptor(String key) {
  Char[] data = key.ToArray();
  Char[] sorted = data.OrderBy(c => c).ToArray();

  return data
    .Zip(sorted, (plain, encrypted) => new {
         Plain = plain,
         Encrypted = encrypted
     })
    .ToDictionary(item => item.Plain, item => item.Encrypted);
}

// Note, that Decryptor is the same as Encryptor by for the last line
private static Dictionary<Char, Char> Decryptor(String key) {
  Char[] data = key.ToArray();
  Char[] sorted = data.OrderBy(c => c).ToArray();

  return data
    .Zip(sorted, (plain, encrypted) => new {
      Plain = plain,
      Encrypted = encrypted
    })
    .ToDictionary(item => item.Encrypted, item => item.Plain);
}

現在,很容易實現Encrypt和Decrypt方法

private static String Encrypt(String source, String key) {
  var dict = Encryptor(key);

  StringBuilder Sb = new StringBuilder(source.Length);

  Char sub;

  foreach (var ch in source)
    if (dict.TryGetValue(ch, out sub))
      Sb.Append(sub);
    else
      Sb.Append(ch);

  return Sb.ToString();
}

 // Note, that Decryptor is the same as Encryptor by for the first line
private static String Decrypt(String source, String key) {
  var dict = Decryptor(key);

  StringBuilder Sb = new StringBuilder(source.Length);

  Char sub;

  foreach (var ch in source)
    if (dict.TryGetValue(ch, out sub))
      Sb.Append(sub);
    else
      Sb.Append(ch);

  return Sb.ToString();
}

小測試

 // For test, let capital letters mmimic the small ones
 string key = "jfkgotmyvhspcandxlrwebquiz" + "jfkgotmyvhspcandxlrwebquiz".ToUpper();

 string plainText = "The Quick Brown Fox Jumps Over The Lazy Dog!";
 string cipherText = Encrypt(plainText, key);
 string decryptedText = Decrypt(cipherText, key);

 Console.WriteLine("Plain     : {0}", plainText);
 Console.WriteLine("Encrypted : {0}", cipherText);
 Console.WriteLine("Decrypted : {0}", plainText);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM