Skip to content

Commit 1557c29

Browse files
committed
- Correção de erros na criptografia;
1 parent 553ed93 commit 1557c29

2 files changed

Lines changed: 205 additions & 173 deletions

File tree

src/utils/RALCripto.pas

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface
55

66
uses
77
Classes, SysUtils,
8-
RALTypes, RALStream;
8+
RALTypes, RALStream, RALBase64;
99

1010
type
1111

@@ -73,49 +73,75 @@ procedure TRALCripto.SetKey(const AValue: StringRAL);
7373

7474
function TRALCripto.Encrypt(const AValue: StringRAL): StringRAL;
7575
var
76-
vStream: TStream;
76+
vInputStream: TStringStream;
7777
begin
78-
vStream := StringToStream(AValue);
78+
vInputStream := nil;
7979
try
80-
Result := Encrypt(vStream);
80+
vInputStream := TStringStream.Create(AValue, TEncoding.UTF8);
81+
82+
vInputStream.Position := 0;
83+
84+
Result := Encrypt(vInputStream);
8185
finally
82-
FreeAndNil(vStream);
86+
FreeAndNil(vInputStream);
8387
end;
8488
end;
8589

8690
function TRALCripto.Decrypt(const AValue: StringRAL): StringRAL;
8791
var
88-
vStream: TStream;
92+
vInputStream: TStringStream;
8993
begin
90-
vStream := StringToStream(AValue);
94+
{ TODO -cCompatibilidade : Melhorar esse código pra compatibilizar com versões antigas do Delphi }
95+
vInputStream := nil;
9196
try
92-
Result := Decrypt(vStream);
97+
vInputStream := TStringStream.Create(TRALBase64.Decode(AValue), TEncoding.ANSI);
98+
99+
vInputStream.Position := 0;
100+
101+
Result := Decrypt(vInputStream);
93102
finally
94-
FreeAndNil(vStream);
103+
FreeAndNil(vInputStream);
95104
end;
96105
end;
97106

98107
function TRALCripto.Encrypt(AValue: TStream): StringRAL;
99108
var
100-
vResult: TStream;
109+
vEncryptedStream: TStream;
101110
begin
102-
vResult := EncryptAsStream(AValue);
111+
{ TODO -cCompatibilidade : Melhorar esse código pra compatibilizar com versões antigas do Delphi }
112+
vEncryptedStream := nil;
103113
try
104-
Result := StreamToString(vResult);
114+
vEncryptedStream := EncryptAsStream(AValue);
115+
116+
vEncryptedStream.Position := 0;
117+
118+
Result := TRALBase64.Encode(vEncryptedStream);
105119
finally
106-
FreeAndNil(vResult);
120+
FreeAndNil(vEncryptedStream);
107121
end;
108122
end;
109123

110124
function TRALCripto.Decrypt(AValue: TStream): StringRAL;
111125
var
112-
vResult: TStream;
126+
vDecryptedStream: TStream;
127+
vStringStream: TStringStream;
113128
begin
114-
vResult := DecryptAsStream(AValue);
129+
vDecryptedStream := nil;
130+
vStringStream := nil;
115131
try
116-
Result := StreamToString(vResult);
132+
vDecryptedStream := DecryptAsStream(AValue);
133+
134+
vStringStream := TStringStream.Create('', TEncoding.UTF8);
135+
136+
vDecryptedStream.Position := 0;
137+
138+
vStringStream.LoadFromStream(vDecryptedStream);
139+
140+
Result := vStringStream.DataString;
117141
finally
118-
FreeAndNil(vResult);
142+
FreeAndNil(vStringStream);
143+
144+
FreeAndNil(vDecryptedStream);
119145
end;
120146
end;
121147

0 commit comments

Comments
 (0)