📄 dsa.pas
字号:
{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, or ICQ me on 20388046.
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 applications. 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. If any algorithm is patented in your country, you should
acquire a license before using this software. Modified versions of this
software must remain in the public domain and must contain an
acknowledgement of the original author (=me).
This implementaion is available at
http://ace.ulyssis.student.kuleuven.ac.be/~triade/GInt/index.htm
copyright 1999, Walied Othman
This header may not be removed.}
Unit DSA;
Interface
Uses Windows, SysUtils, Controls, GInt;
Procedure DSAPrimeSearch(q : TGInt; Var p : TGInt);
Procedure DSASign(p, q, g, x, k : TGInt; M : String; Var r, s : String);
Procedure DSAVerify(p, q, g, y : TGInt; m, r, s : String; Var ok : Boolean);
Implementation
{$H+}
// Searches for a prime p such that p mod q = 1, when calling
// this procedure, provide a random GInt for p
Procedure DSAPrimeSearch(q : TGInt; Var p : TGInt);
Var
q2, one, temp1, temp2 : TGInt;
ok : boolean;
Begin
GIntAdd(q, q, q2);
GIntMod(p, q, temp1);
DecStrToGInt('1', one);
GIntSub(p, temp1, temp2);
GIntDestroy(temp1);
GIntAdd(temp2, one, temp1);
GIntDestroy(temp2);
GIntDestroy(one);
If (temp1^.value Mod 2) = 0 Then
Begin
GIntadd(temp1, q, temp2);
GIntDestroy(temp1);
temp1 := temp2;
End;
GIntdestroy(p);
p := temp1;
ok := false;
While Not ok Do
Begin
GIntadd(p, q2, temp1);
GIntDestroy(p);
p := temp1;
GIntPrimeTest(p, 5, ok);
End;
GIntDestroy(q2);
End;
// p is a prime, (according to the standard, p is i, where i
// ranges from 512 to 1024, bits long and i is a multiple of 64)
// q is a primefactor of p-1, (in the standard q is 160 bit)
// g=h^((p-1)/q) mod p, h random but g > 1
// x secret key, a number less than q
// k random, less than q, same k must not be used twice and kept secret
// M the string you want to sign
// r,s form the signature
Procedure DSASign(p, q, g, x, k : TGInt; M : String; Var r, s : String);
Var
temp1, temp2, temp3, temp4, RGInt, SGInt : TGInt;
Begin
GIntModExp(g, k, p, temp1);
GIntMod(temp1, q, RGInt);
GIntdestroy(temp1);
GIntModInv(k, q, temp1);
strtoGInt(m, temp2);
GIntMulMod(x, RGInt, q, temp3);
GIntAdd(temp2, temp3, temp4);
GIntDestroy(temp2);
GIntDestroy(temp3);
GIntMulMod(temp1, temp4, q, SGInt);
GIntDestroy(temp1);
GIntDestroy(temp4);
GIntToStr(RGInt, r);
GIntToStr(SGInt, s);
GIntDestroy(RGInt);
GIntDestroy(SGInt);
End;
// p is a prime, (according to the standard, p is i, where i
// ranges from 512 to 1024, bits long and i is a multiple of 64)
// q is a primefactor of p-1, (in the standard q is 160 bit)
// g = h^((p-1)/q) mod p, h random but g > 1
// y = g^x mod p
// m is the signed string, r,s form the signature, ok returns
// true if the signature is valid
Procedure DSAVerify(p, q, g, y : TGInt; m, r, s : String; Var ok : Boolean);
Var
w, u1, u2, v, RGInt, SGInt, temp1, temp2, temp3 : TGInt;
Begin
StrToGInt(s, SGInt);
GIntModInv(SGInt, q, w);
StrToGInt(m, temp1);
GIntMulMod(temp1, w, q, u1);
GIntDestroy(temp1);
StrToGInt(r, RGInt);
GIntMulMod(RGInt, w, q, u2);
GIntDestroy(w);
GIntModExp(g, u1, p, temp1);
GIntModExp(y, u2, p, temp2);
GIntMulMod(temp1, temp2, p, temp3);
GIntDestroy(temp1);
GIntDestroy(temp2);
GIntMod(temp3, q, v);
GIntDestroy(temp3);
GIntDestroy(u1);
GIntDestroy(u2);
GIntDestroy(SGInt);
If GIntCompareAbs(RGInt, v) = Eq Then ok := true Else ok := false;
GIntDestroy(v);
GIntDestroy(RGInt);
End;
End.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -