使用Caesar Cipher我管理解密包含加密文本的文本文件。但现在我不得不添加一个将再次加密文本的功能 [英] Using Caesar Cipher I Have Managed To Decrypt A Text File Containing An Encrypted Text. But Now Im Stuck On Adding A Function Which Would Encrypt The Text Again

查看:63
本文介绍了使用Caesar Cipher我管理解密包含加密文本的文本文件。但现在我不得不添加一个将再次加密文本的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string hidden_text = System.IO.File.ReadAllText(@"C:\Users\\Desktop\text\mario.txt");  
			
            string decrypted_text = " ";
			//This declares the number of shifts i will be making
            int shift = 0;

			
            char letter = '0';
            hidden_text = hidden_text.ToUpper();//The hidden_text is all in capital and this reads the hidden text even when its in capital 
             Console.WriteLine("The text i want to decrypt is \n{0}", hidden_text);// this displays on the console the text i will be decrypting
            
			
			//my alphabet of array which would be used in the for loop
			char[] Thealphabet = 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' };







for (int i = 0; i < Thealphabet.Length; i++)        //A for loop would loop around the text in order to find the right shift
{
    decrypted_text = "";
    foreach (char e in hidden_text)
    {
        letter = e;

        if (letter == '\'' || letter == ' ') //this is a true or false using the bool statement
            continue;

        shift = Array.IndexOf(Thealphabet, letter) - i;   //The process of shifting starts here using the array.
        if (shift <= 0)
            shift = shift + 26;

        if (shift >= 26)         // the number 26 is used to keep the answer in bound of the array
            shift = shift - 26;//This takes it back into the array


        decrypted_text += Thealphabet[shift];
    }
    Console.WriteLine("\nThis shows how the shifted text looks like {0}: \n {1}", i + 1, decrypted_text); //This displays in the output the number of shifts made

推荐答案

对您的代码的观察:



1.您的解密代码正在迭代隐藏的源代码26次;每次你根据一个转移#0~25个位置建立一个字符串到你的Char数组中。



a。如果您转移#0位置,您只需取回您解析的字符串



b。你没有做任何事......你在这里显示...每次通过外部for循环创建的字符串,所以,实际上,你已经扔掉了结果。



c。如果你根据< = 0进行测试,你将使用#0 +#26循环并超出数组的范围并得到一个错误。



2.使用 index%26 获取#0~25范围内的有效索引。



加密:如果可以算解密后,这应该很容易。
Observations on your code:

1. your decrypt code is iterating over the hidden source 26 times; each time you build up a string based on a "shift" #0~25 places into your array of Char.

a. if you shift #0 places you simply get back the string you parse

b. you don't do anything ... that you show here ... with the string you have created each time through the outer for loop, so, in effect, you have thrown away the results.

c. if you test based on <= 0, you will loop using #0 + #26 and exceed the bounds of your array and get an error.

2. use index % 26 to get valid indexes in the range #0~25.

Encryption: if you can figure out decryption, then this should be easy.


这篇关于使用Caesar Cipher我管理解密包含加密文本的文本文件。但现在我不得不添加一个将再次加密文本的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆