Мегаобучалка Главная | О нас | Обратная связь


Приложение к курсовой работе



2019-12-29 276 Обсуждений (0)
Приложение к курсовой работе 0.00 из 5.00 0 оценок




 

ПРОГРАММА РЕАЛИЗАЦИИ АЛГОРИТМА RSA 

 

Файл FGIntPrimeGeneration.PAS

 

Unit FGIntPrimeGeneration;

Interface

Uses Windows, SysUtils, Controls, FGInt;

Procedure PrimeSearch(Var GInt : TFGInt);

Implementation

{$H+}

 

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.

 

Файл FGIntRSA.PAS

 

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+}

 

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;

 

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;

 

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;

 

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.

 

Файл Unit1.pas

 

unit Unit1;

 

interface

 

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs,

FGInt, FGIntPrimeGeneration, FGIntRSA, StdCtrls;

 

type

TForm1 = class(TForm)

Edit1: TEdit;

Label1: TLabel;

CheckBox1: TCheckBox;

Label2: TLabel;

Edit2: TEdit;

CheckBox2: TCheckBox;

Button1: TButton;

Label3: TLabel;

Label4: TLabel;

Label5: TLabel;

Label6: TLabel;

Label7: TLabel;

Label8: TLabel;

Label9: TLabel;

Label10: TLabel;

Label11: TLabel;

Label12: TLabel;

Edit3: TEdit;

Label13: TLabel;

Button2: TButton;

Label14: TLabel;

Label15: TLabel;

Label16: TLabel;

Label17: TLabel;

Button3: TButton;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

 

var

Form1: TForm1;

n, e, d, dp, dq, p, q, phi, one, two, gcd, temp, nilgint : TFGInt;

test, signature : String;

ok : boolean;

st: string;

tx: text;

implementation

 

{$R *.dfm}

 

procedure TForm1.Button1Click(Sender: TObject);

begin

if CheckBox1.Checked then

Base10StringToFGInt(Edit1.Text, p) else

Base256StringToFGInt(Edit1.Text, p);

if CheckBox2.Checked then

Base10StringToFGInt(Edit2.Text, q) else

Base256StringToFGInt(Edit2.Text, q);

PrimeSearch(p);

PrimeSearch(q);

if CheckBox1.Checked then

FGIntToBase10String(p, st) else

FGIntToBase256String(p, st);

Edit1.Text:=st;

if CheckBox2.Checked then

FGIntToBase10String(q, st) else

FGIntToBase256String(q, st);

Edit2.Text:=st;

FGIntMul(p, q, n);

p.Number[1] := p.Number[1] - 1;

q.Number[1] := q.Number[1] - 1;

FGIntMul(p, q, phi);

FGIntToBase10String(n, st);

Label5.Caption:=st;

FGIntToBase10String(phi, st);

Label6.Caption:=st;

Base10StringToFGInt('14486581214143', e);

Base10StringToFGInt('1', one);

Base10StringToFGInt('2', two);

FGIntGCD(phi, e, gcd);

While FGIntCompareAbs(gcd, one) <> Eq Do

Begin

FGIntadd(e, two, temp);

FGIntCopy(temp, e);

FGIntGCD(phi, e, gcd);

End;

FGIntDestroy(two);

FGIntDestroy(one);

FGIntDestroy(gcd);

FGIntModInv(e, phi, d);

FGIntModInv(e, p, dp);

FGIntModInv(e, q, dq);

p.Number[1] := p.Number[1] + 1;

q.Number[1] := q.Number[1] + 1;

FGIntDestroy(phi);

FGIntDestroy(nilgint);

FGIntToBase256String(e, st);

Label8.Caption:=st;

FGIntToBase10String(d, st);

Label10.Caption:=st;

end;

 

procedure TForm1.Button2Click(Sender: TObject);

var prom: string;

begin

test := Edit3.Text;

RSAEncrypt(test, e, n, test);

Label11.Caption:=test;

PGPConvertBase256to64(test,prom);

Label17.Caption:=prom;

RSADecrypt(test, d, n, Nilgint, Nilgint, Nilgint, Nilgint, test);

Label12.Caption:=test;

 

end;

 

procedure TForm1.Button3Click(Sender: TObject);

begin

AssignFile(tx,'key.txt');

ReWrite(tx); CloseFile(tx);

Append(tx);

FGIntToBase256String(n, st);

ConvertBase256to64(st, st);

WriteLn(tx,st);

FGIntToBase256String(e, st);

ConvertBase256to64(st, st);

WriteLn(tx,st);

FGIntToBase256String(d, st);

ConvertBase256to64(st, st);

WriteLn(tx,st);

CloseFile(tx);

end;

 

end.

Результат выполнения программы:


 



2019-12-29 276 Обсуждений (0)
Приложение к курсовой работе 0.00 из 5.00 0 оценок









Обсуждение в статье: Приложение к курсовой работе

Обсуждений еще не было, будьте первым... ↓↓↓

Отправить сообщение

Популярное:
Как построить свою речь (словесное оформление): При подготовке публичного выступления перед оратором возникает вопрос, как лучше словесно оформить свою...
Почему люди поддаются рекламе?: Только не надо искать ответы в качестве или количестве рекламы...
Организация как механизм и форма жизни коллектива: Организация не сможет достичь поставленных целей без соответствующей внутренней...
Почему двоичная система счисления так распространена?: Каждая цифра должна быть как-то представлена на физическом носителе...



©2015-2024 megaobuchalka.ru Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. (276)

Почему 1285321 студент выбрали МегаОбучалку...

Система поиска информации

Мобильная версия сайта

Удобная навигация

Нет шокирующей рекламы



(0.006 сек.)