gpt4 book ai didi

c# - c# 中的 autokey vigenere 解密

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:32 25 4
gpt4 key购买 nike

这是我解密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

输出的第一行是指自动生成的 key

第二行是解密后的文本。

最佳答案

您的代码中几乎没有错误:

1) 看这一行:

for (int i = 0; i < cipherText.Length - kslength; i++)

kslength = cipherText.Length - key.Length 所以你的代码是

for (int i = 0; i < key.Length; i++)

key 长度 < 小于文本长度,因此您过早完成解密。

2)

char temp = Convert.ToChar(p);
newpl[i] = temp;

您解密了符号,但是使用自动 key 解密,您应该将解密的符号添加到您的 key 中。

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

关于c# - c# 中的 autokey vigenere 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55150785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com