我想制作加密方法,接受大写和小写都在纯文本变量中(“Hello World”它只接受小写字母请帮助我,我想要amke 它的工作正常,只有小写字母作为输入请帮助我
using System;
class SubstitutionCipher
{
static void Main()
{
string key = "jfkgotmyvhspcandxlrwebquiz";
string plainText = "the quick brown fox jumps over the lazy dog";
string cipherText = Encrypt(plainText, key);
string decryptedText = Decrypt(cipherText, key);
Console.WriteLine("Plain : {0}", plainText);
Console.WriteLine("Encrypted : {0}", cipherText);
Console.WriteLine("Decrypted : {0}", plainText);
Console.ReadKey();
}
//encryption method
static string Encrypt(string plainText, string key)
{
char[] chars = new char[plainText.Length];
for(int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = plainText[i] - 97;
chars[i] = key[j];
}
}
return new string(chars);
}
//decryption method
static string Decrypt(string cipherText, string key)
{
char[] chars = new char[cipherText.Length];
for(int i = 0; i < cipherText.Length; i++)
{
if (cipherText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = key.IndexOf(cipherText[i]) - 97;
chars[i] = (char)j;
}
}
return new string(chars);
}
}
答案 0 :(得分:0)
您正在减去'a',这仅适用于小写字符。你应该检查它是大写还是小写,然后分别减去A或a。此外,您可以直接使用“a”或“A”来获得与int
等效的字符。
我的意思是将此用于解密和加密的else
语句:
static string Encrypt(string plainText, string key)
{
char[] chars = new char[plainText.Length];
for (int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else if (Char.IsUpper(plainText[i]))
{
int j = plainText[i] - 'A';
chars[i] = key[j];
}
else
{
int j = plainText[i] - 'a';
chars[i] = key[j];
}
}
return new string(chars);
static string Decrypt(string cipherText, string key)
{
char[] chars = new char[cipherText.Length];
for (int i = 0; i < cipherText.Length; i++)
{
if (cipherText[i] == ' ')
{
chars[i] = ' ';
}
else if (Char.IsUpper(cipherText[i]))
{
int j = key.IndexOf(cipherText[i]) - 'A';
chars[i] = (char)j;
}
else
{
int j = key.IndexOf(cipherText[i]) - 'a';
chars[i] = (char)j;
}
}
return new string(chars);
}
或加密和解密的短版本:
else
{
int j = Char.IsUpper(plainText[i]) ? plainText[i] - 'A' : plainText[i] - 'a';
chars[i] = key[j];
}
else
{
int j = Char.IsUpper(cipherText[i]) ? key.IndexOf(cipherText[i]) - 'A' : key.IndexOf(cipherText[i]) - 'a';
chars[i] = (char)j;
}
修改强>
好的,我认为算法已经完成了,我刚刚修复了导致异常的部分。但是,你是:
a)没有考虑哪个字母是大写的 b)没有工作的Decryptor。 (我认为它有效,但是你的主要方法有明文结果作为解密的消息)。
要修复您的解密器,您需要在加密器上添加一种存储大写字母的方法。你可以对它更加复杂,但是现在,我们只是将其加密为密文上的大写字母。
static string Encrypt(string plainText, string key)
{
char[] chars = new char[plainText.Length];
for (int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = Char.IsUpper(plainText[i]) ? plainText[i] - 'A' : plainText[i] - 'a';
chars[i] = Char.IsUpper(plainText[i]) ? Char.ToUpper(key[j]) : key[j];
}
}
return new string(chars);
解密你应该在键上找到它作为小写字符,但输出为大写字符。
static string Decrypt(string cipherText, string key)
{
char[] chars = new char[cipherText.Length];
for (int i = 0; i < cipherText.Length; i++)
{
if (cipherText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = Char.IsUpper(cipherText[i]) ? key.IndexOf(Char.ToLower((char)cipherText[i])) + 'a' : key.IndexOf(cipherText[i]) + 'a';
chars[i] = Char.IsUpper(cipherText[i]) ? Char.ToUpper((char)j) : (char)j;
}
}
return new string(chars);
}
答案 1 :(得分:0)
这是一个有趣的问题,尤其是在一般情况中陈述时。所以我们有
a
,j
)为了提高效率,让我们设计一个Encryptor
\ Decryptor
作为Dictionary<Char, Char>
,为了做到这一点,我们应该对密钥中的字符进行排序并对其进行对应;
例如为了
key = "jfkgo"
sorted key = "fgjko"
对将是
{j, f} // when encrypting "j" becomes "f", on decryption "f" turns into "j"
{f, g}
{k, j}
{g, k}
{o, o}
从加密时,我们有加密,当从右到左读 解密时:< / p>
private static Dictionary<Char, Char> Encryptor(String key) {
Char[] data = key.ToArray();
Char[] sorted = data.OrderBy(c => c).ToArray();
return data
.Zip(sorted, (plain, encrypted) => new {
Plain = plain,
Encrypted = encrypted
})
.ToDictionary(item => item.Plain, item => item.Encrypted);
}
// Note, that Decryptor is the same as Encryptor by for the last line
private static Dictionary<Char, Char> Decryptor(String key) {
Char[] data = key.ToArray();
Char[] sorted = data.OrderBy(c => c).ToArray();
return data
.Zip(sorted, (plain, encrypted) => new {
Plain = plain,
Encrypted = encrypted
})
.ToDictionary(item => item.Encrypted, item => item.Plain);
}
现在,实现加密和解密方法很容易
private static String Encrypt(String source, String key) {
var dict = Encryptor(key);
StringBuilder Sb = new StringBuilder(source.Length);
Char sub;
foreach (var ch in source)
if (dict.TryGetValue(ch, out sub))
Sb.Append(sub);
else
Sb.Append(ch);
return Sb.ToString();
}
// Note, that Decryptor is the same as Encryptor by for the first line
private static String Decrypt(String source, String key) {
var dict = Decryptor(key);
StringBuilder Sb = new StringBuilder(source.Length);
Char sub;
foreach (var ch in source)
if (dict.TryGetValue(ch, out sub))
Sb.Append(sub);
else
Sb.Append(ch);
return Sb.ToString();
}
小测试
// For test, let capital letters mmimic the small ones
string key = "jfkgotmyvhspcandxlrwebquiz" + "jfkgotmyvhspcandxlrwebquiz".ToUpper();
string plainText = "The Quick Brown Fox Jumps Over The Lazy Dog!";
string cipherText = Encrypt(plainText, key);
string decryptedText = Decrypt(cipherText, key);
Console.WriteLine("Plain : {0}", plainText);
Console.WriteLine("Encrypted : {0}", cipherText);
Console.WriteLine("Decrypted : {0}", plainText);