www.pudn.com > rsa.zip > RSA.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 RSA; 
 
Interface 
 
Uses Windows, SysUtils, Controls, GInt; 
 
Procedure RSAEncrypt(P : String; exp, modb : TGInt; Var E : String); 
Procedure RSADecrypt(E : String; exp, modb : TGInt; Var D : String); 
 
 
Implementation 
 
 
{$H+} 
 
 
 
 
// Encrypt a string with the RSA algorithm, P^exp mod modb = E 
 
Procedure RSAEncrypt(P : String; exp, modb : TGInt; Var E : String); 
Var 
   i, j, modbits : longint; 
   PGInt, temp : TGInt; 
   tempstr1, tempstr2, tempstr3 : String; 
Begin 
   GInttobinstr(modb, tempstr1); 
   modbits := length(tempstr1); 
   convert8to1bit(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' Do delete(tempstr3, 1, 1); 
      binstrtoGInt(tempstr3, PGInt); 
      delete(tempstr1, 1, modbits - 1); 
      GIntModExp(PGInt, exp, modb, temp); 
      GIntDestroy(PGInt); 
      tempstr3 := ''; 
      GInttobinstr(temp, tempstr3); 
      While (length(tempstr3) Mod modbits) <> 0 Do tempstr3 := '0' + tempstr3; 
      tempstr2 := tempstr2 + tempstr3; 
      GIntdestroy(temp); 
   End; 
 
   While Not (tempstr2[1] = '1') Do delete(tempstr2, 1, 1); 
   Convert1to8bit(tempstr2, E); 
End; 
 
 
// Decrypt a string with the RSA algorithm, E^exp mod modb = D 
 
Procedure RSADecrypt(E : String; exp, modb : TGInt; Var D : String); 
Var 
   i, j, modbits : longint; 
   EGInt, temp : TGInt; 
   tempstr1, tempstr2, tempstr3 : String; 
Begin 
   GInttobinstr(modb, tempstr1); 
   modbits := length(tempstr1); 
   convert8to1bit(E, tempstr1); 
   While copy(tempstr1, 1, 1) = '0' Do delete(tempstr1, 1, 1); 
   While (length(tempstr1) Mod modbits) <> 0 Do tempstr1 := '0' + tempstr1; 
 
   j := length(tempstr1) Div modbits; 
   tempstr2 := ''; 
   For i := 1 To j Do 
   Begin 
      tempstr3 := copy(tempstr1, 1, modbits); 
      While copy(tempstr3, 1, 1) = '0' Do delete(tempstr3, 1, 1); 
      binstrtoGInt(tempstr3, EGInt); 
      delete(tempstr1, 1, modbits); 
      GIntModExp(EGInt, exp, modb, temp); 
      GIntDestroy(EGInt); 
      tempstr3 := ''; 
      GInttobinstr(temp, tempstr3); 
      While (length(tempstr3) Mod (modbits - 1)) <> 0 Do tempstr3 := '0' + tempstr3; 
      tempstr2 := tempstr2 + tempstr3; 
      GIntdestroy(temp); 
   End; 
 
   While Not (copy(tempstr2, 1, 3) = '111') Do delete(tempstr2, 1, 1); 
   delete(tempstr2, 1, 3); 
   Convert1to8bit(tempstr2, D); 
End; 
 
 
End.