Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi String mit Salsa20 verschlüsseln (https://www.delphipraxis.net/132104-string-mit-salsa20-verschluesseln.html)

cookie22 6. Apr 2009 10:50


String mit Salsa20 verschlüsseln
 
hallo,

ich möchte mit salsa20 strings verschlüsseln. leider klappt ds nicht so wie ich mir das vorstelle. ich poste mal den salsa code.

Delphi-Quellcode:
unit salsa20;

{Salsa20 stream cipher routines}


interface

{$i STD.INC}

uses
  BTypes;

const
  salsa_blocklength = 64;                 {Block length in bytes}

type
  TSalsaBlk = array[0..15] of longint;

  {Structure containing the context of salsa20}
  salsa_ctx  = packed record
                  input : TSalsaBlk;      {internal state}
                  rounds: word;           {number of rounds}
                  kbits : word;           {number of key bits}
                end;

procedure salsa_keysetup(var ctx: salsa_ctx; key: pointer);
  {-Key setup, 128 bits of key^ are used, default rounds=12. It is the user's}
  { responsibility to supply a pointer to at least 128 accessible key bits!}

procedure salsa_keysetup256(var ctx: salsa_ctx; key: pointer);
  {-Key setup, 256 bits of key^ are used, default rounds=20 It is the user's}
  { responsibility to supply a pointer to at least 256 accessible key bits!}

procedure salsa_xkeysetup(var ctx: salsa_ctx; key: pointer; keybits, rounds: word);
  {-Key setup, 128 bits of key^ are used if keybits<>256.. It is the user's }
  { responsibility to supply a pointer to at least 128 (resp 256) accessible}
  { key bits. If rounds not in [8,12,20], then rounds=20 will be used.}

procedure salsa_ivsetup(var ctx: salsa_ctx; IV: pointer);
  {-IV setup, 64 bits of IV^ are used. It is the user's responsibility to }
  { supply least 64 accessible IV bits. After having called salsa_keysetup,}
  { the user is allowed to call salsa_ivsetup different times in order to }
  { encrypt/decrypt different messages with the same key but different IV's}



procedure salsa_encrypt_bytes(var ctx: salsa_ctx; ptp, ctp: pointer; msglen: longint);
  {-Bytewise encryption, msglen: message length in bytes}

procedure salsa_encrypt_blocks(var ctx: salsa_ctx; ptp, ctp: pointer; blocks: word);
  {-Blockwise encrypt plainttext to ciphertext, blocks: length in 64 byte blocks}

procedure salsa_encrypt_packet(var ctx: salsa_ctx; IV, ptp, ctp: pointer; msglen: longint);
  {-All-in-one encryption of (short) packets, msglen: message length in bytes}
  { It is the user's responsibility to supply least 64 accessible IV bits.}



procedure salsa_decrypt_bytes(var ctx: salsa_ctx; ctp, ptp: pointer; msglen: longint);
  {-Bytewise decryption, msglen: message length in bytes}

procedure salsa_decrypt_blocks(var ctx: salsa_ctx; ctp, ptp: pointer; blocks: word);
  {-Blockwise decryption, blocks: length in 64 byte blocks}

procedure salsa_decrypt_packet(var ctx: salsa_ctx; IV, ctp, ptp: pointer; msglen: longint);
  {-All-in-one encryption of (short) packets, msglen: message length in bytes}
  { It is the user's responsibility to supply least 64 accessible IV bits.}



procedure salsa_keystream_bytes(var ctx: salsa_ctx; keystream: pointer; kslen: longint);
  {-Generate keystream, kslen: keystream length in bytes}

procedure salsa_keystream_blocks(var ctx: salsa_ctx; keystream: pointer; blocks: word);
  {-Generate keystream, blocks: keystream length in 64 byte blocks}



function salsa_Selftest: boolean;
  {-Simple self-test of Salsa20, tests 128/256 key bits and 8/12/20 rounds}
so hab ich das versucht:
Delphi-Quellcode:
procedure TForm1.Button5Click(Sender: TObject);
var
  IV, key: array[0..31] of byte;
  Text, Text2, Text3: string;
  ctx: salsa_ctx;
begin
  Text := 'Test';
  Text2 := '';
  fillchar(IV, sizeof(IV), 0);
  fillchar(key, sizeof(key), 0);
  key[0] := $80;
  salsa_xkeysetup(ctx, @key, 256, 20);
  salsa_ivsetup(ctx, @IV);
  salsa_encrypt_bytes(ctx, @Text, @Text2,length(Text));
  salsa_decrypt_bytes(ctx, @Text2, @Text3, length(Text2));
  showmessage(Text3);
end;
leider verabschiedet sich das ganze beim entschlüsseln mit ner schutzverletzung. sieht jemand was ich da falsch mache?

mfg,
cookie

DeddyH 6. Apr 2009 11:00

Re: String mit Salsa20 verschlüsseln
 
Müsste es nicht
Delphi-Quellcode:
salsa_encrypt_bytes(ctx, @Text[1], @Text2[1],length(Text));
usw. heißen?

cookie22 6. Apr 2009 11:13

Re: String mit Salsa20 verschlüsseln
 
das hatte ich auch gedacht, aber dann gibs schon beim verschlüsseln ne schutzverletzung.

himitsu 6. Apr 2009 11:50

Re: String mit Salsa20 verschlüsseln
 
du MUßT auch erstmal für Text2 und Text3 den Speicher reservieren, bevor die Funktionen da war reinschreiben können! :warn:


[add]
Delphi-Quellcode:
...
salsa_ivsetup(ctx, @IV);
SetLength(Text2, Length(Text));
salsa_encrypt_bytes(ctx, PChar(Text), PChar(Text2), length(Text));
SetLength(Text3, Length(Text2));
salsa_decrypt_bytes(ctx, PChar(Text2), PChar(Text3), length(Text2));
showmessage(Text3);

// oder

...
salsa_ivsetup(ctx, @IV);
SetLength(Text2, Length(Text));
if Text2 <> '' then salsa_encrypt_bytes(ctx, @Text[1], @Text2[1], length(Text));
SetLength(Text3, Length(Text2));
if Text3 <> '' then salsa_decrypt_bytes(ctx, @Text2[1], @Text3[1], length(Text2));
showmessage(Text3);
vorrausgesetzt die verschlüsselte und entschlüsselte Textlänge ist gleich :!:


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:27 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz