-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.pas
More file actions
163 lines (133 loc) · 3.82 KB
/
encryption.pas
File metadata and controls
163 lines (133 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
unit Encryption;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Dialogs,
DCPrc4, DCPsha512, Base64,
AppDb;
Type
{ TEncryptDecrypt }
TEncryptDecrypt = Class(TAppDatabase)
private
FEncryDecry, FSalt : String;
function Encode_String_Base64(Text : String) : String;
function Decode_String_Base64(Text : String) : String;
public
constructor Create(aText : String); overload;
destructor Destroy; override;
function GenerateSalt: String;
function Encrypt_String(Text, aSalt : String) : String;
function Decrypt_String(Text, aSalt : String) : String;
function HashString(aString, aSalt : String) : String;
function VerifyPassword(pwdPlain, pwdEncrypted, aSalt: String) : Boolean;
Property Salt : String read FSalt write FSalt;
end;
const
USER_NAMETXT = '-!Quercus7Salvia10_';
implementation
{ TEncryptDecrypt }
function TEncryptDecrypt.Encode_String_Base64(Text: String): String;
begin
Result := EncodeStringBase64(Text);
end;
function TEncryptDecrypt.Decode_String_Base64(Text: String): String;
begin
Result := DecodeStringBase64(Text);
end;
constructor TEncryptDecrypt.Create(aText : String);
const
_ADD = 'API_manager';
begin
// inherited;
FEncryDecry := aText + USER_NAMETXT+ _ADD;
end;
destructor TEncryptDecrypt.Destroy;
begin
inherited Destroy;
end;
function TEncryptDecrypt.GenerateSalt: String;
var
aSalt : String;
begin
aSalt := TGUID.NewGuid.ToString();
aSalt := StringReplace(aSalt, '{', '', [rfReplaceAll]);
aSalt := StringReplace(aSalt, '}', '', [rfReplaceAll]);
aSalt := StringReplace(aSalt, '-', '', [rfReplaceAll]);
Result := aSalt;
end;
function TEncryptDecrypt.Encrypt_String(Text, aSalt: String): String;
var
Cipher : TDCP_rc4;
begin
Result := '';
Cipher:= TDCP_rc4.Create(nil);
Cipher.InitStr(copy(aSalt, 0, 5)+ FEncryDecry, TDCP_sha512);
Result := Cipher.EncryptString(Text);
Result := Encode_String_Base64(Result);;
Cipher.Burn;
Cipher.Free;
end;
function TEncryptDecrypt.Decrypt_String(Text, aSalt: String): String;
var
Cipher : TDCP_rc4;
begin
Cipher:= TDCP_rc4.Create(nil);
Cipher.InitStr(copy(aSalt, 0, 5)+ FEncryDecry, TDCP_sha512);
if Text = '' then // Empty value is not allowed
Result := ''
else begin
Result := Decode_String_Base64(Text);
Result := Cipher.DecryptString(Result);
end;
Cipher.Burn;
Cipher.Free;
end;
function TEncryptDecrypt.HashString(aString, aSalt: String): String;
var
Hash : TDCP_sha512;
Digest: array[0..64] of byte; // sha256 produces a 256bit digest (32bytes) 256/8=32
i: integer;
HashedString: string;
begin
//Digest[0] := 0;
if (aString <> '') and (salt <> '') then begin
Hash:= TDCP_sha512.Create(nil); // create the hash
Hash.Init; // initialize it
Hash.UpdateStr(salt+aString);
Hash.Final(Digest); // produce the digest
Hash.Free;
HashedString:= '';
for i:= 0 to 50 do begin
HashedString := HashedString + IntToHex(Digest[i],2);
end;
aString := HashedString;
Result := aString;
end
else
Result := '';
end;
function TEncryptDecrypt.VerifyPassword(pwdPlain, pwdEncrypted, aSalt: String): Boolean;
var
computedPwdHash : String;
begin
if (salt <> '') and
(pwdEncrypted <> '') and
(pwdPlain <> '') then
begin
computedPwdHash := HashString(pwdPlain, Salt);
if (computedPwdHash = pwdEncrypted) then
begin
Result := True;
end
else
begin
Result := False;
end;
end
else
begin
MessageDlg('Fout', 'Wachtwoord of Salt bevat geen waarde.', mtError, [mbOK],0);
Result := False;
end;
end;
end.