-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cipher.pas
214 lines (181 loc) · 6.45 KB
/
Cipher.pas
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
unit Cipher;
interface
{Сначала реализация, потом рефакторинг. Шифрование будет каким-нибудь интерфейсом описано, а это уедет в реализацию}
uses System.SysUtils, System.Classes, CMLTypes, DCPcrypt2, DCPblockciphers, DCPrijndael, DCPSha1, DCPbase64;
const
CIPHER_OK = 0;
CIPHER_IO_ERROR = 1;
CIPHER_WRONG_PASSWORD = 2;
CIPHER_CONTROL_GUID = '2b580ce6-e72f-433d-9788-3ecb6b0d9580';
type
TFileCipher = class
private
password: WideString;
fileCipher, filenameCipher: TDCP_rijndael; //AES ciphers
DoFilenameCipher: boolean; //шифровать имена файлов
PasswordWrong: boolean;
procedure CiphersInit();
procedure CiphersDestroy();
protected
public
property WrongPassword: boolean read PasswordWrong;
{filePassword: пароль для шифрования\дешифрации файлов
filePasswordControl: контрольный код для проверки файлового пароля (только дешифрация), пустой - не проверять
}
constructor Create(password: WideString; PasswordControl: WideString = ''; DoFilenameCipher: boolean = false);
destructor Destroy; override;
function CryptFile(SourceFileName, DestinationFilename: WideString): integer;
function CryptStream(SourceStream, DestinationStream: TStream): integer;
function CryptFileName(const FileName: WideString): WideString;
function DecryptFile(SourceFileName, DestinationFilename: WideString): integer;
function DecryptStream(SourceStream, DestinationStream: TStream): integer;
function DecryptFileName(const FileName: WideString): WideString;
procedure DecryptDirListing(var CloudMailRuDirListing: TCloudMailRuDirListing);
class function Base64ToSafe(const Base64: WideString): WideString; //converts Base64-encoded string to URL and Filename safe (RFC 4648)
class function Base64FromSafe(const Safe: WideString): WideString;
class function CryptedGUID(const password: WideString): WideString;
end;
implementation
{TFileCipher}
class function TFileCipher.Base64FromSafe(const Safe: WideString): WideString;
begin
Result := Safe;
Result := StringReplace(Result, '-', '+', [rfReplaceAll]);
Result := StringReplace(Result, '_', '/', [rfReplaceAll]);
end;
class function TFileCipher.Base64ToSafe(const Base64: WideString): WideString;
begin
Result := Base64;
Result := StringReplace(Result, '+', '-', [rfReplaceAll]);
Result := StringReplace(Result, '/', '_', [rfReplaceAll]);
end;
class function TFileCipher.CryptedGUID(const password: WideString): WideString;
var
tmpCipher: TDCP_rijndael;
begin
tmpCipher := TDCP_rijndael.Create(nil);
tmpCipher.InitStr(password, TDCP_sha1);
Result := tmpCipher.EncryptString(CIPHER_CONTROL_GUID);
tmpCipher.Burn;
tmpCipher.Destroy;
end;
procedure TFileCipher.CiphersDestroy;
begin
self.fileCipher.Burn;
self.fileCipher.Destroy;
if self.DoFilenameCipher then
begin
self.filenameCipher.Burn;
self.filenameCipher.Destroy;
end;
end;
procedure TFileCipher.CiphersInit;
begin
self.fileCipher := TDCP_rijndael.Create(nil);
self.fileCipher.InitStr(self.password, TDCP_sha1);
if self.DoFilenameCipher then
begin
self.filenameCipher := TDCP_rijndael.Create(nil);
self.filenameCipher.InitStr(self.password, TDCP_sha1);
end;
end;
constructor TFileCipher.Create(password: WideString; PasswordControl: WideString = ''; DoFilenameCipher: boolean = false);
begin
self.password := password;
self.DoFilenameCipher := DoFilenameCipher;
self.CiphersInit();
if EmptyWideStr <> PasswordControl then
PasswordWrong := not(self.fileCipher.EncryptString(CIPHER_CONTROL_GUID) = PasswordControl); //признак неверного пароля
self.CiphersDestroy;
end;
function TFileCipher.CryptFile(SourceFileName, DestinationFilename: WideString): integer;
var
SourceStream, DestinationStream: TFileStream;
begin
Result := CIPHER_OK;
self.CiphersInit();
try
SourceStream := TFileStream.Create(SourceFileName, fmOpenRead or fmShareDenyWrite);
DestinationStream := TFileStream.Create(DestinationFilename, fmCreate);
if SourceStream.Size > 0 then
self.CryptStream(SourceStream, DestinationStream);
SourceStream.Free;
DestinationStream.Free;
except
self.CiphersDestroy();
Result := CIPHER_IO_ERROR;
end;
self.CiphersDestroy();
end;
function TFileCipher.CryptFileName(const FileName: WideString): WideString;
begin
self.CiphersInit();
Result := ExtractFileName(FileName);
if EmptyWideStr = Result then
exit;
if DoFilenameCipher then
Result := Base64ToSafe(self.filenameCipher.EncryptString(Result));
self.CiphersDestroy;
end;
function TFileCipher.CryptStream(SourceStream, DestinationStream: TStream): integer;
begin
self.CiphersInit();
Result := 0;
if SourceStream.Size > 0 then
Result := self.fileCipher.EncryptStream(SourceStream, DestinationStream, SourceStream.Size);
self.CiphersDestroy;
end;
procedure TFileCipher.DecryptDirListing(var CloudMailRuDirListing: TCloudMailRuDirListing);
var
i: integer;
begin
for i := 0 to Length(CloudMailRuDirListing) - 1 do
begin
CloudMailRuDirListing[i].visible_name := self.DecryptFileName(CloudMailRuDirListing[i].name);
end;
end;
function TFileCipher.DecryptFile(SourceFileName, DestinationFilename: WideString): integer;
var
SourceStream, DestinationStream: TFileStream;
begin
self.CiphersInit();
Result := CIPHER_OK;
try
SourceStream := TFileStream.Create(SourceFileName, fmOpenRead or fmShareDenyWrite);
DestinationStream := TFileStream.Create(DestinationFilename, fmCreate);
if SourceStream.Size > 0 then
self.DecryptStream(SourceStream, DestinationStream);
SourceStream.Free;
DestinationStream.Free;
except
self.CiphersDestroy;
Result := CIPHER_IO_ERROR;
end;
self.CiphersDestroy;
end;
function TFileCipher.DecryptFileName(const FileName: WideString): WideString;
begin
self.CiphersInit();
Result := ExtractFileName(FileName);
if EmptyWideStr = Result then
exit;
if DoFilenameCipher then
Result := self.filenameCipher.DecryptString(Base64FromSafe(FileName));
self.CiphersDestroy();
end;
function TFileCipher.DecryptStream(SourceStream, DestinationStream: TStream): integer;
begin
self.CiphersInit();
Result := 0;
if SourceStream.Size > 0 then
Result := self.fileCipher.DecryptStream(SourceStream, DestinationStream, SourceStream.Size);
self.CiphersDestroy();
end;
destructor TFileCipher.Destroy;
begin
{self.fileCipher.Destroy;
if Assigned(self.filenameCipher) then
self.filenameCipher.Destroy;}
inherited;
end;
end.