c# 中的 autokey vigenere 解密
autokey vigenere decryption in c#
这是我解密 autokey vigenere 密码算法的代码
string cipherText = "zicvtwqngkzeiigasxstslvvwla";
string key = "deceptive";
key = key.ToLower();
cipherText = cipherText.ToLower();
int klength = key.Length;
int kslength = (int)(cipherText.Length - key.Length);
string pl = string.Empty;
char[] newpl = new char[cipherText.Length];
int a = Convert.ToInt32('a');
for (int i = 0; i < cipherText.Length - kslength; i++)
{
int c = Convert.ToInt32(cipherText[i]) - a;
if (c< 0) c += 26;
int k = Convert.ToInt32(key[i]) - a;
if (k < 0) k += 26;
int p = (c - k);
p %= 26;
if (p < 0) p += 26;
p += a;
char temp = Convert.ToChar(p);
newpl[i] = temp;
}
char[] NewKey = new char[cipherText.Length];
char[] ciphertext = new char[cipherText.Length];
char[] chars = new char[cipherText.Length];
int count =0;
for (int i = 0; i < key.Length; i++)
{
NewKey[i] = key[i];
count++;
}
int j = 0;
for (int i = count; i < cipherText.Length; i++)
{
NewKey[i] = newpl[j];
j++;
}
Console.WriteLine(NewKey);
for (int i = klength; i < cipherText.Length; i++)
{
int c = Convert.ToInt32(cipherText[i]) - a;
int k = Convert.ToInt32(NewKey[i]) - a;
int p = (c - k);
p %= 26;
if (p < 0) p += 26;
p += a;
char temp = Convert.ToChar(p);
newpl[i] = temp;
}
pl = new string(newpl);
Console.WriteLine(pl);
它给了我输出:
deceptivewearedisc
wearediscoveredsavlmleoopet
而正确的输出应该是:
deceptivewearediscoveredsav
wearediscoveredsaveyourself
输出的第一行是指自动生成的密钥
第二行是解密后的文字
您的代码中有一些错误:
1) 看这一行:
for (int i = 0; i < cipherText.Length - kslength; i++)
kslength = cipherText.Length - key.Length
所以你的代码是
for (int i = 0; i < key.Length; i++)
密钥长度小于文本长度,因此您完成解密太早了。
2)
char temp = Convert.ToChar(p);
newpl[i] = temp;
您已解密符号,但使用自动密钥解密时,您应该将解密符号添加到您的密钥中。
3)
for (int i = 0; i < key.Length; i++)
应该改为 NewKey.Length
,因为 key
比修复 #2 后我们真正需要的要长。
固定码:
string cipherText = "zicvtwqngkzeiigasxstslvvwla";
string key = "deceptive";
key = key.ToLower();
cipherText = cipherText.ToLower();
int klength = key.Length;
string pl = string.Empty;
char[] newpl = new char[cipherText.Length];
int a = Convert.ToInt32('a');
for (int i = 0; i < cipherText.Length; i++)
{
int c = Convert.ToInt32(cipherText[i]) - a;
if (c < 0) c += 26;
int k = Convert.ToInt32(key[i]) - a;
if (k < 0) k += 26;
int p = (c - k);
p %= 26;
if (p < 0) p += 26;
p += a;
char temp = Convert.ToChar(p);
key += temp;
newpl[i] = temp;
}
char[] NewKey = new char[cipherText.Length];
int count = 0;
for (int i = 0; i < NewKey.Length; i++)
{
NewKey[i] = key[i];
count++;
}
int j = 0;
for (int i = count; i < cipherText.Length; i++)
{
NewKey[i] = newpl[j];
j++;
}
Console.WriteLine(NewKey);
for (int i = klength; i < cipherText.Length; i++)
{
int c = Convert.ToInt32(cipherText[i]) - a;
int k = Convert.ToInt32(NewKey[i]) - a;
int p = (c - k);
p %= 26;
if (p < 0) p += 26;
p += a;
char temp = Convert.ToChar(p);
newpl[i] = temp;
}
pl = new string(newpl);
Console.WriteLine(pl);
输出:
deceptivewearediscoveredsav
wearediscoveredsaveyourself
这是我解密 autokey vigenere 密码算法的代码
string cipherText = "zicvtwqngkzeiigasxstslvvwla";
string key = "deceptive";
key = key.ToLower();
cipherText = cipherText.ToLower();
int klength = key.Length;
int kslength = (int)(cipherText.Length - key.Length);
string pl = string.Empty;
char[] newpl = new char[cipherText.Length];
int a = Convert.ToInt32('a');
for (int i = 0; i < cipherText.Length - kslength; i++)
{
int c = Convert.ToInt32(cipherText[i]) - a;
if (c< 0) c += 26;
int k = Convert.ToInt32(key[i]) - a;
if (k < 0) k += 26;
int p = (c - k);
p %= 26;
if (p < 0) p += 26;
p += a;
char temp = Convert.ToChar(p);
newpl[i] = temp;
}
char[] NewKey = new char[cipherText.Length];
char[] ciphertext = new char[cipherText.Length];
char[] chars = new char[cipherText.Length];
int count =0;
for (int i = 0; i < key.Length; i++)
{
NewKey[i] = key[i];
count++;
}
int j = 0;
for (int i = count; i < cipherText.Length; i++)
{
NewKey[i] = newpl[j];
j++;
}
Console.WriteLine(NewKey);
for (int i = klength; i < cipherText.Length; i++)
{
int c = Convert.ToInt32(cipherText[i]) - a;
int k = Convert.ToInt32(NewKey[i]) - a;
int p = (c - k);
p %= 26;
if (p < 0) p += 26;
p += a;
char temp = Convert.ToChar(p);
newpl[i] = temp;
}
pl = new string(newpl);
Console.WriteLine(pl);
它给了我输出:
deceptivewearedisc
wearediscoveredsavlmleoopet
而正确的输出应该是:
deceptivewearediscoveredsav
wearediscoveredsaveyourself
输出的第一行是指自动生成的密钥
第二行是解密后的文字
您的代码中有一些错误:
1) 看这一行:
for (int i = 0; i < cipherText.Length - kslength; i++)
kslength = cipherText.Length - key.Length
所以你的代码是
for (int i = 0; i < key.Length; i++)
密钥长度小于文本长度,因此您完成解密太早了。
2)
char temp = Convert.ToChar(p);
newpl[i] = temp;
您已解密符号,但使用自动密钥解密时,您应该将解密符号添加到您的密钥中。
3)
for (int i = 0; i < key.Length; i++)
应该改为 NewKey.Length
,因为 key
比修复 #2 后我们真正需要的要长。
固定码:
string cipherText = "zicvtwqngkzeiigasxstslvvwla";
string key = "deceptive";
key = key.ToLower();
cipherText = cipherText.ToLower();
int klength = key.Length;
string pl = string.Empty;
char[] newpl = new char[cipherText.Length];
int a = Convert.ToInt32('a');
for (int i = 0; i < cipherText.Length; i++)
{
int c = Convert.ToInt32(cipherText[i]) - a;
if (c < 0) c += 26;
int k = Convert.ToInt32(key[i]) - a;
if (k < 0) k += 26;
int p = (c - k);
p %= 26;
if (p < 0) p += 26;
p += a;
char temp = Convert.ToChar(p);
key += temp;
newpl[i] = temp;
}
char[] NewKey = new char[cipherText.Length];
int count = 0;
for (int i = 0; i < NewKey.Length; i++)
{
NewKey[i] = key[i];
count++;
}
int j = 0;
for (int i = count; i < cipherText.Length; i++)
{
NewKey[i] = newpl[j];
j++;
}
Console.WriteLine(NewKey);
for (int i = klength; i < cipherText.Length; i++)
{
int c = Convert.ToInt32(cipherText[i]) - a;
int k = Convert.ToInt32(NewKey[i]) - a;
int p = (c - k);
p %= 26;
if (p < 0) p += 26;
p += a;
char temp = Convert.ToChar(p);
newpl[i] = temp;
}
pl = new string(newpl);
Console.WriteLine(pl);
输出:
deceptivewearediscoveredsav
wearediscoveredsaveyourself