所以我是一名教师,希望明年开办一个小型黑客/加密俱乐部。 所以我正在编写一些加密方法的解决方案。我编写了一个随机替换密码,但无法解决随机密码方法。
我知道我需要使用字母频率和字母对,不知道如何完美地编程。
我使用的句子是"这是对alans加密技能的测试"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the sentence to encrypt");
string plainText = Console.ReadLine();
string cipherText = encrypt(plainText.ToUpper());
Console.WriteLine(cipherText);
Console.ReadLine();
}
public static string encrypt(string phrase)
{
List<string> originalLetters = new List<string>();
//enter the original phrase to the list
for (int i = 0; i < phrase.Length; i++ )
{
originalLetters.Add(phrase[i].ToString());
}
//randomise the subsitutions
//all
List<string> allLetters = new List<string>{"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"};
//shuffled
List<string> shuffledLetters = new List<string>();
int times = allLetters.Count;
Random rng = new Random();
for (int i = 0; i < times;i++ )
{
int position = rng.Next(0, allLetters.Count);
shuffledLetters.Add(allLetters[position]);
Console.Write(allLetters[position] + ", ");
allLetters.RemoveAt(position);
}
Console.WriteLine();
string cipherText = "";
//now change letters into shuffled letters
string SortingLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < originalLetters.Count;i++)
{
//take the original letter
//find its position in the sorting letters
int index = SortingLetters.IndexOf(originalLetters[i]);
//use position to replace to the new string
if (index != -1)
{
//swap the letter with the index plus the key
cipherText += shuffledLetters[index];
}
else
{
cipherText += originalLetters[i];
}
}
//return the encrypted message
return cipherText;
}
public static string decrypt(string phrase)
{
//take the encrypted message
//find most frequent letter as E, focus on single letter words as either I or a
return "finalAnswer";
}
}
}
我还包含了示例输出。我想让我的学生坚持不懈地解决这个问题,但只是使用一个固定密钥基本上使它成为一个我已经有解决方案的凯撒密码。我对显示一系列答案的程序没有任何问题,用户必须从
中找到合适的答案感谢任何帮助
由于