提问人:PKYNI 提问时间:6/18/2018 更新时间:6/18/2018 访问量:1638
c# 替换密码解密
c# Substitution Cipher decryption
问:
所以我是一名老师,希望明年能经营一个小型黑客/密码学俱乐部。 因此,我正在为一些加密方法编写解决方案。我已经编写了一个随机替换密码,但无法计算出随机的解密方法。
我知道我需要使用字母频率和字母对,但不确定如何完美地编程。
我使用的句子是“这是对 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";
}
}
}
我还包含了示例输出。我想让我的学生很难破解它,但只要使用一个固定的密钥,基本上就可以把它变成一个凯撒密码,我已经有了解决方案。我对程序显示一系列答案没有问题,用户必须从中找到正确的答案
任何帮助都是值得赞赏的
谢谢
答: 暂无答案
评论
Dictionary<char, int>
string
shuffledLetters
需要解密,这样你就可以呈现各种候选字母或部分字母,并让学生根据缺失的字符迭代可能的排列。