📄 rc5unit.pas
字号:
unit Rc5unit;
{*****************************************************************************
UNIT: TRC5Unit
Description: This unit contains an Object Pascal Object which can be used to
perform TRC5 block ciphers. 'RC5' is a trademark of RSA security
corporation, this code is an implementation of the algorithm which
RSA referers to as 'RC5'. TRC5 can be used with different
parameters, w (word size in bits), r (encryption rounds), and b
(key length in bytes). This implementation always uses TRC5-32,r,b.
Note the word size if fixed. You should always use at least 12
rounds(FRounds), and a key length of greater then 8bytes.
For more information on the algorithm see 'Applied Cryptography',
Bruce Snheier or visit RSA's www page where you can d/l a
postscript file describing the algorithm. http://www.rsa.com
The RC5 Algorithm was produced by Ronald Rivest.(See LEGAL)
-----------------------------------------------------------------------------
Code Author: Greg Carter, gregc@cryptocard.com
Organization: CRYPTOCard Corporation, info@cryptocard.com
R&D Division, Carleton Place, ON, CANADA, K7C 3T2
1-613-253-3152 Voice, 1-613-253-4685 Fax.
Date of V.1: Jan. 3 1996.
Compatibility & Testing with BP7.0: Anne Marcel Roorda, garfield@xs4all.nl
-----------------------------------------------------------------------------}
{Useage: Below is typical usage(for File)of the TRC5 Object,
Follow these steps:
1) Declare and Create Variable of type TRC5.
2) Set InputSource Type, either SourceFile, SourceByteArray, or
SourceString(Pascal style string).
3) Set Cipher Mode, optionally IVector.
4) Point to Input Source and set Input Length(If needed)
5) Point to Output Structure(array, file).
6) Set Key;
7) Call BF_EncipherData Method.
8) Reference the Output. Thats it.
**** Note **** Steps 2..6 can occure in any order.
Here is a procedure in Delphi used to encrypt a file:
procedure Tcryptfrm.OpenCiphButtonClick(Sender: TObject);
var
RC5: TRC5; (*Step 1*)
begin
RC5 := TRC5.Create;(*Step 1b*)
try
If OpenDialog1.Execute then
begin
RC5.InputType := SourceFile; (*Step 2*)
RC5.CipherMode := ECBMode; (*Step 3*)
RC5.InputFilePath := OpenDialog1.FileName; (*Step 4*)
RC5.OutputFilePath := ChangeFileExt(OpenDialog1.FileName, '.ccc'); (*Step 5*)
RC5.Key := 'abcdefghijklmnopqrstuvwxyz'; (*Step 6*)
RC5.BF_EncipherData(False); (*Step 7*)
end;
finally
RC5.free;
end;
end;
{-----------------------------------------------------------------------------}
{LEGAL: The algorithm is in the process of being patented, and its name
'RC5' is trademarked. Please oontact RSA Data Security for
licensing arrangements. This code is copyright by
CRYPTOCard. CRYPTOCard grants anyone who may wish to use, modify
or redistribute this code privileges to do so, provided the user
agrees to the following three(3) rules:
1)Any Applications, (ie exes which make use of this
Object...), for-profit or non-profit,
must acknowledge the author of this Object(ie.
TRC5 Implementation provided by Greg Carter, CRYPTOCard
Corporation) somewhere in the accompanying Application
documentation(ie AboutBox, HelpFile, readme...). NO runtime
or licensing fees are required!
2)Any Developer Component(ie Delphi Component, Visual Basic VBX,
DLL) derived from this software must acknowledge that it is
derived from "TRC5 Object Pascal Implementation Originated by
Greg Carter, CRYPTOCard Corporation 1996". Also all efforts should
be made to point out any changes from the original.
!!!!!Further, any Developer Components based on this code
*MAY NOT* be sold for profit. This Object was placed into the
public domain, and therefore any derived components should
also.!!!!!
3)CRYPTOCard Corporation makes no representations concerning this
software or the suitability of this software for any particular
purpose. It is provided "as is" without express or implied
warranty of any kind. CRYPTOCard accepts no liability from any
loss or damage as a result of using this software.
CRYPTOCard Corporation is in no way affiliated with RSA Data Security Inc.
The RC5 Algorithm was produced by Ronald Rivest.
-----------------------------------------------------------------------------
Why Use this instead of a freely available C DLL?
The goal was to provide a number of Encryption/Hash implementations in Object
Pascal, so that the Pascal Developer has considerably more freedom. These
Implementations are geared toward the PC(Intel) Microsoft Windows developer,
who will be using Borland's New 32bit developement environment(Delphi32). The
code generated by this new compiler is considerablely faster then 16bit versions.
And should provide the Developer with faster implementations then those using
C DLLs.
-----------------------------------------------------------------------------
NOTES: Make sure to read the LEGAL notes.
------------------------------------------------------------------------------
Revised: 00/00/00 BY: ******* Reason: ******
------------------------------------------------------------------------------
}
interface
{Declare the compiler defines}
{$I CRYPTDEF.INC}
{------Changeable compiler switches-----------------------------------}
{$A+ Word align variables }
{$F+ Force Far calls }
{$K+ Use smart callbacks
{$N+ Allow coprocessor instructions }
{$P+ Open parameters enabled }
{$S+ Stack checking }
{$T- @ operator is NOT typed }
{$IFDEF DELPHI}
{$U- Non Pentium safe FDIV }
{$Z- No automatic word-sized enumerations}
{$ENDIF}
{---------------------------------------------------------------------}
{.$DEFINE TEST}
uses SysUtils, Cryptcon{$IFDEF DELPHI}, Classes, Controls{$ENDIF}
{$IFDEF BP7},objects{$ENDIF};
const
P = $B7E15163;
Q = $9E3779B9;
type
AB = record
A: UWORD_32bits;
B: UWORD_32bits;
end; {AB}
pAB = ^AB;
{$IFDEF DELPHI}
TRC5 = class(TCrypto)
Private
{ Private declarations }
{$ENDIF}
{$IFDEF BP7}
PRC5 = ^TRC5; {For BP7 Objects}
TRC5 = object(TCrypto)
Public {Since BP7 doesn't support Properties, we make these Public}
{$ENDIF}
FRounds: BYTE; {Number of Rounds}
Private
FpActiveBlock: pAB; {64bit Cipher BLOCK}
FpA: PLong; {Lower 32 bits of Active Encipher Data, Little Endian}
FpB: PLong; {Upper 32 bits of Active Encipher Data, Little Endian}
FpKey: PLArray; {Pointer to SubKeys Array}
FSubKeyLen: WORD;{Previous Subkey Length}
{$IFDEF DELPHI}
Procedure EncipherBLOCK;override; {Enciphers BLOCK}
Procedure DecipherBLOCK;override; {Deciphers BLOCK}
Procedure SetKeys; override; {Sets up En\DecipherKey SubKeys}
{$ENDIF}
{$IFDEF BP7}
Procedure EncipherBLOCK; virtual; {Enciphers BLOCK}
Procedure DecipherBLOCK; virtual; {Deciphers BLOCK}
Procedure SetKeys; virtual; {Sets up En\DecipherKey SubKeys}
{$ENDIF}
public
{ Public declarations }
{$IFDEF DELPHI}
constructor Create(Owner: TComponent);override;
destructor Destroy;override;
Published
property Rounds: BYTE read FRounds write FRounds;
{$ENDIF}
{$IFDEF BP7}
constructor Init;
destructor Done;
{$ENDIF}
end;{TRC5}
{$IFDEF DELPHI}
procedure Register;{register the component to the Delphi toolbar}
{$ENDIF}
implementation
{This will only work on an intel}
{$IFDEF i286}
Function ROL(A: Longint; Amount: BYTE): Longint;NEAR;
begin
inline(
$8A/$4E/$04/ { mov cl,[bp+4] }
$66/$8B/$46/$06/ { mov eax,[bp+6] }
$66/$D3/$C0/ { rol eax,cl }
$66/$89/$46/$FC { mov [bp-4],eax }
);
end;
Function ROR(A: UWORD_32bits; Amount: BYTE): UWORD_32bits;NEAR;
begin
inline(
$8A/$4E/$04/ { mov cl,[bp+4] }
$66/$8B/$46/$06/ { mov eax,[bp+6] }
$66/$D3/$C8/ { ror eax,cl }
$66/$89/$46/$FC { mov [bp-4],eax }
);
end;
{$ENDIF}
{$IFDEF i386}
Function ROL(A: Longint; Amount: BYTE): Longint; Assembler;
asm
mov cl, Amount
rol eax, cl
end;
Function ROR(A: Longint; Amount: BYTE): Longint; Assembler;
asm
mov cl, Amount
ror eax, cl
end;
{$ENDIF}
{$IFDEF DELPHI}
procedure Register;
{Registers the Component to the toobar, on the tab named 'Crypto'}
{Now all a Delphi programmer needs to do is drag n drop to have
Blowfish encryption}
begin
RegisterComponents('Crypto', [TRC5]);
end;
{$ENDIF}
{==================================TRC5========================================}
{$IFDEF DELPHI}
constructor TRC5.Create(Owner: TComponent);
{$ENDIF}
{$IFDEF BP7}
constructor TRC5.Init;
{$ENDIF}
begin
{GetMem(FIVTemp, FBLOCKSIZE);}
{$IFDEF DELPHI}
inherited Create(Owner);
{$ENDIF}
FBLOCKSIZE := SizeOf(AB);
FpActiveBlock := @FSmallBuffer;
FpA := @FpActiveBlock^.A;
FpB := @FpActiveBlock^.B;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -