Показать сообщение отдельно
  #10  
Старый 08.12.2010, 17:23
mirt steelwater mirt steelwater вне форума
Прохожий
 
Регистрация: 17.10.2010
Сообщения: 35
Репутация: 10
Восклицание

ок, посмотрел исходники поискал более надежный алгоритм блочного шифрования, остановился на ANUBIS:
http://ru.wikipedia.org/wiki/Anubis
нашел исходники на С от автора:
http://www.larc.usp.br/~pbarreto/AnubisPage.html
нашел реализацию на Дельфи от какго-то немецкого профессора:
http://home.netsurf.de/wolfgang.ehrhardt/crypt_en.html
в итоге написал такое вот:
Код:
unit Anubis;

interface

uses
    Windows, SysUtils, Classes,
    btypes, anu_base,
    anu_ctr, anu_cbc;

{$ALIGN ON}

type
    TAnubisByteKey128 = array [0..15] of Byte;
    TAnubisByteKey160 = array [0..19] of Byte;
    TAnubisByteKey192 = array [0..23] of Byte;
    TAnubisByteKey256 = array [0..31] of Byte;

    TAnubisCharKey128 = array [0..15] of Char8;
    TAnubisCharKey160 = array [0..19] of Char8;
    TAnubisCharKey192 = array [0..23] of Char8;
    TAnubisCharKey256 = array [0..31] of Char8;

    TAnubisBlock = TANUBlock;

const
    NULL_ANUBIS_BLOCK : TAnubisBlock = ($00,$00,$00,$00,
                                        $00,$00,$00,$00,
                                        $00,$00,$00,$00,
                                        $00,$00,$00,$00);

function AnubisEncrypt (const aValue: String;
                        const aKey; const aKeyBits: WORD;
                        IV: TAnubisBlock) : String; overload;
function AnubisEncrypt (const aValue: String;
                        const aKey; const aKeyBits: WORD) : String; overload;
function AnubisDecrypt (const aValue: String;
                        const aKey; const aKeyBits: WORD;
                        IV: TAnubisBlock) : String; overload;
function AnubisDecrypt (const aValue: String;
                        const aKey; const aKeyBits: WORD) : String; overload;

function AnubisError (const anErrorCode: Integer) : String;

{$I 'anu_err.inc'}

implementation

function StrToHex (const aValue: String) : String;
var
   I : Integer;
begin
   Result := '';
   for I := 1 to Length (aValue) do
   begin
       Result := Result + IntToHex ( Ord (aValue [i]), 2 );
   end;
end;

function HexToStr (const aValue: String) : String;
var
   I      : Integer;
   Buffer : String [3];
begin
   Result := '';
   I := 0;
   while ( I < Length (aValue) ) do
   begin
       Inc (I);
       if ( I mod 2 = 0 ) then
       begin
           Buffer := UpperCase ( Format ('$%s%s',[ aValue [I-1], aValue [i] ]) );
           Result := Result + Char ( StrToInt (Buffer) );
       end;
   end;
end;

function AnubisError (const anErrorCode: Integer) : String;
begin
    Result := '';
    case anErrorCode of
        -1   : Result := ERR_ANUBIS_INVALID_KEY_SIZE;
        -2   : Result := ERR_ANUBIS_INVALID_MODE;
        -3   : Result := ERR_ANUBIS_INVALID_LENGTH;
        -4   : Result := ERR_ANUBIS_DATA_AFTER_SHORT_BLOCK;
        -5   : Result := ERR_ANUBIS_MULTIPLE_INC_PROCS;
        -6   : Result := ERR_ANUBIS_INVALID_POINTER;
        -7   : Result := ERR_ANUBIS_EAX_INV_TEXT_LENGTH;
        -8   : Result := ERR_ANUBIS_EAX_INV_TAG_LENGTH;
        -9   : Result := ERR_ANUBIS_EAX_VERIFY_TAG;
        -15  : Result := ERR_ANUBIS_CTR_SEEK_OFFSET;
        -20  : Result := ERR_ANUBIS_INVALID_16BIT_LENGTH;
        else   Result := ERR_ANUBIS_UNKNOWN;
    end;
end;

{ Cipher Text Counter }
function AnubisEncrypt (const aValue: String;
                        const aKey; const aKeyBits: WORD;
                        IV: TAnubisBlock) : String;
var
    md  : TANUContext;
    buf : PChar;
    err : Integer;
    I   : integer;
begin
    Result := '';
    err := 0;
    try
        buf := PChar (aValue);
        err := ANU_CBC_Init_Encr (aKey,128,IV,md);
        //err := ANU_CTR_Init (aKey,128,IV,md);
        if ( err <> 0 ) then
            raise Exception.Create ( AnubisError (err) );
        err := ANU_CBC_Encrypt ( buf, buf, Length (buf), md );
        //err := ANU_CTR_Encrypt ( buf, buf, Length (buf), md );
        if ( err <> 0 ) then
            raise Exception.Create ( AnubisError (err) );
    finally
        if ( err = 0 ) then
            Result := StrToHex ( StrPas (buf) );
    end;
end;

function AnubisEncrypt (const aValue: String;
                        const aKey; const aKeyBits: WORD) : String;
begin
    Result := AnubisEncrypt (aValue,aKey,aKeyBits,NULL_ANUBIS_BLOCK);
end;

function AnubisDecrypt (const aValue: String;
                        const aKey; const aKeyBits: WORD;
                        IV: TAnubisBlock) : String;
var
    md  : TANUContext;
    buf : PChar;
    err : Integer;
begin
    Result := '';
    err := 0;
    try
        buf := PChar ( HexToStr (aValue) );
        err := ANU_CBC_Init_Decr (aKey,128,IV,md);
        //err := ANU_CTR_Init (aKey,128,IV,md);
        if ( err <> 0 ) then
            raise Exception.Create ( AnubisError (err) );
        err := ANU_CBC_Decrypt ( buf, buf, Length (buf), md );
        //err := ANU_CTR_Decrypt ( buf, buf, Length (buf), md );
        if ( err <> 0 ) then
            raise Exception.Create ( AnubisError (err) );
    finally
        if ( err = 0 ) then
            Result := StrPas (buf);
    end;
end;

function AnubisDecrypt (const aValue: String;
                        const aKey; const aKeyBits: WORD) : String;
begin
    Result := AnubisDecrypt (aValue,aKey,aKeyBits,NULL_ANUBIS_BLOCK);
end;

end.

все работает, но как-то нестабильно - некоторые строки при некоторых значениях Key и IV шифруются не в 128-битную строку, а во что-то другое - то короче, то длиннее.
применение несколько раз одного и того же набора Encrypt/Decrypt через несколько итераций искажает строку.
кто может помочь разобраться?