カスタム暗号化のコードを書きました。挑戦です。現在、次のような理由で機能しません。
Encrypt("abc", "こんにちはみんな") 戻り値: sdfhsjfjs Decrypt("abc", "sdfhsjfjs") 戻り値: diuifidu
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);
}
また、文字列には長さのプロパティがあることを知っていますが、それを修正するのを忘れています。