문제

this piece of code does return the right answer whenever i run it once on the right crypting key, but if i run it twice it gives completely different result

int key = Int32.Parse(MinKeyTB.Text, System.Globalization.NumberStyles.HexNumber);
CR = new Crypto(key);
textBox3.Text = string.Empty;
foreach (string temp in (ASCIIEncoding.ASCII.GetString(CR.Decrypt(ToBurtal))).Split('\n'))
{
    textBox3.Text += temp + Environment.NewLine;
}
key++;
MinKeyTB.Text = (key).ToString("X");

the min key textbox is where i type the key to be used, the right key is 1234 which works fine but if i ran it twice with 1233 then 1234 it gives a completely wrong answer

key is local variable (should not cause a problem) CR is a custom class type which i reference it with new to a new object (should not cause a problem) i clean textbox3 everytime before display (should not cause a problem) ToBurtal is global byte[] which doesn't get modified anywhere in the application

any idea ? thanks and have a wonderful day

도움이 되었습니까?

해결책

My guess is that ToBurtal is getting modified inside the Decrypt function. Try it with a local copy of the array and see if that fixes your issue:

byte[] ToBurtalTemp = new byte[ToBurtal.Length];
Array.Copy(ToBurtal, ToBurtalTemp, ToBurtal.Length);

foreach (string temp in (ASCIIEncoding.ASCII.GetString(CR.Decrypt(ToBurtalTemp))).Split('\n'))
// etc.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top