Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2007.07.29;
Скачать: CL | DM;

Вниз

Использование без TMemoryStream   Найти похожие ветки 

 
Ivolg ©   (2007-07-03 23:46) [0]

Имеется алгоритм шифрования:

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
  TTriple = Array[0..2] of Word;

type
 TForm1 = class(TForm)
   Button1: TButton;
   Button2: TButton;
   Memo1: TMemo;
   Memo2: TMemo;
   procedure Button1Click(Sender: TObject);
   function FileCrypt(InFile, OutFile: String; Key: TTriple; Encrypt: Boolean): boolean;
   function XorEncrypt(Inf: Pointer; InfSize: Cardinal; Outf: Pointer; OutfSize: Cardinal; Key: TTriple): Boolean;
   function XorDecrypt(Inf: Pointer; InfSize: Cardinal; Outf: Pointer; OutfSize: Cardinal; Key: TTriple): Boolean;
   procedure Button2Click(Sender: TObject);
   procedure FormCreate(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;
 dtKey: TTriple;
 iPass: Integer;
 hFile: Integer;

implementation

{$R *.dfm}

function TForm1.XorDecrypt(Inf: Pointer; InfSize: Cardinal; Outf: Pointer; OutfSize: Cardinal; Key: TTriple): Boolean;
var
 pIn, pOut:   ^byte;
 i: integer;
begin
 if InfSize=InfSize then
   begin
     pIn:=Inf;
     pOut:=Outf;
       for i := 1 to InfSize do
         begin
           Application.ProcessMessages;
           pOut^ := pIn^ xor (Key[2] shr 8);
           Key[2] := byte(pOut^ + Key[2]) * Key[0] + Key[1];
           inc(pIn);
           inc(pOut);
         end;
     Result := True;
   end else
     Result := False;
end;

function TForm1.XorEncrypt(Inf: Pointer; InfSize: Cardinal; Outf: Pointer; OutfSize: Cardinal; Key: TTriple): Boolean;
var
 pIn, pOut:   ^byte;
 I: Integer;
begin
 if InfSize=OutfSize then
   begin
     pIn:=Inf;
     pOut:=Outf;
       for i := 1 to InfSize do
         begin
           Application.ProcessMessages;
           pOut^:=pIn^ xor (Key[2] shr 8);
           Key[2] := Byte(pIn^ + Key[2]) * Key[0] + Key[1];
           inc(pIn);
           inc(pOut);
         end;
       Result := True;
     end else
       Result := False;
end;

function TForm1.FileCrypt(InFile, OutFile: String; Key: TTriple; Encrypt: Boolean): boolean;
var
   MIn, MOut: TMemoryStream;
begin
   MIn:=TMemoryStream.Create;
   MOut:=TMemoryStream.Create;
 try
   MIn.LoadFromFile(InFile);
   MOut.SetSize(MIn.Size);
   if Encrypt then
     Result := XorEncrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key)
   else
     Result := XorDecrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key);
   MOut.SaveToFile(OutFile);
  finally
   MOut.Free;
   MIn.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   dtKey[0] := abs(iPass) div 100;
   dtKey[1] := abs(iPass) XOR 100;
   dtKey[2] := abs(iPass) AND 100;
   FileCrypt("C:\help.txt" ,"C:\help.txt"+".x", dtKey, True);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    dtKey[0] := abs(iPass) div 100;
    dtKey[1] := abs(iPass) XOR 100;
    dtKey[2] := abs(iPass) AND 100;
    FileCrypt("C:\help.txt.x" ,"C:\help.txt"+".orig", dtKey, False);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     iPass:=12345;
end;

end.



Как можно его применять без использования потока TMemoryStream ? Например, текст, который находится в Memo1 перекодировать и вывести в Memo2 ? Спасибо !


 
Германн ©   (2007-07-04 01:08) [1]


> Как можно его применять без использования потока TMemoryStream
> ?

Молча.
Раньше просили пример. А теперь уже и пример не помогает. :(


 
Плохиш ©   (2007-07-04 01:17) [2]


> Раньше просили пример. А теперь уже и пример не помогает.

Прогресс, однака...


 
Kostafey ©   (2007-07-04 03:45) [3]

> Как можно его применять без использования потока TMemoryStream
> ?

А так чем плохо ?

В примере потоки исползуются для чтения/записи файла.
Вместо этого можно например использовать тип File.

А вообще, можно и из потока выводить данные в Memo:

procedure TForm1.Button1Click(Sender: TObject);
var
 Stream: TMemoryStream;
begin
 Stream := TMemoryStream.Create;
 Stream.LoadFromFile("e:\Temp\somefile.lua");
 Memo1.Lines.LoadFromStream(Stream);
end;


 
{RASkov} ©   (2007-07-04 04:15) [4]

> [0] Ivolg ©   (03.07.07 23:46)

> function TForm1.XorDecrypt(Inf: Pointer; InfSize: Cardinal; Outf: Pointer; OutfSize: Cardinal; Key: TTriple): Boolean;
> var
> pIn, pOut:   ^byte;
> i: integer;
> begin
> if InfSize=InfSize then

Выделенное - оно так и надо, да?
И чем, кроме этого(выделенного), отличаются XorEncrypt и XorDecrypt? Т.е. какой смысл делать однин и тот же алгоритм двумя процедурами с разными именами...
В итоге... тебе нужна только одна процедура (XorDecrypt или XorEncrypt) и изменить реализацию FileCrypt(вместе с названием(именем)) с учетом [3]
или переписать XorDecrypt и XorEncrypt для работы со строками...
Например:
function XorEncode(const Key, Source: string): string;
var
 I: Integer;
 C: Byte;
begin
 Result := "";
 for I := 1 to Length(Source) do begin
   if Length(Key) > 0 then
     C := Byte(Key[1 + ((I - 1) mod Length(Key))]) xor Byte(Source[I])
   else
     C := Byte(Source[I]);
   Result := Result + AnsiLowerCase(IntToHex(C, 2));
 end;
end;

function XorDecode(const Key, Source: string): string;
var
 I: Integer;
 C: Char;
begin
 Result := "";
 for I := 0 to Length(Source) div 2 - 1 do begin
   C := Chr(StrToIntDef("$" + Copy(Source, (I * 2) + 1, 2), Ord(" ")));
   if Length(Key) > 0 then
     C := Chr(Byte(Key[1 + (I mod Length(Key))]) xor Byte(C));
   Result := Result + C;
 end;
end;

© RxStrUtils.pas


 
{RASkov} ©   (2007-07-04 04:23) [5]

> И чем, кроме этого(выделенного), отличаются XorEncrypt и XorDecrypt?

Ай... нашел одно отличие :)

> function TForm1.XorDecrypt();
....
begin
....
   Key[2] := byte(pOut^ + Key[2]) * Key[0] + Key[1];
....
end;
и
> function TForm1.XorEncrypt()
....
begin
....
   Key[2] := Byte(pIn^ + Key[2]) * Key[0] + Key[1];
....
end;

:)



Страницы: 1 вся ветка

Текущий архив: 2007.07.29;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.015 c
15-1183143709
ferr
2007-06-29 23:01
2007.07.29
Что требуется для поездки в Украину?


2-1183383561
авыф
2007-07-02 17:39
2007.07.29
MS SQL 2005


15-1182947588
TruePunk
2007-06-27 16:33
2007.07.29
wap ресурсы


15-1183102300
Nucer
2007-06-29 11:31
2007.07.29
Помогите настроить WIPFW


2-1183697767
delphino
2007-07-06 08:56
2007.07.29
Не могу найти ошибку в выражении