rsa吧 关注:84贴子:248
  • 14回复贴,共1

delphi的Rsa算法源代码

只看楼主收藏回复



1楼2009-06-30 19:06回复
    文件1
    FGIntPrimeGeneration.PAS


    2楼2009-06-30 19:09
    回复
      {License, info, etc
       ------------------
      This implementation is made by Walied Othman, to contact me
      mail to Walied.Othman@Student.KULeuven.ac.be or
      Triade@ace.Ulyssis.Student.KULeuven.ac.be,
      always mention wether it 's about the FGInt for Delphi or for
      FreePascal, or wether it 's about the 6xs, preferably in the subject line.
      If you 're going to use these implementations, at least mention my
      name or something and notify me so I may even put a link on my page.
      This implementation is freeware and according to the coderpunks'
      manifesto it should remain so, so don 't use these implementations
      in commercial software.  Encryption, as a tool to ensure privacy
      should be free and accessible for anyone.  If you plan to use these
      implementations in a commercial application, contact me before
      doing so, that way you can license the software to use it in commercial
      Software.  If any algorithm is patented in your country, you should
      acquire a license before using this software.  Modified versions of this
      software must contain an acknowledgement of the original author (=me).
      This implementaion is available at
      http://ace.ulyssis.student.kuleuven.ac.be/~triade/
      copyright 2000, Walied Othman
      This header may not be removed.
      }


      3楼2009-06-30 19:10
      回复
        Unit FGIntPrimeGeneration;
        Interface
        Uses Windows, SysUtils, Controls, FGInt;
        Procedure PrimeSearch(Var GInt : TFGInt);
        Implementation
        {$H+}
        // Does an incremental search for primes starting from GInt,
        // when one is found, it is stored in GInt
        Procedure PrimeSearch(Var GInt : TFGInt);
        Var
           temp, two : TFGInt;
           ok : Boolean;
        Begin
           If (GInt.Number[1] Mod 2) = 0 Then GInt.Number[1] := GInt.Number[1] + 1;
           Base10StringToFGInt('2', two);
           ok := false;
           While Not ok Do
           Begin
              FGIntAdd(GInt, two, temp);
              FGIntCopy(temp, GInt);
              FGIntPrimeTest(GInt, 4, ok);
           End;
           FGIntDestroy(two);
        End;
        End.


        4楼2009-06-30 19:10
        回复
          文件2:FGIntRSA.PAS


          5楼2009-06-30 19:11
          回复
            {License, info, etc
             ------------------
            This implementation is made by Walied Othman, to contact me
            mail to Walied.Othman@Student.KULeuven.ac.be or
            Triade@ace.Ulyssis.Student.KULeuven.ac.be,
            always mention wether it 's about the FGInt for Delphi or for
            FreePascal, or wether it 's about the 6xs, preferably in the subject line.
            If you 're going to use these implementations, at least mention my
            name or something and notify me so I may even put a link on my page.
            This implementation is freeware and according to the coderpunks'
            manifesto it should remain so, so don 't use these implementations
            in commercial software.  Encryption, as a tool to ensure privacy
            should be free and accessible for anyone.  If you plan to use these
            implementations in a commercial application, contact me before
            doing so, that way you can license the software to use it in commercial
            Software.  If any algorithm is patented in your country, you should
            acquire a license before using this software.  Modified versions of this
            software must contain an acknowledgement of the original author (=me).
            This implementaion is available at
            http://ace.ulyssis.student.kuleuven.ac.be/~triade/
            copyright 2000, Walied Othman
            This header may not be removed.
            }


            6楼2009-06-30 19:11
            回复
              Unit FGIntRSA;
              Interface
              Uses Windows, SysUtils, Controls, FGInt;
              Procedure RSAEncrypt(P : String; Var exp, modb : TFGInt; Var E : String);
              Procedure RSADecrypt(E : String; Var exp, modb, d_p, d_q, p, q : TFGInt; Var D : String);
              Procedure RSASign(M : String; Var d, n, dp, dq, p, q : TFGInt; Var S : String);
              Procedure RSAVerify(M, S : String; Var e, n : TFGInt; Var valid : boolean);
              Implementation
              {$H+}


              7楼2009-06-30 19:12
              回复
                // Encrypt a string with the RSA algorithm, P^exp mod modb = E
                Procedure RSAEncrypt(P : String; Var exp, modb : TFGInt; Var E : String);
                Var
                   i, j, modbits : longint;
                   PGInt, temp, zero : TFGInt;
                   tempstr1, tempstr2, tempstr3 : String;
                Begin
                   Base2StringToFGInt('0', zero);
                   FGIntToBase2String(modb, tempstr1);
                   modbits := length(tempstr1);
                   convertBase256to2(P, tempstr1);
                   tempstr1 := '111' + tempstr1;
                   j := modbits - 1;
                   While (length(tempstr1) Mod j) <> 0 Do tempstr1 := '0' + tempstr1;
                   j := length(tempstr1) Div (modbits - 1);
                   tempstr2 := '';
                   For i := 1 To j Do
                   Begin
                      tempstr3 := copy(tempstr1, 1, modbits - 1);
                      While (copy(tempstr3, 1, 1) = '0') And (length(tempstr3) > 1) Do delete(tempstr3, 1, 1);
                      Base2StringToFGInt(tempstr3, PGInt);
                      delete(tempstr1, 1, modbits - 1);
                      If tempstr3 = '0' Then FGIntCopy(zero, temp) Else FGIntMontgomeryModExp(PGInt, exp, modb, temp);
                      FGIntDestroy(PGInt);
                      tempstr3 := '';
                      FGIntToBase2String(temp, tempstr3);
                      While (length(tempstr3) Mod modbits) <> 0 Do tempstr3 := '0' + tempstr3;
                      tempstr2 := tempstr2 + tempstr3;
                      FGIntdestroy(temp);
                   End;
                   While (tempstr2[1] = '0') And (length(tempstr2) > 1) Do delete(tempstr2, 1, 1);
                   ConvertBase2To256(tempstr2, E);
                   FGIntDestroy(zero);
                End;
                // Decrypt a string with the RSA algorithm, E^exp mod modb = D
                // provide nil for exp.Number if you want a speedup by using the chinese
                // remainder theorem, modb = p*q, d_p*e mod (p-1) = 1 and
                // d_q*e mod (q-1) where e is the encryption exponent used


                8楼2009-06-30 19:12
                回复
                  Procedure RSADecrypt(E : String; Var exp, modb, d_p, d_q, p, q : TFGInt; Var D : String);
                  Var
                     i, j, modbits : longint;
                     EGInt, temp, temp1, temp2, temp3, ppinvq, qqinvp, zero : TFGInt;
                     tempstr1, tempstr2, tempstr3 : String;
                  Begin
                     Base2StringToFGInt('0', zero);
                     FGIntToBase2String(modb, tempstr1);
                     modbits := length(tempstr1);
                     convertBase256to2(E, tempstr1);
                     While copy(tempstr1, 1, 1) = '0' Do delete(tempstr1, 1, 1);
                     While (length(tempstr1) Mod modbits) <> 0 Do tempstr1 := '0' + tempstr1;
                     If exp.Number = Nil Then
                     Begin
                        FGIntModInv(q, p, temp1);
                        FGIntMul(q, temp1, qqinvp);
                        FGIntDestroy(temp1);
                        FGIntModInv(p, q, temp1);
                        FGIntMul(p, temp1, ppinvq);
                        FGIntDestroy(temp1);
                     End;
                     j := length(tempstr1) Div modbits;
                     tempstr2 := '';
                     For i := 1 To j Do
                     Begin
                        tempstr3 := copy(tempstr1, 1, modbits);
                        While (copy(tempstr3, 1, 1) = '0') And (length(tempstr3) > 1) Do delete(tempstr3, 1, 1);
                        Base2StringToFGInt(tempstr3, EGInt);
                        delete(tempstr1, 1, modbits);
                        If tempstr3 = '0' Then FGIntCopy(zero, temp) Else
                        Begin
                           If exp.Number <> Nil Then FGIntMontgomeryModExp(EGInt, exp, modb, temp) Else
                           Begin
                              FGIntMontgomeryModExp(EGInt, d_p, p, temp1);
                              FGIntMul(temp1, qqinvp, temp3);
                              FGIntCopy(temp3, temp1);
                              FGIntMontgomeryModExp(EGInt, d_q, q, temp2);
                              FGIntMul(temp2, ppinvq, temp3);
                              FGIntCopy(temp3, temp2);
                              FGIntAddMod(temp1, temp2, modb, temp);
                              FGIntDestroy(temp1);
                              FGIntDestroy(temp2);
                           End;
                        End;
                        FGIntDestroy(EGInt);
                        tempstr3 := '';
                        FGIntToBase2String(temp, tempstr3);
                        While (length(tempstr3) Mod (modbits - 1)) <> 0 Do tempstr3 := '0' + tempstr3;
                        tempstr2 := tempstr2 + tempstr3;
                        FGIntdestroy(temp);
                     End;
                     If exp.Number = Nil Then
                     Begin
                        FGIntDestroy(ppinvq);
                        FGIntDestroy(qqinvp);
                     End;
                     While (Not (copy(tempstr2, 1, 3) = '111')) And (length(tempstr2) > 3) Do delete(tempstr2, 1, 1);
                     delete(tempstr2, 1, 3);
                     ConvertBase2To256(tempstr2, D);
                     FGIntDestroy(zero);
                  End;


                  9楼2009-06-30 19:12
                  回复
                    // Sign strings with the RSA algorithm, M^d mod n = S
                    // provide nil for exp.Number if you want a speedup by using the chinese
                    // remainder theorem, n = p*q, dp*e mod (p-1) = 1 and
                    // dq*e mod (q-1) where e is the encryption exponent used
                    Procedure RSASign(M : String; Var d, n, dp, dq, p, q : TFGInt; Var S : String);
                    Var
                       MGInt, SGInt, temp, temp1, temp2, temp3, ppinvq, qqinvp : TFGInt;
                    Begin
                       Base256StringToFGInt(M, MGInt);
                       If d.Number <> Nil Then FGIntMontgomeryModExp(MGInt, d, n, SGInt) Else
                       Begin
                          FGIntModInv(p, q, temp);
                          FGIntMul(p, temp, ppinvq);
                          FGIntDestroy(temp);
                          FGIntModInv(q, p, temp);
                          FGIntMul(q, temp, qqinvp);
                          FGIntDestroy(temp);
                          FGIntMontgomeryModExp(MGInt, dp, p, temp1);
                          FGIntMul(temp1, qqinvp, temp2);
                          FGIntCopy(temp2, temp1);
                          FGIntMontgomeryModExp(MGInt, dq, q, temp2);
                          FGIntMul(temp2, ppinvq, temp3);
                          FGIntCopy(temp3, temp2);
                          FGIntAddMod(temp1, temp2, n, SGInt);
                          FGIntDestroy(temp1);
                          FGIntDestroy(temp2);
                          FGIntDestroy(ppinvq);
                          FGIntDestroy(qqinvp);
                       End;
                       FGIntToBase256String(SGInt, S);
                       FGIntDestroy(MGInt);
                       FGIntDestroy(SGInt);
                    End;


                    10楼2009-06-30 19:13
                    回复
                      // Verify digitally signed strings with the RSA algorihthm,
                      // If M = S^e mod n then ok:=true else ok:=false
                      Procedure RSAVerify(M, S : String; Var e, n : TFGInt; Var valid : boolean);
                      Var
                         MGInt, SGInt, temp : TFGInt;
                      Begin
                         Base256StringToFGInt(S, SGInt);
                         Base256StringToFGInt(M, MGInt);
                         FGIntMod(MGInt, n, temp);
                         FGIntCopy(temp, MGInt);
                         FGIntMontgomeryModExp(SGInt, e, n, temp);
                         FGIntCopy(temp, SGInt);
                         valid := (FGIntCompareAbs(SGInt, MGInt) = Eq);
                         FGIntDestroy(SGInt);
                         FGIntDestroy(MGInt);
                      End;
                      End.


                      11楼2009-06-30 19:13
                      回复
                        文件3:FGInt.pas


                        12楼2009-06-30 19:14
                        回复
                          {License, info, etc
                           ------------------
                          This implementation is made by Walied Othman, to contact me
                          mail to Walied.Othman@Student.KULeuven.ac.be or
                          Triade@ace.Ulyssis.Student.KULeuven.ac.be,
                          always mention wether it 's about the FGInt for Delphi or for
                          FreePascal, or wether it 's about the 6xs, preferably in the subject line.
                          If you 're going to use these implementations, at least mention my
                          name or something and notify me so I may even put a link on my page.
                          This implementation is freeware and according to the coderpunks'
                          manifesto it should remain so, so don 't use these implementations
                          in commercial software.  Encryption, as a tool to ensure privacy
                          should be free and accessible for anyone.  If you plan to use these
                          implementations in a commercial application, contact me before
                          doing so, that way you can license the software to use it in commercial
                          Software.  If any algorithm is patented in your country, you should
                          acquire a license before using this software.  Modified versions of this
                          software must contain an acknowledgement of the original author (=me).
                          This implementaion is available at
                          http://ace.ulyssis.student.kuleuven.ac.be/~triade/
                          copyright 2000, Walied Othman
                          This header may not be removed.
                          }
                          Unit FGInt;
                          Interface
                          Uses Windows, SysUtils, Controls, Math;
                          Type
                             TCompare = (Lt, St, Eq, Er);
                             TSign = (negative, positive);
                             TFGInt = Record
                                Sign : TSign;
                                Number : Array Of int64;
                             End;
                          Procedure zeronetochar8(Var g : char; Const x : String);
                          Procedure zeronetochar6(Var g : integer; Const x : String);
                          Procedure initialize8(Var trans : Array Of String);
                          Procedure initialize6(Var trans : Array Of String);
                          


                          13楼2009-06-30 23:08
                          回复
                            Procedure initialize6PGP(Var trans : Array Of String);
                            Procedure ConvertBase256to64(Const str256 : String; Var str64 : String);
                            Procedure ConvertBase64to256(Const str64 : String; Var str256 : String);
                            Procedure ConvertBase256to2(Const str256 : String; Var str2 : String);
                            Procedure ConvertBase64to2(Const str64 : String; Var str2 : String);
                            Procedure ConvertBase2to256(str2 : String; Var str256 : String);
                            Procedure ConvertBase2to64(str2 : String; Var str64 : String);
                            Procedure ConvertBase256StringToHexString(Str256 : String; Var HexStr : String);
                            Procedure ConvertHexStringToBase256String(HexStr : String; Var Str256 : String);
                            Procedure PGPConvertBase256to64(Var str256, str64 : String);
                            Procedure PGPConvertBase64to256(str64 : String; Var str256 : String);
                            Procedure PGPConvertBase64to2(str64 : String; Var str2 : String);
                            Procedure Base10StringToFGInt(Base10 : String; Var FGInt : TFGInt);
                            Procedure FGIntToBase10String(Const FGInt : TFGInt; Var Base10 : String);
                            Procedure FGIntDestroy(Var FGInt : TFGInt);
                            Function FGIntCompareAbs(Const FGInt1, FGInt2 : TFGInt) : TCompare;
                            Procedure FGIntAdd(Const FGInt1, FGInt2 : TFGInt; Var Sum : TFGInt);
                            Procedure FGIntChangeSign(Var FGInt : TFGInt);
                            Procedure FGIntSub(Var FGInt1, FGInt2, dif : TFGInt);
                            Procedure FGIntMulByInt(Const FGInt : TFGInt; Var res : TFGInt; by : int64);
                            Procedure FGIntMulByIntbis(Var FGInt : TFGInt; by : int64);
                            Procedure FGIntDivByInt(Const FGInt : TFGInt; Var res : TFGInt; by : int64; Var modres : int64);
                            Procedure FGIntDivByIntBis(Var FGInt : TFGInt; by : int64; Var modres : int64);
                            Procedure FGIntModByInt(Const FGInt : TFGInt; by : int64; Var modres : int64);
                            Procedure FGIntAbs(Var FGInt : TFGInt);
                            Procedure FGIntCopy(Const FGInt1 : TFGInt; Var FGInt2 : TFGInt);
                            Procedure FGIntShiftLeft(Var FGInt : TFGInt);
                            Procedure FGIntShiftRight(Var FGInt : TFGInt);
                            Procedure FGIntShiftRightBy31(Var FGInt : TFGInt);
                            Procedure FGIntAddBis(Var FGInt1 : TFGInt; Const FGInt2 : TFGInt);
                            


                            14楼2009-06-30 23:08
                            回复
                              这个库不好使,VC和java的库可以通用。
                              delphi的这个fgint库,只能自己用,烦死了。


                              16楼2012-03-10 17:04
                              回复