我有一些代码可以解密文件中的加密文本,但我遇到的问题是空格和新行没有显示,而是用字母替换。我写了如果character =''继续只是为了清理它。
using System;
using System.IO;
namespace ceasarAssignment
{
public class caesarShift
{
public static void Main()
{
string file = @"text.txt", // Name of the file that is being read from
encrypted = File.ReadAllText(file), // Reading the file
decrypted = " ";
char character = '0';
int shift = 0;
encrypted = encrypted.ToUpper(); // Puts the string into uppercase
char[] alph = 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' };
// The array above is the alphabet (26 characters)
Console.WriteLine("Encrypted text: \n{0}", encrypted);
//Shows the encrypted text before it is decrypted
for (int i = 0; i < alph.Length; i++) //adds a counter so that this for loop will repeat until it has happened 26 times
{
decrypted = "";
foreach (char c in encrypted)
{
character = c;// each letter in the file is now defined as a 'character'
if (character == ' ')//if the character is a space it will just continue
continue;
shift = Array.IndexOf(alph, character) - i; //searchs the array for the character then minuses the counter to add the correct shift
if (shift <= 0)
shift = shift + 26;// if the character is at the beginning of the array go to the end
if (shift >= 26)
shift = shift - 26;// if the character is at the end of the array go to the beginning
decrypted += alph[shift];
}
Console.WriteLine("\n Shift {0} \n {1}", i + 1, decrypted); //Shows the decrypted code for each 26 shifts
}
}
}
}
答案 0 :(得分:1)
如果char.IsLetter(c))
为真,你应该只解密一个角色,否则你应该只通过未经修改的角色。
if (!char.IsLetter(c)) {
decrypted += c;
continue;
}
答案 1 :(得分:1)
你可以检查你正在看的角色是否在你的字母表中
var groupedQuery = from p in db.People
where p.City != null && p.State != null
group p by new {p.City, p.State}
into gp
select new Thing {
TotalNumber = gp.Count(),
City = gp.Key.City,
State = gp.Key.State
};
IQueryable<Thing> retQuery = groupedQuery.AsQueryable();
retQuery= retQuery.Take(10);
return retQuery;
答案 2 :(得分:0)
if (character == ' ')//if the character is a space it will just continue
continue;
应该在您的decrypted
字符串后添加一个空格,然后continue
。
试
if (character == ' ') {
decrypted += character;
continue;
}
答案 3 :(得分:0)
我认为这就是你想要的:
private static void Main(string[] args)
{
string file = @"text.txt",
encrypted = File.ReadAllText(file),
decrypted =string.Empty;
char character = '0';
int shift = 0;
encrypted = encrypted.ToUpper();
char[] alph = 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'
};
Console.WriteLine("Encrypted text: \n{0}", encrypted);
for (int i = 0; i < alph.Length; i++)
{
decrypted = "";
foreach (char c in encrypted)
{
character = c;
if (character == ' '||character==10)
{
decrypted += character;
continue;
}
shift = Array.IndexOf(alph, character) - i;
if (shift <= 0)
shift = shift + 26;
if (shift >= 26)
shift = shift - 26;
decrypted += alph[shift];
}
Console.WriteLine("\n Shift {0} \n {1}", i + 1, decrypted);
}
}