C#凯撒密码无法破译
C# Caesar Cipher can't decipher
我正在尝试为作业制作 C# 凯撒密码。我已经尝试这样做了很长一段时间,但没有取得任何进展。
我现在遇到的问题是,与其使用我的 encrypted_text 并破译它,它只是 cycles through that alphabet, ignoring the character that it started on.应该发生的是它是为了采用 encrypted_text并循环遍历字母表,将每个字母改变一定的数字。
这是我目前拥有的:
using System;
using System.IO;
class cipher
{
public static void Main(string[] args)
{
string encrypted_text = "exxego";
string decoded_text = "";
char character;
int shift = 0;
bool userright = false;
char[] alphabet = new char[26] { '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' };
do
{
Console.WriteLine("How many times would you like to shift? (Between 0 and 26)");
shift = Convert.ToInt32(Console.ReadLine());
if (shift > 26)
{
Console.WriteLine("Over the limit");
userright = false;
}
if (shift < 0)
{
Console.WriteLine("Under the limit");
userright = false;
}
if (shift <= 26 && shift >= 0)
{
userright = true;
}
} while (userright == false);
for (int i = 0; i < alphabet.Length; i++)
{
decoded_text = "";
foreach (char c in encrypted_text)
{
character = c;
if (character == '\'' || character == ' ')
continue;
shift = Array.IndexOf(alphabet, character) - i;
if (shift <= 0)
shift = shift + 26;
if (shift >= 26)
shift = shift - 26;
decoded_text += alphabet[shift];
}
Console.WriteLine("\nShift #{0} \n{1}", i + 1, decoded_text);
}
StreamWriter file = new StreamWriter("decryptedtext.txt");
file.WriteLine(decoded_text);
file.Close();
}
}
正如你从我的照片中看到的那样,我越来越近了。我只需要能够破解这个。任何帮助将不胜感激。比较简单还请见谅question/solution,我是新手
您的字母表包含大写字符,但您输入的全是小写字母。您需要通过将所有输入转换为相同大小写或处理两个 upper/lower 个大小写字母来处理这种情况。
我正在尝试为作业制作 C# 凯撒密码。我已经尝试这样做了很长一段时间,但没有取得任何进展。
我现在遇到的问题是,与其使用我的 encrypted_text 并破译它,它只是 cycles through that alphabet, ignoring the character that it started on.应该发生的是它是为了采用 encrypted_text并循环遍历字母表,将每个字母改变一定的数字。
这是我目前拥有的:
using System;
using System.IO;
class cipher
{
public static void Main(string[] args)
{
string encrypted_text = "exxego";
string decoded_text = "";
char character;
int shift = 0;
bool userright = false;
char[] alphabet = new char[26] { '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' };
do
{
Console.WriteLine("How many times would you like to shift? (Between 0 and 26)");
shift = Convert.ToInt32(Console.ReadLine());
if (shift > 26)
{
Console.WriteLine("Over the limit");
userright = false;
}
if (shift < 0)
{
Console.WriteLine("Under the limit");
userright = false;
}
if (shift <= 26 && shift >= 0)
{
userright = true;
}
} while (userright == false);
for (int i = 0; i < alphabet.Length; i++)
{
decoded_text = "";
foreach (char c in encrypted_text)
{
character = c;
if (character == '\'' || character == ' ')
continue;
shift = Array.IndexOf(alphabet, character) - i;
if (shift <= 0)
shift = shift + 26;
if (shift >= 26)
shift = shift - 26;
decoded_text += alphabet[shift];
}
Console.WriteLine("\nShift #{0} \n{1}", i + 1, decoded_text);
}
StreamWriter file = new StreamWriter("decryptedtext.txt");
file.WriteLine(decoded_text);
file.Close();
}
}
正如你从我的照片中看到的那样,我越来越近了。我只需要能够破解这个。任何帮助将不胜感激。比较简单还请见谅question/solution,我是新手
您的字母表包含大写字符,但您输入的全是小写字母。您需要通过将所有输入转换为相同大小写或处理两个 upper/lower 个大小写字母来处理这种情况。