我正在制作一个自定义加密/解密软件。我已经写了代码:
public int CountCharInStringAccordingToArray(string Text)
{
int Count = 0;
foreach (char x in Text)
{
Count++;
}
return Count - 1;
}
public int CountCharInString(string Text)
{
int Count = 0;
foreach (char x in Text)
{
Count++;
}
return Count;
}
public string Encrypt(string Key, string PlainText)
{
int[] TempKey = new int[CountCharInString(Key)];
int[] TempText = new int[CountCharInString(PlainText)];
int[] EncryptedInt = new int[CountCharInString(PlainText)];
char[] EncryptedChar = new char[CountCharInString(PlainText)];
for (int i = 0; i < CountCharInStringAccordingToArray(Key); i++)
{
TempKey[i] = (int)Key[i];
}
for (int i = 0; i < CountCharInStringAccordingToArray(PlainText); i++)
{
TempText[i] = (int)PlainText[i];
}
int Index = 0;
for (int i = 0; i < CountCharInStringAccordingToArray(PlainText); i++)
{
if (Index == CountCharInStringAccordingToArray(Key))
{
Index = 0;
}
EncryptedInt[i] = TempKey[Index] + TempText[i];
Index++;
EncryptedChar[i] = (char)EncryptedInt[i];
}
return new string(EncryptedChar);
}
public string Decrypt(string Key, string EncryptedText)
{
int[] TempKey = new int[CountCharInString(Key)];
int[] TempText = new int[CountCharInString(EncryptedText)];
int[] DecryptedInt = new int[CountCharInString(EncryptedText)];
char[] DecryptedChar = new char[CountCharInString(EncryptedText)];
for (int i = 0; i < CountCharInStringAccordingToArray(Key); i++)
{
TempKey[i] = (int)Key[i];
}
for (int i = 0; i < CountCharInStringAccordingToArray(EncryptedText); i++)
{
TempText[i] = (int)EncryptedText[i];
}
int Index = 0;
for (int i = 0; i < CountCharInStringAccordingToArray(EncryptedText); i++)
{
if (Index == CountCharInStringAccordingToArray(Key))
{
Index = 0;
}
DecryptedInt[i] = TempText[i] - TempKey[Index];
Index++;
DecryptedChar[i] = (char)DecryptedInt[i];
}
return new string(DecryptedChar);
}
但我的问题是当我加密时:
键 = abc123
PlainText = 大家好,你们好吗?
加密结果:§ÑÕ
当我解密时:
键 = abc123
加密文本 = §ÑÕ
解密结果:゚゙ンᅥ
当我解密加密文本时,我得到的东西与“大家好,你们好吗?”完全不同。
有人可以帮忙吗?提前致谢。