Autor Beitrag
glotzer
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 393
Erhaltene Danke: 49

Win 7
Lazarus
BeitragVerfasst: Do 05.01.12 08:24 
Hallo alle zusammen,

ich veruche einen Quelltext von c# nach Delphi zu übersetzen, er entschlüsselt ein Netzwerkpacket.

das Orginal lautet
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
        public static bool RsaOTDecrypt(ref byte[] buffer, int position, int length)
        {
            if (length - position != 128)
                return false;

            byte[] temp = new byte[128];
            Array.Copy(buffer, position, temp, 0128);

            BigInteger input = new BigInteger(temp);
            BigInteger output;

            BigInteger m1 = input.modPow(otServerDP, otServerP);
            // m2 = c^dq mod q
            BigInteger m2 = input.modPow(otServerDQ, otServerQ);
            BigInteger h;

            if (m2 > m1)
            {
                // thanks to benm!
                h = otServerP - ((m2 - m1) * otServerInverseQ % otServerP);
                output = m2 + otServerQ * h;
            }
            else
            {
                // h = (m1 - m2) * qInv mod p
                h = (m1 - m2) * otServerInverseQ % otServerP;
                // m = m2 + q * h;
                output = m2 + otServerQ * h;
            }

            Array.Copy(GetPaddedValue(output), 0, buffer, position, 128);
            return true;
        }
        #endregion

        #region Private Functions

        private static byte[] GetPaddedValue(BigInteger value)
        {
            byte[] result = value.getBytes();

            int length = (1024 >> 3);
            if (result.Length >= length)
                return result;

            // left-pad 0x00 value on the result (same integer, correct length)
            byte[] padded = new byte[length];
            System.Buffer.BlockCopy(result, 0, padded, (length - result.Length), result.Length);
            // temporary result may contain decrypted (plaintext) data, clear it
            Array.Clear(result, 0, result.Length);
            return padded;
        }

        #endregion
    }


in Delphi hab ich daraus (zusammen mit BigNum2)

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
function GetPaddedValue(value: TBigNumber): TBytes;
var
  l: integer;
  padded: TBytes;
begin
  result := TBytes(value);
  l := (1024 shr 3);
  if length(result) >= l then
  begin
    exit;
  end;

  padded := copy(result,(l - length(result)),length(result));
  result := padded;
end;



function RSADecrypt(buffer: PBytes;position: Integer;length:Integer): boolean;
var
  input,output,m1,m2,h: TBigNumber;
begin

  if (length - position <> 128then
  begin
    result :=  false;
    exit;
  end;
  SetLength(input,128);

  input := TBigNumber(copy(buffer^,position, 128));
  m1 := BM_PowerMod(input,otServerDP,otServerP);
  m2 := BM_PowerMod(input,otServerDQ,otServerQ);

  if BM_CompareG(m2,m1) then
  begin
    h := BM_Sub(otServerP,BM_MultiplyMod(BM_Sub(m2,m1),otServerInverseQ,otServerP));
    output := BM_Add(m2,BM_Multiply(otServerQ,h));
  end else
  begin
    h := BM_MultiplyMod(BM_Sub(m2,m1),otServerInverseQ,otServerP);
    output := BM_Add(m2,BM_Multiply(otServerQ,h));
  end;


  buffer^ := copy(GetPaddedValue(output),position,128);
  result := true;
end;


gemacht. einziges Problem: das Ergebniss passt nicht. Hat wer von euch eine Idee
was ich da falsch gemacht hab? Mein Problem ist vorallem das ich mit C# noch nie wirklich etwas gemacht hab.

Vielen Dank schonmal im Vorraus :D

_________________
ja, ich schreibe grundsätzlich alles klein und meine rechtschreibfehler sind absicht
Gammatester
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 328
Erhaltene Danke: 101



BeitragVerfasst: Do 05.01.12 13:14 
Im zweiten Zweig ist das Vorzeichen vertauscht, bzw. es wird total falsch gerechnet, da ja, falls ich mich richtig errinnere, Bignum2 keine Vorzeichen kennt ??!!

Der Zweig sollte also etwa so lauten (hier ist m2 <= m1):
ausblenden Delphi-Quelltext
1:
h := BM_MultiplyMod(BM_Sub(m1,m2),otServerInverseQ,otServerP);					
glotzer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 393
Erhaltene Danke: 49

Win 7
Lazarus
BeitragVerfasst: Do 05.01.12 18:42 
da hatte ich wohl was übersehn ^^ danke.
Nur leider geht es immer noch nicht. Da muss ich doch wirklich nochmal schauen ob BigNum wirklich keine Vorzeichen kann, falls dem so ist muss ich was anderes suchen ;(

_________________
ja, ich schreibe grundsätzlich alles klein und meine rechtschreibfehler sind absicht