Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Для новичков > Encryption... помогите нубу!!!


Автор: GreedyFly 7.9.2014, 15:44
День добрый!
Подскажите как с помощью этой Unit  шифровать, дешифровывать строки...
к примеру:
qwerty - с ключом (123456)

Код


unit Encrypt;

interface

uses
   SysUtils, WinProcs, Messages, Classes, Graphics, WinTypes, Controls,
   Forms, Dialogs;

type
   TActionType = (atEncryption, atDecryption);
   TEncryption = class(TComponent)
   private
      { Private declarations }
      FInputString:string;
      FOutputString:string;
      FHexInputString:string;
      FHexOutputString:string;
      FKeyString:string;
      FAction:TActionType;
      procedure SetInputString(input:string);
      procedure SetOutputString(input:string);
      procedure SetKeyString(input:string);
      Function EncryptionEngine (Src:String; Key : String;
   Encrypt : Boolean):String;
   protected
      { Protected declarations }
   public
      { Public declarations }
      constructor Create(AOwner: TComponent);override;
      Procedure Execute;
   published
      { Published declarations }
      property Input: String read FInputString write SetInputString;
      property Output: String read FOutputString write SetOutputString;
      property Key: String read FKeysTRING write SetKeyString;
      property Action: TActionType read FAction write FAction default atEncryption;
   end;

   procedure Register;

   implementation

   procedure Register; 
   begin
      RegisterComponents('Componentes', [TEncryption]);
   end;

   constructor TEncryption.Create(AOwner: TComponent);

   begin      
      inherited Create(AOwner);
      Action:=atEncryption;
   end;

   Procedure TEncryption.SetOutputString(input:string);
   begin
      if input<> FOutputString then
         FOutputString:=input;
   end;

   Procedure TEncryption.SetKeyString(input:string);
   begin
      if input<> FKeyString then
         FkeyString:=input;
   end;

   Procedure TEncryption.SetInputString(input:string);
   begin
      if input<> FInputString then
         FInputString:=input;
   end;

   Function TEncryption.EncryptionEngine (Src:String; Key : String; 
   Encrypt : Boolean):string;
   var
      idx       :Integer;
      KeyLen    :Integer;
      KeyPos    :Integer;
      offset    :Integer;
      dest      :string;
      SrcPos    :Integer;
      SrcAsc    :Integer;
      TmpSrcAsc :Integer;
      Range     :Integer;

   begin
      KeyLen:=Length(Key);
      if KeyLen = 0 then key:='Tom Lee';
      KeyPos:=0;
      SrcPos:=0;
      SrcAsc:=0;
      Range:=256;
      if Encrypt then
      begin
         Randomize;
         offset:=Random(Range);
         dest:=format('%1.2x',[offset]);
         for SrcPos:= 1 to Length(Src) do
         begin
            SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;
            if KeyPos < KeyLen then KeyPos:=KeyPos + 1 
            else KeyPos:= 1;
            SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);
            dest:= dest + format('%1.2x',[SrcAsc]);
            offset:= SrcAsc;
         end end else
         begin
            offset:=StrToInt('$'+copy(src,1,2));
            SrcPos:= 3;
            repeat
               SrcAsc:=StrToInt('$'+copy(src,SrcPos,2));
               if KeyPos < Keylen then KeyPos := KeyPos + 1 else Keypos := 1;
               TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
               if TmpSrcAsc <= offset then
                  TmpSrcAsc := 255 + TmpSrcAsc - offset
               else
                  TmpSrcAsc:= TmpSrcAsc - offset;
               dest := dest + chr(TmpSrcAsc);
               offset:=SrcAsc;
               SrcPos:=SrcPos  + 2;
            until SrcPos >= Length(Src);
         end;
         Result:=Dest;
      end;

      procedure TEncryption.Execute;
      var
         EncryptionFlag:Boolean;
      begin
         if length(FInputString)=0 then
         begin
            FOutputString:='';
            exit;
         end;

         if FAction = atEncryption then
            EncryptionFlag:= True
         else
            EncryptionFlag:= False;

         FOutputString:=EncryptionEngine(FInputString, FKeyString, 
         EncryptionFlag);
 
      end;
   end.

Автор: drkot 7.9.2014, 17:42
Код

with TEncryption.create(self) do begin
input:= 'qwerty';
key:= '123456';
action:= atEncryption;
execute;
showmessage(output);
end;

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)