java

位置:IT落伍者 >> java >> 浏览文章

教你一招:CRC算法的实现


发布日期:2024年08月26日
 
教你一招:CRC算法的实现
uses

WindowsSysUtilsClasses;

const//Crc

Table:Array[] of DWORD=

( $$ $EEEC $BA

$DC $AFF $EA $EA

$EDB $DCBA $EDEE $DD

$BCB $EBCBD $EBD $BFD

$DB $ABF $FB $BEDE

$ADADD $DDDEEB $FDB $DC

$C $BAC $FDFA $ACEC

$CF $CD $FAFD $DDF

$BEC $CE $DE $A

$CED $BD $DDFD $AABB

$BAFA $BC $DBBBCD $ACBCF

$DCE $DFC $DCDDCF $ABDD

$DAC $DEA $CD $BFD

$BFB $BC $CFBA $BBDAF

$BE $F $CCDB $BBE

$FFC $C $CDAB $BDD

$DC $DB $DBC $EFDA

$B $BBF $FBFEA $EBD

$CA $FF $AE $EE

$FADBB $DDD $C $EC

$BBF $CC $D $FE

$CED $BAB $FC $FFC

$BDC $BE $BBEBEA $FCBC

$DDDDF $DAD $CDCF $FBDC

$DB $ABCE $ABC $DBBE

$ADFA $DDD $ADCD $DDFFB

$EA $EDFC $AD $DABD

$D $DE $AAACF $DDDCC

$C $AA $BEB $CC

$B $FB $BD $CEEF

$EDEFE $DC $BD $CDAB

$BD $EBD $BBDCB $CBACAD

$EDB $ABFBB $BEC $BDA

$EAD $DDAF $DB $DC

$EB $B $DDAE $AAAA

$EECFB $FFD $AAE $DEB

$FF $AD $EF $CFE

$FD $CB $C $EBE

$FEDB $DBE $DAAA $DDACC

$FBDFF $EBEEFF $BBE $BED

$DDAE $ADE $DCC $FDFF

$DBBF $ABC $FBDD $BB

$DDBDA $AFABC $AF $A

$DFEFC $ADF $EEEF $BE

$CBBC $BCA $FDA $E

$CCC $BBB $B $F

$CBABBE $BBDB $BBA $CBA

$CDFFA $BDCF $CDEB $BDEAED

$BCB $ECF $AAC $DA

$CA $EBEF $ $

$BFA $EBA $BBBAE $CBB

$DEB $EDBED $CDCEFB $BDBDF

$DDD $FDE $DDBF $FDAE

$BECD $FBB $FBE $B

$AE $FFFA $BCA $BC

$FEFF $FAE $BFFD $CCF

$AAE $DDDEE $E $BC

$A $DF $D $EEDB

$AEDAA $DDADC $DFB $DBF

$ABCAE $DEBBEC $BCFF $BFFE

$BDBDFC $CABACA $B $BAA

$BAD $CDD $DE $DBF

$BAE $CAB $DB $AFB

$BBBE $CCEA $ADFB $DEFD);

procedure GetCRCFile(FileName:string;var CRC:DWORD);

function GetCrcStr(s: string; Seed: LongInt):string;

implementation

procedure GetCRCFile(FileName:string;var CRC:DWORD);

var

F:file;

BytesRead:DWORD;

Buffer:array[] of Byte;

i:Word;

begin

FileMode :=;

CRC :=$ffffffff;

{$I}

AssignFile(FFileName);

Reset(F);

if IoResult = then

begin

repeat

BlockRead(FBufferSizeof(Buffer)BytesRead);

for i := to BytesRead do

CRC := (CRC shr ) xor Table[Buffer[i] xor (CRC and $ff)];

until BytesRead = ;

end;

CloseFile(F);

{$I+}

CRC := not CRC;

end;

function GetCrcStr(s: string; Seed: LongInt):string;

var

Count: Integer;

CrcVal: LongInt;

begin

CrcVal := Seed;

for Count := to Length(s) do

CrcVal := Table[Byte(CrcVal xor DWORD(Ord(s[Count])))] xor ((CrcVal shr ) and $FFFFFF);

Result := IntToHex(not(CrcVal) );

end;

end

调用

uses Crc;

procedure TFormButtonClick(Sender: TObject);

begin

EditText:=GetCrcStr();//这里取指定字符串的CRC校验值;

end;

procedure TFormButtonClick(Sender: TObject);

var

FileStr:String;

crc: DWORD;

begin

FileStr:=ApplicationExeName;//这里取指定的文件的crc校验值;

GetCRCFile(FileStrcrc);

if crc<> then

EditText:=PChar(IntToHex(crc));

end;               

上一篇:对称加密算法与非对称加密算法

下一篇:Java开发者的Scala指南: Scala+Twitter=Scitter