Einzelnen Beitrag anzeigen

Benutzerbild von cookie22
cookie22

Registriert seit: 28. Jun 2006
Ort: Düsseldorf
936 Beiträge
 
Delphi XE2 Professional
 
#1

String mit Salsa20 verschlüsseln

  Alt 6. Apr 2009, 10:50
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
  Mit Zitat antworten Zitat