Message Confidentiality Practice(RSA and base 64)

Click here to download code (NB: You should register first)

[download id=”3″]

 

Part 1

Message Confidentiality
Practice

This demo made a simple implementation of Message Confidentiality. It encrypt/decrypt with a message using public/private key pair.

Author: Huan Meng huanm@kth.se

 

The process of operation:

1. Generate two random numbers:

 

2. Generate key pairs:

 

3. Click “Ecryption” and get the Ciphertext(record the ciphertext). Input the record ciphertext or delete the plaintext and click “Decryption”:

 

The main source code is as follows:

unit Unit1;

 

interface

 

uses

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

StdCtrls, Spin, ComCtrls;

 

type

TForm1 = class(TForm)

Button1: TButton;

Button2: TButton;

Edit1: TEdit;

Edit2: TEdit;

Edit3: TEdit;

Edit4: TEdit;

Edit5: TEdit;

Edit6: TEdit;

Edit7: TEdit;

Edit8: TEdit;

Edit9: TEdit;

Edit10: TEdit;

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Label4: TLabel;

Label5: TLabel;

Label6: TLabel;

Label7: TLabel;

Label8: TLabel;

Label9: TLabel;

Label10: TLabel;

Button3: TButton;

Button4: TButton;

Button5: TButton;

edit13: TMemo;

edit11: TRichEdit;

Label11: TLabel;

Label12: TLabel;

Edit12: TEdit;

Label14: TLabel;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure Button4Click(Sender: TObject);

procedure Button5Click(Sender: TObject);

procedure Button6Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

 

var

Form1: TForm1;

 

implementation

 

uses ranlib, FGInt, FGIntPrimeGeneration, FGIntRSA;

 

{$R *.DFM}

 

procedure TForm1.Button1Click(Sender: TObject);

 

var seed, i: integer;

var s,s2: string;

 

begin

seed := Round(Time()*3600000.0);

 

WRandomInit(seed);

 

for i := 1 to 20 do begin

WRandom();

WIRandom(0,1000);

end;

s:=floattostr(WIRandom(0,1000000000));

s2:=floattostr(WIRandom(0,1000000000));

 

edit1.text:=s;

edit2.text:=s2;

end;

 

procedure TForm1.Button2Click(Sender: TObject);

var

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

test, signature : String;

p1,q1,dp1,dq1,e1,d1,n1,nil1,gcdStr:string;

ok : boolean;

begin

Base10StringToFGInt(edit1.text, p);

PrimeSearch(p);

Base256StringToFGInt(edit2.text, q);

PrimeSearch(q);

// Compute the modulus

FGIntMul(p, q, n);

 

// Compute p-1, q-1 by adjusting the last digit of the GInt

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

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

// Compute phi(n)

FGIntMul(p, q, phi);//作(p-1)*(q-1)=phi

// Choose a public exponent e such that GCD(e,phi)=1

Base10StringToFGInt(‘65537’, 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;

FGIntToBase10String(e,gcdstr);

edit12.text:=gcdstr;

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;

Base10StringToFGInt(‘100′, te1);

Base10StringToFGInt(’25’, te2);

FGIntMul(te1,te2,te3);

FGIntToBase10String(p,p1);

edit3.text:=p1;

FGIntToBase10String(q,q1);

edit4.text:=q1;

FGIntToBase10String(dp,dp1);

edit5.text:=dp1;

FGIntToBase10String(dq,dq1);

edit6.text:=dq1;

FGIntToBase10String(e,e1);

edit7.text:=e1;

FGIntToBase10String(d,d1);

edit8.text:=d1;

FGIntToBase10String(n,n1);

edit9.text:=n1;

FGIntToBase10String(phi,nil1);

edit10.text:=nil1;

FGIntDestroy(phi);

 

FGIntDestroy(nilgint);

 

 

FGIntDestroy(p);

FGIntDestroy(q);

FGIntDestroy(dp);

FGIntDestroy(dq);

FGIntDestroy(e);

FGIntDestroy(d);

FGIntDestroy(n);

end;

 

 

procedure TForm1.Button3Click(Sender: TObject);

var

test,s,b64:string;

e,n:tfgint;

stin,ms:integer;

begin

test:=edit11.text;

Base10StringToFGInt(edit7.Text, e);

Base10StringToFGInt(edit9.text, n);

RSAEncrypt(test, e, n, test);

edit13.Clear;

ConvertBase256to64(test,b64);

//stin:=length(test) div 10;

//ms:=length(test) mod 10;

//s:=

edit13.lines.Add(b64);

 

FGIntDestroy(e);

FGIntDestroy(n);

 

end;

 

procedure TForm1.Button4Click(Sender: TObject);

var

test,b64:string;

d,n,nilgint:tfgint;

begin

test:=edit13.text;

ConvertBase64to256(test,b64);

test:=”;

Base10StringToFGInt(edit8.Text, d);

Base10StringToFGInt(edit9.text, n);

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

edit11.text:=test;

FGIntDestroy(d);

FGIntDestroy(n);

FGIntDestroy(nilgint);

end;

 

procedure TForm1.Button5Click(Sender: TObject);

begin

edit11.clear;

edit13.clear;

end;

 

procedure TForm1.Button6Click(Sender: TObject);

var

a,b,c:tfgint;

ss:string;

begin

Base10StringToFGInt(edit1.Text,a);

Base10StringToFGInt(edit2.Text,b);

FGIntModInv(a,b,c);

FGIntToBase10String(c,ss);

 

FGIntDestroy(a);

FGIntDestroy(b);

FGIntDestroy(c);

 

end;

 

end.

 

 

end.

 

Conclusion

By encrypting the arbitrary message using secret key cryptography with a public/private key pair, it assures the confidentiality of the message.

Compile environment: Delphi 7

 

 

Part 2

Message Confidentiality Practice

This demo made a simple implementation of Message Confidentiality. It encrypt/decrypt an arbitrary message using secret key cryptography, including Base64 encoding.

Author: Jing Ba jingb@kth.se

 

The process of operation:

1. Input the arbitrary message:


 

2. Click “Base64 Ecryption” and get the Ciphertext(record the ciphertext):


3. Input the record ciphertext or delete the plaintext and click “Base64 Decryption”:


 

4. We get the plaintext:


 

The main source code is as follows:

unit Unit1;

 

interface

 

uses

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

Dialogs, StdCtrls;

 

type

TForm1 = class(TForm)

Button1: TButton;

Button2: TButton;

Edit1: TEdit;

Edit2: TEdit;

Label1: TLabel;

Label2: TLabel;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

 

var

Form1: TForm1;

function Base64Encode(const s: string): string;

function Base64Decode(const s: string): string;

implementation

 

{$R *.dfm}

 

function Base64Encode(const s: string): string;

var

i,c1,c2,c3: Integer;

m,n: Integer;

const

Base64: string = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/’;

begin

Result := ”;

m:=1;

n:=0;

for i := 1 to (Length(s) div 3) do

begin

c1 := Ord(s[m]);

c2 := Ord(s[m+1]);

c3 := Ord(s[m+2]);

m:=m+3;

Result := Result+base64[(c1 shr 2)and $3F+1];

Result := Result+base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];

Result := Result+base64[((c2 shl 2)and $3C) or ((c3 shr 6)and $03)+1];

Result := Result+base64[c3 and $3F+1];

n:=n+4;

if(n = 76)then

begin

n:=0;

Result := Result+#13#10;

end;

end;

if (Length(s) mod 3)=1 then

begin

c1 := Ord(s[m]);

Result := Result+base64[(c1 shr 2)and $3F+1];

Result := Result+base64[(c1 shl 4)and $30+1];

Result := Result+’=’;

Result := Result+’=’;

end;

if (Length(s) mod 3)=2 then

begin

c1 := Ord(s[m]);

c2 := Ord(s[m+1]);

Result := Result+ base64[(c1 shr 2)and $3F+1];

Result := Result+ base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];

Result := Result+base64[(c2 shl 2)and $3C+1];

Result := Result+ ‘=’;

end;

end;

 

function Base64Decode(const s: string): string;

var

i,m,n: Integer;

c1,c2,c3,c4: Integer;

const

Base64: string = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/’;

begin

Result := ”;

n:=1;

m:=Length(s);

if s[m]=’=’then m:=m-1;

if s[m]=’=’then m:=m-1;

for i:=1 to m div 4 do

begin

c1:=Pos(s[n],Base64)-1;

c2:=Pos(s[n+1],Base64)-1;

c3:=Pos(s[n+2],Base64)-1;

c4:=Pos(s[n+3],Base64)-1;

n:=n+4;

Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));

Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));

Result:=Result+Chr(((c3 shl 6)and $C0)or c4);

end;

if m mod 4=2 then

begin

c1:=Pos(s[n],Base64)-1;

c2:=Pos(s[n+1],Base64)-1;

Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));

end;

 

if m mod 4=3 then

begin

c1:=Pos(s[n],Base64)-1;

c2:=Pos(s[n+1],Base64)-1;

c3:=Pos(s[n+2],Base64)-1;

Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));

Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));

end;

 

end;

 

procedure TForm1.Button1Click(Sender: TObject);

begin

Edit2.Text :=Base64Encode(Edit1.Text);

end;

 

procedure TForm1.Button2Click(Sender: TObject);

begin

Edit1.Text :=Base64Decode(Edit2.Text);

end;

 

procedure TForm1.FormCreate(Sender: TObject);

begin

 

end;

 

end.

 

 

Conclusion

By encrypting the arbitrary message using secret key cryptography with Base64 encoding, it assures the confidentiality of the message.

Compile environment: Delphi 7

Message Confidentiality Practice(RSA and base 64)》上有3条评论

  1. Importantecole

    _
    ===============================================
    ===============================================
    ===============================================

    Hello Friend!

    40 VERIFIABLE FREESPINS No Deposit

    >> https://lnkd.in/dj7ETHsK _

    Virtuousness chances!

    ===============================================
    ===============================================
    ===============================================
    _

    [回复]

    回复

Importantecole进行回复 取消回复

邮箱地址不会被公开。 必填项已用*标注