我正在创建一个基本的凯撒密码加密/解密。我有一个随机的问题" u"出现而不是空间..任何不确定如何纠正这一点,任何建议将非常感激。
// Decryption Method //
static void decryption() {
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("\n*********************************** Decryption *********************************");
Console.ResetColor();
//pulls getPath from varables class
string path = globalVars.getPath();
string fileContent = "";
string encrypted_text = System.IO.File.ReadAllText(path); //String variable that contains the text from a file. To get the text, the method in a class SystemIO is ran to read the text. It expects a parameter, which is a file directory.
string decoded_text = " ";
int shift = 0;
char character = '0';
encrypted_text = encrypted_text.ToUpper();
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'
};
Console.WriteLine("The encrypted text is \n{0}", encrypted_text); //Display the encrypted text
for (int i = 0; i < alphabet.Length; i++) //Start a loop which will display 25 different candidates of decipher
{
decoded_text = "";
foreach(char c in encrypted_text) {
character = c;
if (character == '\'' || character == ' ') continue;
shift = Array.IndexOf(alphabet, character) - i; //Define a shift which is the index of a character in an alphabet array, take away the itteration of this loop. Store the result in a variable
if (shift <= 0) shift = shift + 26;
if (shift >= 26) shift = shift - 26;
decoded_text += alphabet[shift];
}
Console.WriteLine("\n\nShift {0} \n\n {1}", i + 1, decoded_text);
fileContent += "Shift " + (i + 1).ToString() + "\r\n" + decoded_text + "\r\n";
}
// Save Decrypted Output to .TXT file - allows user to choose save path within filesystem.
string filename;
string savePath;
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("\nWhat do you want to name your file??");
Console.ResetColor();
filename = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Where would you like to save your file??");
Console.ResetColor();
savePath = Console.ReadLine();
File.WriteAllText(savePath + filename + ".txt", fileContent);
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("File Saved");
Console.WriteLine(Console.Read());
}
请参阅下面的输入和输出文本图像
答案 0 :(得分:2)
您的错误源于Array.IndexOf
返回-1
(即相当于z)的事实,如果数组中没有该字符,那么转移-1 - i
。特别是,每20个字符的新行字符会导致问题。
我建议您检查一下
if (character == '\'' || character == ' ')
continue;
替换为支票
if (!alphabet.Contains(character))
{
decoded_text += character;
continue;
}
哪个更强大,尤其适用于\n
或\r
后跟\n
(\r\n
等字符的字符,这是Windows上的换行符)。将字符添加到输出中是可选的,但可能有帮助。
答案 1 :(得分:1)
您的问题出在新行字符中,这一行:
if (character == '\'' || character == ' ') continue;
不检查。
在这种情况下,Array.IndexOf
将返回-1。由于您的文字在i == 5
(即“Shift 6”)时被正确解密,因此对于未知字符,这将为您提供alphabet[20]
,即U
。输入文件中的行长度为20个字符,实际上,输出中的20个字符后出现第一个错误U
:THEINCREDIBLELEGACYO U FTHE ...
您应该检查Array.IndexOf(...) >= 0
。
答案 2 :(得分:0)
我没有直接回答你的问题,只要说它可能与你移位字节的方式有关。但是,我想分享一段时间以前我在互联网上找到的字节密码,这正是你要做的事情,但只需很少的代码行:Original Source Here
尽管存在有偏见的downvotes,但代码片段实际上非常好,并且适用于简单的混淆策略。这是代码,以防链接死亡:
public static class SimpleByteCipher
{
public static byte[] EncryptStringToByteArray( string data , string password , uint seed)
{
byte[] bytes = Encoding.ASCII.GetBytes( data );
byte[] passwordBytes = Encoding.ASCII.GetBytes( password );
int passwordShiftIndex = 0;
for( int i = 0; i < bytes.Length; i++ )
{
bytes[ i ] = ( byte )( bytes[ i ] + passwordBytes[ passwordShiftIndex ] + seed );
passwordShiftIndex = ( passwordShiftIndex + 1 ) % passwordBytes.Length;
}
return bytes;
}
public static string DecryptByteArrayToString( byte[] data , string password , uint seed)
{
byte[] bytes = data;
byte[] passwordBytes = Encoding.ASCII.GetBytes( password );
int passwordShiftIndex = 0;
for( int i = 0; i < bytes.Length; i++ )
{
bytes[ i ] = ( byte )( bytes[ i ] - passwordBytes[ passwordShiftIndex ] - seed );
passwordShiftIndex = ( passwordShiftIndex + 1 ) % passwordBytes.Length;
}
return Encoding.ASCII.GetString( bytes );
}
}
同样,我知道这不是一个直接的答案,但也许你可以从用户的方式中学到一些东西。