Commit 9432bc1d661270d2e1141c2f7bb16b74fd2629f4

Authored by anderson.peterle@previdencia.gov.br
1 parent 97cbcee8
Exists in master

Melhoria no TestaCrypt e ajuste de código no CACIC_Library

git-svn-id: http://svn.softwarepublico.gov.br/svn/cacic/cacic/trunk/agente-windows@802 fecfc0c7-e812-0410-ae72-849f08638ee7
CACIC_Library.pas
... ... @@ -100,7 +100,7 @@ type
100 100 function isWindows9xME() : boolean;
101 101 function getWindowsStrId() : string;
102 102 function getWinDir() : string;
103   - function getHomeDrive : string;
  103 + function getHomeDrive() : string;
104 104 function isWindowsAdmin() : boolean;
105 105 function createSampleProcess(p_cmd: string; p_wait: boolean ) : boolean;
106 106 procedure showTrayIcon(p_visible:boolean);
... ... @@ -242,7 +242,7 @@ end;
242 242 {*------------------------------------------------------------------------------
243 243 Retorna a unidade de instalação do MS-Windows
244 244 -------------------------------------------------------------------------------}
245   -function TCACIC_Windows.getHomeDrive : string;
  245 +function TCACIC_Windows.getHomeDrive() : string;
246 246 begin
247 247 Result := MidStr(getWinDir,1,3); //x:\
248 248 end;
... ...
testacrypt/main_testacrypt.dfm
... ... @@ -90,16 +90,17 @@ object Form1: TForm1
90 90 object Label_FraseCriptografadaEnviadaEstacao: TLabel
91 91 Left = 8
92 92 Top = 110
93   - Width = 209
  93 + Width = 337
94 94 Height = 13
95   - Caption = 'Frase Criptografada (para envio ao servidor):'
  95 + Caption =
  96 + 'Frase Criptografada (para envio ao servidor ou testes de decript' +
  97 + 'ografia):'
96 98 Font.Charset = DEFAULT_CHARSET
97 99 Font.Color = clWindowText
98 100 Font.Height = -11
99 101 Font.Name = 'MS Sans Serif'
100 102 Font.Style = []
101 103 ParentFont = False
102   - Visible = False
103 104 end
104 105 object Label_IVStation: TLabel
105 106 Left = 8
... ... @@ -157,10 +158,9 @@ object Form1: TForm1
157 158 Font.Style = []
158 159 MaxLength = 100
159 160 ParentFont = False
160   - ReadOnly = True
161 161 TabOrder = 2
162   - Visible = False
163 162 OnChange = Edit_FraseCriptografadaEnviadaEstacaoChange
  163 + OnExit = Edit_FraseCriptografadaEnviadaEstacaoExit
164 164 end
165 165 object Edit_IVStation: TEdit
166 166 Left = 8
... ...
testacrypt/main_testacrypt.pas
... ... @@ -3,7 +3,14 @@ unit main_testacrypt;
3 3 interface
4 4  
5 5 uses
6   - Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  6 + Windows,
  7 + Messages,
  8 + SysUtils,
  9 + Variants,
  10 + Classes,
  11 + Graphics,
  12 + Controls,
  13 + Forms,
7 14 XML,
8 15 LibXmlParser,
9 16 IdHTTP,
... ... @@ -11,13 +18,14 @@ uses
11 18 IdComponent,
12 19 IdTCPConnection,
13 20 IdTCPClient,
14   - DCPcrypt2,
15   - DCPrijndael,
16   - DCPbase64,
17 21 StdCtrls,
18 22 WinSock,
19 23 NB30,
20   - ComCtrls, PJVersionInfo, JvExComCtrls, JvStatusBar;
  24 + ComCtrls,
  25 + PJVersionInfo,
  26 + JvExComCtrls,
  27 + JvStatusBar,
  28 + CACIC_Library;
21 29  
22 30 type
23 31 TForm1 = class(TForm)
... ... @@ -51,7 +59,6 @@ type
51 59 StatusBar_Mensagens: TJvStatusBar;
52 60 procedure Button_EfetuaTesteClick(Sender: TObject);
53 61 function PadWithZeros(const str : string; size : integer) : string;
54   - function EnCrypt(p_Data : String) : String;
55 62 procedure Button_FinalizaClick(Sender: TObject);
56 63 procedure Edit_FraseOriginalKeyUp(Sender: TObject; var Key: Word;
57 64 Shift: TShiftState);
... ... @@ -73,6 +80,7 @@ type
73 80 procedure Edit_ScriptPathChange(Sender: TObject);
74 81 procedure DesfazCriticas;
75 82 procedure Edit_IVStationChange(Sender: TObject);
  83 + procedure Edit_FraseCriptografadaEnviadaEstacaoExit(Sender: TObject);
76 84 private
77 85 { Private declarations }
78 86 public
... ... @@ -80,14 +88,10 @@ type
80 88 end;
81 89  
82 90 var Form1: TForm1;
83   - v_CipherKey,
84   - v_IV : String;
85 91 boolProcessaPausa : boolean;
86 92  
87   -// Some constants that are dependant on the cipher being used
88   -// Assuming MCRYPT_RIJNDAEL_128 (i.e., 128bit blocksize, 256bit keysize)
89   -const KeySize = 32; // 32 bytes = 256 bits
90   - BlockSize = 16; // 16 bytes = 128 bits
  93 +var
  94 + g_oCacic: TCACIC;
91 95  
92 96 implementation
93 97  
... ... @@ -95,7 +99,9 @@ implementation
95 99 procedure TForm1.CriptografaPalavra;
96 100 Begin
97 101 if (trim(form1.Edit_FraseOriginal.Text)<>'') then
98   - Form1.Edit_FraseCriptografadaEnviadaEstacao.Text := form1.EnCrypt(trim(form1.Edit_FraseOriginal.Text));
  102 + Form1.Edit_FraseCriptografadaEnviadaEstacao.Text := g_oCacic.enCrypt(trim(form1.Edit_FraseOriginal.Text))
  103 + else if (trim(form1.Edit_FraseCriptografadaEnviadaEstacao.Text)<>'') then
  104 + Form1.Edit_FraseOriginal.Text := g_oCacic.deCrypt(trim(form1.Edit_FraseCriptografadaEnviadaEstacao.Text));
99 105 End;
100 106  
101 107 procedure TForm1.Button_EfetuaTesteClick(Sender: TObject);
... ... @@ -109,18 +115,19 @@ var v_retorno,
109 115 intAux : integer;
110 116 begin
111 117  
  118 + boolProcessaPausa := true;
  119 +// InicializaCampos;
  120 + CriptografaPalavra;
  121 +
112 122 intAux := POS('255.255.255.255',Edit_ScriptPath.Text);
113 123 if (intAux > 0) then
114 124 Begin
115   - StatusBar_Mensagens.Panels[0].Text := 'ATENÇÃO: Informe um endereço válido para o teste';
  125 + StatusBar_Mensagens.Panels[0].Text := 'ATENÇÃO: Caso não seja um teste local, informe um endereço válido.';
116 126 StatusBar_Mensagens.Color := clYellow;
117 127 Edit_ScriptPath.SetFocus;
118 128 End
119 129 else
120 130 Begin
121   - boolProcessaPausa := true;
122   - InicializaCampos;
123   - CriptografaPalavra;
124 131  
125 132 Request_Config := TStringList.Create;
126 133 Request_Config.Values['cs_operacao'] := 'TestaCrypt';
... ... @@ -163,7 +170,7 @@ begin
163 170 Begin
164 171 Form1.StatusBar_Mensagens.Panels[0].Text := 'Problemas na comunicação...';
165 172 Sleep(1000);
166   - Form1.StatusBar_Mensagens.Panels[0].Text := '';
  173 + Form1.StatusBar_Mensagens.Panels[0].Text := '';
167 174 End;
168 175 End;
169 176 Request_Config.Free;
... ... @@ -228,47 +235,6 @@ begin
228 235 end;
229 236 end;
230 237  
231   -// Encrypt a string and return the Base64 encoded result
232   -function TForm1.EnCrypt(p_Data : String) : String;
233   -var
234   - l_Cipher : TDCP_rijndael;
235   - l_Data, l_Key, l_IV : string;
236   -begin
237   - Form1.StatusBar_Mensagens.Panels[0].Text := 'Criptografando "'+p_Data+'"';
238   -
239   - if boolProcessaPausa then
240   - Begin
241   - boolProcessaPausa := false;
242   - Sleep(1000);
243   - End;
244   - Form1.StatusBar_Mensagens.Panels[0].Text := '';
245   - Try
246   - // Pad Key, IV and Data with zeros as appropriate
247   - l_Key := form1.PadWithZeros(trim(form1.Edit_CipherKeyStation.Text),KeySize);
248   - l_IV := form1.PadWithZeros(trim(form1.Edit_IVStation.Text),BlockSize);
249   - l_Data := form1.PadWithZeros(p_Data,BlockSize);
250   -
251   - // Create the cipher and initialise according to the key length
252   - l_Cipher := TDCP_rijndael.Create(nil);
253   - if Length(trim(form1.Edit_CipherKeyStation.Text)) <= 16 then
254   - l_Cipher.Init(l_Key[1],128,@l_IV[1])
255   - else if Length(trim(form1.Edit_CipherKeyStation.Text)) <= 24 then
256   - l_Cipher.Init(l_Key[1],192,@l_IV[1])
257   - else
258   - l_Cipher.Init(l_Key[1],256,@l_IV[1]);
259   -
260   - // Encrypt the data
261   - l_Cipher.EncryptCBC(l_Data[1],l_Data[1],Length(l_Data));
262   -
263   - // Free the cipher and clear sensitive information
264   - l_Cipher.Free;
265   - FillChar(l_Key[1],Length(l_Key),0);
266   -
267   - // Return the Base64 encoded result
268   - Result := Base64EncodeStr(l_Data);
269   - Except
270   - End;
271   -end;
272 238  
273 239  
274 240 procedure TForm1.Button_FinalizaClick(Sender: TObject);
... ... @@ -303,12 +269,12 @@ end;
303 269  
304 270 procedure TForm1.FormCreate(Sender: TObject);
305 271 begin
306   - //chave AES. Recomenda-se que cada empresa/órgão altere a sua chave.
307   - v_CipherKey := 'CacicBrasil';
308   - v_IV := 'abcdefghijklmnop';
  272 + g_oCacic := TCACIC.Create;
  273 + g_oCacic.setBoolCipher(true);
  274 +
  275 + form1.Edit_IVStation.Text := g_oCacic.getIV;
  276 + form1.Edit_CipherKeyStation.Text := g_oCacic.getCipherKey;
309 277  
310   - form1.Edit_IVStation.Text := v_IV;
311   - form1.Edit_CipherKeyStation.Text := v_CipherKey;
312 278 Form1.StatusBar_Mensagens.Panels[1].Text := 'v: '+getVersionInfo(ParamStr(0));
313 279 boolProcessaPausa := false;
314 280 end;
... ... @@ -329,15 +295,7 @@ procedure TForm1.Edit_FraseCriptografadaEnviadaEstacaoChange(
329 295 Sender: TObject);
330 296 begin
331 297 if trim(form1.Edit_FraseCriptografadaEnviadaEstacao.Text) = '' then
332   - Begin
333   - form1.Edit_FraseCriptografadaEnviadaEstacao.Visible := false;
334   - form1.Label_FraseCriptografadaEnviadaEstacao.Visible := false;
335   - End
336   - else
337   - Begin
338   - form1.Edit_FraseCriptografadaEnviadaEstacao.Visible := true;
339   - form1.Label_FraseCriptografadaEnviadaEstacao.Visible := true;
340   - End;
  298 + form1.Button_EfetuaTeste.Enabled := true;
341 299 ProcessaPausa;
342 300 end;
343 301  
... ... @@ -439,4 +397,14 @@ begin
439 397 DesfazCriticas;
440 398 end;
441 399  
  400 +procedure TForm1.Edit_FraseCriptografadaEnviadaEstacaoExit(
  401 + Sender: TObject);
  402 +begin
  403 + if (form1.Edit_FraseCriptografadaEnviadaEstacao.Text <> '') then
  404 + Begin
  405 + form1.Button_EfetuaTeste.Enabled := true;
  406 + End;
  407 +
  408 +end;
  409 +
442 410 end.
... ...