Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to encrypt and decrypt files or strings Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
158
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to encrypt and decrypt files or strings

Answer:

Here's a simple yet effective encryption function:

1   unit EZCrypt;
2   
3   {modeled by Ben Hochstrasser(bhoc@surfeu.ch) after some code snippet from Borland}
4   
5   interface
6   
7   uses
8     Windows, Classes;
9   
10  type
11    TWordTriple = array[0..2] of Word;
12  
13  function FileEncrypt(InFile, OutFile: string; Key: TWordTriple): boolean;
14  function FileDecrypt(InFile, OutFile: string; Key: TWordTriple): boolean;
15  function TextEncrypt(const s: string; Key: TWordTriple): string;
16  function TextDecrypt(const s: string; Key: TWordTriple): string;
17  function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer;
18    TargetSize: Cardinal; Key: TWordTriple): boolean;
19  function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer;
20    TargetSize: Cardinal; Key: TWordTriple): boolean;
21  
22  implementation
23  
24  function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer;
25    TargetSize: Cardinal; Key: TWordTriple): boolean;
26  var
27    pIn, pOut: ^byte;
28    i: Cardinal;
29  begin
30    if SrcSize = TargetSize then
31    begin
32      pIn := Src;
33      pOut := Target;
34      for i := 1 to SrcSize do
35      begin
36        pOut^ := pIn^ xor (Key[2] shr 8);
37        Key[2] := Byte(pIn^ + Key[2]) * Key[0] + Key[1];
38        inc(pIn);
39        inc(pOut);
40      end;
41      Result := True;
42    end
43    else
44      Result := False;
45  end;
46  
47  function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer;
48    TargetSize: Cardinal; Key: TWordTriple): boolean;
49  var
50    pIn, pOut: ^byte;
51    i: Cardinal;
52  begin
53    if SrcSize = TargetSize then
54    begin
55      pIn := Src;
56      pOut := Target;
57      for i := 1 to SrcSize do
58      begin
59        pOut^ := pIn^ xor (Key[2] shr 8);
60        Key[2] := byte(pOut^ + Key[2]) * Key[0] + Key[1];
61        inc(pIn);
62        inc(pOut);
63      end;
64      Result := True;
65    end
66    else
67      Result := False;
68  end;
69  
70  function TextCrypt(const s: string; Key: TWordTriple; Encrypt: Boolean): string;
71  var
72    bOK: Boolean;
73  begin
74    SetLength(Result, Length(s));
75    if Encrypt then
76      bOK := MemoryEncrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key)
77    else
78      bOK := MemoryDecrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key);
79    if not bOK then
80      Result := '';
81  end;
82  
83  function FileCrypt(InFile, OutFile: string; Key: TWordTriple; Encrypt: Boolean):
84    boolean;
85  var
86    MIn, MOut: TMemoryStream;
87  begin
88    MIn := TMemoryStream.Create;
89    MOut := TMemoryStream.Create;
90    try
91      MIn.LoadFromFile(InFile);
92      MOut.SetSize(MIn.Size);
93      if Encrypt then
94        Result := MemoryEncrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key)
95      else
96        Result := MemoryDecrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key);
97      MOut.SaveToFile(OutFile);
98    finally
99      MOut.Free;
100     MIn.Free;
101   end;
102 end;
103 
104 function TextEncrypt(const s: string; Key: TWordTriple): string;
105 begin
106   Result := TextCrypt(s, Key, True);
107 end;
108 
109 function TextDecrypt(const s: string; Key: TWordTriple): string;
110 begin
111   Result := TextCrypt(s, Key, False);
112 end;
113 
114 function FileEncrypt(InFile, OutFile: string; Key: TWordTriple): boolean;
115 begin
116   Result := FileCrypt(InFile, OutFile, Key, True);
117 end;
118 
119 function FileDecrypt(InFile, OutFile: string; Key: TWordTriple): boolean;
120 begin
121   Result := FileCrypt(InFile, OutFile, Key, False);
122 end;
123 
124 end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC