BlowFishの使い方


倉戸有珠  2013-11-18 13:11:46  No: 45594

FreePascalについていた BlowFish の使い方がわからないので教えてください。
やりたいことは、文字列の暗号化・複合化とTMemoryStreamの暗号化です。


倉戸有珠BlowFish (1)  2013-11-18 13:15:03  No: 45595

{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 1999-2000 by the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
{
  Unit implementing simple blowfish algorithm
}
{$ifdef fpc}
{$mode objfpc}
{$h+}
{$inline on}
{$endif}
unit BlowFish;

interface

uses SysUtils,Classes;

Const
  BFRounds = 16;      { 16 blowfish rounds }

Type
  PBlowFishKey = ^TBlowFishKey;
  TBlowFishKey = array[0..55] of Byte;
  TBFBlock     = array[0..1] of LongInt;     { BlowFish }

type
  TBlowFish = Class(TObject)
  Private
    PBox    : array[0..(BFRounds+1)] of LongInt;
    SBox    : array[0..3, 0..255] of LongInt;
    Function F(x : Cardinal)  : Cardinal;{$ifdef fpc}inline;{$endif}
  Public
    Constructor Create(Key : TBlowFishKey; KeySize : Integer);
    Procedure Encrypt(var Block : TBFBlock);
    Procedure Decrypt(var Block : TBFBlock);
  end;

Type
  EBlowFishError = Class(EStreamError);

  { TBlowFishStream }

  TBlowFishStream = Class(TOwnerStream)
  Private
    FBF     : TBlowFish;
    FData   : TBFBlock;
    FBufpos : Byte;
    FPos    : Int64;
  protected
    function GetPosition: Int64; override;
    procedure InvalidSeek; override;
  Public
    Constructor Create(AKey : TBlowFishKey; AKeySize : Byte; Dest: TStream);
    Constructor Create(Const KeyPhrase : String; Dest: TStream);
    Destructor Destroy; override;
    Property BlowFish : TBlowFish Read FBF;
  end;

  TBlowFishEncryptStream = Class(TBlowFishStream)
  public
    Destructor Destroy; override;
    function Write(const Buffer; Count: Longint): Longint; override;
    function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
    procedure Flush;
  end;

  TBlowFishDeCryptStream = Class(TBlowFishStream)
  public
    function Read(var Buffer; Count: Longint): Longint; override;
    function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
  end;

Implementation

ResourceString
  SNoSeekAllowed  = 'Seek not allowed on encryption streams';
  SErrEmptyPassPhraseNotAllowed = 'Empty passphrase is not allowed in constructor';

{ Blowfish lookup tables }

const
 bf_P: array[0..(BFRounds + 1)] of DWord = (
  $243F6A88, $85A308D3, $13198A2E, $03707344,
  $A4093822, $299F31D0, $082EFA98, $EC4E6C89,
  $452821E6, $38D01377, $BE5466CF, $34E90C6C,
  $C0AC29B7, $C97C50DD, $3F84D5B5, $B5470917,
  $9216D5D9, $8979FB1B);

const
 bf_S: array[0..3, 0..255] of DWord =
{
//長いので省略。
$D1310BA6, $98DFB5AC, $2FFD72DB, $D01ADFB7,
  $B8E1AFED, $6A267E96, $BA7C9045, $F12C7F99,
  $24A19947, $B3916CF7, $0801F2E2, $858EFC16,
  $636920D8, $71574E69, $A458FEA3, $F4933D7E,
//このようになっています
}


BlowFish(2)  2013-11-18 13:19:12  No: 45596

続きも貼ろうと思ったんですが、エラーが出て貼れないようです。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加