给你个AES加密的例子: unit MyAES;interfaceuses Windows, Classes, SysUtils;const FSBox: array [0..15, 0..15] of Byte = {0} (($63, $7c, $77, $7b, $f2, $6b, $6f, $c5, $30, $01, $67, $2b, $fe, $d7, $ab, $76), {1} ($ca, $82, $c9, $7d, $fa, $59, $47, $f0, $ad, $d4, $a2, $af, $9c, $a4, $72, $c0), {2} ($b7, $fd, $93, $26, $36, $3f, $f7, $cc, $34, $a5, $e5, $f1, $71, $d8, $31, $15), {3} ($04, $c7, $23, $c3, $18, $96, $05, $9a, $07, $12, $80, $e2, $eb, $27, $b2, $75), {4} ($09, $83, $2c, $1a, $1b, $6e, $5a, $a0, $52, $3b, $d6, $b3, $29, $e3, $2f, $84), {5} ($53, $d1, $00, $ed, $20, $fc, $b1, $5b, $6a, $cb, $be, $39, $4a, $4c, $58, $cf), {6} ($d0, $ef, $aa, $fb, $43, $4d, $33, $85, $45, $f9, $02, $7f, $50, $3c, $9f, $a8), {7} ($51, $a3, $40, $8f, $92, $9d, $38, $f5, $bc, $b6, $da, $21, $10, $ff, $f3, $d2), {8} ($cd, $0c, $13, $ec, $5f, $97, $44, $17, $c4, $a7, $7e, $3d, $64, $5d, $19, $73), {9} ($60, $81, $4f, $dc, $22, $2a, $90, $88, $46, $ee, $b8, $14, $de, $5e, $0b, $db), {a} ($e0, $32, $3a, $0a, $49, $06, $24, $5c, $c2, $d3, $ac, $62, $91, $95, $e4, $79), {b} ($e7, $c8, $37, $6d, $8d, $d5, $4e, $a9, $6c, $56, $f4, $ea, $65, $7a, $ae, $08), {c} ($ba, $78, $25, $2e, $1c, $a6, $b4, $c6, $e8, $dd, $74, $1f, $4b, $bd, $8b, $8a), {d} ($70, $3e, $b5, $66, $48, $03, $f6, $0e, $61, $35, $57, $b9, $86, $c1, $1d, $9e), {e} ($e1, $f8, $98, $11, $69, $d9, $8e, $94, $9b, $1e, $87, $e9, $ce, $55, $28, $df), {f} ($8c, $a1, $89, $0d, $bf, $e6, $42, $68, $41, $99, $2d, $0f, $b0, $54, $bb, $16)); FIsBox: array [0..15, 0..15] of Byte = {0} (($52, $09, $6a, $d5, $30, $36, $a5, $38, $bf, $40, $a3, $9e, $81, $f3, $d7, $fb), {1} ($7c, $e3, $39, $82, $9b, $2f, $ff, $87, $34, $8e, $43, $44, $c4, $de, $e9, $cb), {2} ($54, $7b, $94, $32, $a6, $c2, $23, $3d, $ee, $4c, $95, $0b, $42, $fa, $c3, $4e), {3} ($08, $2e, $a1, $66, $28, $d9, $24, $b2, $76, $5b, $a2, $49, $6d, $8b, $d1, $25), {4} ($72, $f8, $f6, $64, $86, $68, $98, $16, $d4, $a4, $5c, $cc, $5d, $65, $b6, $92), {5} ($6c, $70, $48, $50, $fd, $ed, $b9, $da, $5e, $15, $46, $57, $a7, $8d, $9d, $84), {6} ($90, $d8, $ab, $00, $8c, $bc, $d3, $0a, $f7, $e4, $58, $05, $b8, $b3, $45, $06), {7} ($d0, $2c, $1e, $8f, $ca, $3f, $0f, $02, $c1, $af, $bd, $03, $01, $13, $8a, $6b), {8} ($3a, $91, $11, $41, $4f, $67, $dc, $ea, $97, $f2, $cf, $ce, $f0, $b4, $e6, $73), {9} ($96, $ac, $74, $22, $e7, $ad, $35, $85, $e2, $f9, $37, $e8, $1c, $75, $df, $6e), {a} ($47, $f1, $1a, $71, $1d, $29, $c5, $89, $6f, $b7, $62, $0e, $aa, $18, $be, $1b), {b} ($fc, $56, $3e, $4b, $c6, $d2, $79, $20, $9a, $db, $c0, $fe, $78, $cd, $5a, $f4), {c} ($1f, $dd, $a8, $33, $88, $07, $c7, $31, $b1, $12, $10, $59, $27, $80, $ec, $5f), {d} ($60, $51, $7f, $a9, $19, $b5, $4a, $0d, $2d, $e5, $7a, $9f, $93, $c9, $9c, $ef), {e} ($a0, $e0, $3b, $4d, $ae, $2a, $f5, $b0, $c8, $eb, $bb, $3c, $83, $53, $99, $61), {f} ($17, $2b, $04, $7e, $ba, $77, $d6, $26, $e1, $69, $14, $63, $55, $21, $0c, $7d)); FRcon: array [0..10, 0..3] of Byte = (($00, $00, $00, $00), ($01, $00, $00, $00), ($02, $00, $00, $00), ($04, $00, $00, $00), ($08, $00, $00, $00), ($10, $00, $00, $00), ($20, $00, $00, $00), ($40, $00, $00, $00), ($80, $00, $00, $00), ($1b, $00, $00, $00), ($36, $00, $00, $00));type TKeySize = (Bits128, Bits192, Bits256 ); TAES = class(TObject) private FNb: Integer; FNk: Integer; FNr: Integer; FKey: array [0..31] of Byte; FW: array [0..59, 0..3] of Byte; FState: array [0..3, 0..3] of Byte; procedure SetNbNkNr(keySize: TKeySize); procedure AddRoundKey(round: Integer); procedure KeyExpansion(); procedure SubBytes(); procedure InvSubBytes(); procedure ShiftRows(); procedure InvShiftRows(); procedure MixColumns(); procedure InvMixColumns(); function gfmultby01(b: Byte): Byte; function gfmultby02(b: Byte): Byte; function gfmultby03(b: Byte): Byte; function gfmultby09(b: Byte): Byte; function gfmultby0b(b: Byte): Byte; function gfmultby0d(b: Byte): Byte; function gfmultby0e(b: Byte): Byte; function SubWord(word: LongWord): LongWord; function RotWord(word: LongWord): LongWord; procedure FCipher(input: array of Byte; var output: array of Byte); procedure FInvCipher(input: array of Byte; var output: array of Byte); public constructor Create(keySize: TKeySize; key: string); function CipherStream(input: TStream; output: TStream): Boolean; function InvCipherStream(input: TStream; output: TStream): Boolean; function CiperString(input: string): string; function InvCipherString(input: string): string; function Test(L: LongWord): string; end;implementation{ TAES }procedure TAES.AddRoundKey(round: Integer); var r, c: Integer; begin for r := 0 to 3 do for c := 0 to 3 do FState[r,c] := Byte((Integer(FState[r, c]) xor (Fw[(round*4)+c, r]))); end;procedure TAES.FCipher(input: array of Byte; var output: array of Byte); var i, round: Integer; begin for i := 0 to 4 * FNb - 1 do FState[i mod 4, i div 4] := input[i]; AddRoundKey(0); for round := 1 to FNr - 1 do begin SubBytes(); ShiftRows(); MixColumns(); AddRoundKey(round); end; SubBytes(); ShiftRows(); AddRoundKey(FNr); for i := 0 to FNb * 4 - 1 do output[i] := FState[i mod 4, i div 4]; end;constructor TAES.Create(keySize: TKeySize; key: string); begin inherited Create; SetNbNkNr(keySize); ZeroMemory(@FKey, SizeOf(FKey)); if Length(key) > FNk * 4 then CopyMemory(@FKey, @key[1], FNk * 4) else if Length(key) > 0 then CopyMemory(@FKey, @key[1], Length(key)); KeyExpansion(); end;function TAES.gfmultby01(b: Byte): Byte; begin Result := b; end;function TAES.gfmultby02(b: Byte): Byte; begin if (b < $80) then Result := Byte(Integer(b shl 1)) else Result := Byte((Integer(b shl 1)) xor (Integer($1b))); end;function TAES.gfmultby03(b: Byte): Byte; begin Result := Byte((Integer(gfmultby02(b))) xor (Integer(b))); end;function TAES.gfmultby09(b: Byte): Byte; begin Result := Byte((Integer(gfmultby02(gfmultby02(gfmultby02(b))))) xor (Integer(b))); end;function TAES.gfmultby0b(b: Byte): Byte; begin Result := Byte((Integer(gfmultby02(gfmultby02(gfmultby02(b))))) xor (Integer(gfmultby02(b))) xor (Integer(b))); end;function TAES.gfmultby0d(b: Byte): Byte; begin Result := Byte((Integer(gfmultby02(gfmultby02(gfmultby02(b))))) xor (Integer(gfmultby02(gfmultby02(b)))) xor (Integer(b))); end;function TAES.gfmultby0e(b: Byte): Byte; begin Result := Byte((Integer(gfmultby02(gfmultby02(gfmultby02(b))))) xor (Integer(gfmultby02(gfmultby02(b)))) xor (Integer(gfmultby02(b)))); end;procedure TAES.FInvCipher(input: array of Byte; var output: array of Byte); var i, round: Integer; begin for i := 0 to 4 * FNb - 1 do FState[i mod 4, i div 4] := input[i]; AddRoundKey(FNr); for round := FNr - 1 downto 1 do begin InvShiftRows(); InvSubBytes(); AddRoundKey(round); InvMixColumns(); end; InvShiftRows(); InvSubBytes(); AddRoundKey(0); for i := 0 to FNb * 4 - 1 do output[i] := FState[i mod 4, i div 4]; end;
procedure TAES.InvMixColumns; var temp: array [0..3, 0..3] of Byte; r, c: Integer; begin for r := 0 to 3 do for c := 0 to 3 do temp[r, c] := FState[r, c]; for c := 0 to 3 do begin FState[0, c] := Byte((Integer(gfmultby0e(temp[0, c])) xor (Integer(gfmultby0b(temp[1, c]))) xor (Integer(gfmultby0d(temp[2, c]))) xor (Integer(gfmultby09(temp[3, c]))))); FState[1, c] := Byte((Integer(gfmultby09(temp[0, c])) xor (Integer(gfmultby0e(temp[1, c]))) xor (Integer(gfmultby0b(temp[2, c]))) xor (Integer(gfmultby0d(temp[3, c]))))); FState[2, c] := Byte((Integer(gfmultby0d(temp[0, c])) xor (Integer(gfmultby09(temp[1, c]))) xor (Integer(gfmultby0e(temp[2, c]))) xor (Integer(gfmultby0b(temp[3, c]))))); FState[3, c] := Byte((Integer(gfmultby0b(temp[0, c])) xor (Integer(gfmultby0d(temp[1, c]))) xor (Integer(gfmultby09(temp[2, c]))) xor (Integer(gfmultby0e(temp[3, c]))))); end; end;procedure TAES.InvShiftRows; var temp: array [0..3, 0..3] of Byte; r, c: Integer; begin for r := 0 to 3 do for c := 0 to 3 do temp[r, c] := FState[r, c]; for r := 1 to 3 do for c := 0 to 3 do FState[r, (c + r) mod FNb] := temp[r, c]; end;procedure TAES.InvSubBytes; var r, c: Integer; begin for r := 0 to 3 do for c := 0 to 3 do FState[r, c] := FIsBox[FState[r, c] shr 4, FState[r, c] and $0F]; end;procedure TAES.KeyExpansion; var row: Integer; temp: array [0..3] of Byte; TPL: LongWord; begin for row := 0 to FNk - 1 do begin FW[row, 0] := FKey[4 * row]; FW[row, 1] := FKey[4 * row + 1]; FW[row, 2] := FKey[4 * row + 2]; FW[row, 3] := FKey[4 * row + 3]; end; for row := FNk to FNb * (FNr + 1) - 1 do begin temp[0] := FW[row - 1, 0]; temp[1] := FW[row - 1, 1]; temp[2] := FW[row - 1, 2]; temp[3] := FW[row - 1, 3]; if row mod FNk = 0 then begin CopyMemory(@TPL, @temp, SizeOf(TPL)); TPL := SubWord(RotWord(TPL)); CopyMemory(@temp, @TPL, SizeOf(temp)); temp[0] := Byte((Integer(temp[0])) xor (Integer(FRcon[row div FNk, 0]))); temp[1] := Byte((Integer(temp[1])) xor (Integer(FRcon[row div FNk, 1]))); temp[2] := Byte((Integer(temp[2])) xor (Integer(FRcon[row div FNk, 2]))); temp[3] := Byte((Integer(temp[3])) xor (Integer(FRcon[row div FNk, 3]))); end else if (FNK > 6) and ((row mod FNk) = 4) then begin CopyMemory(@TPL, @temp, SizeOf(TPL)); TPL := SubWord(TPL); CopyMemory(@temp, @TPL, SizeOf(temp)); end; FW[row, 0] := Byte((Integer(FW[row-FNk, 0])) xor (Integer(temp[0]))); FW[row, 1] := Byte((Integer(FW[row-FNk, 1])) xor (Integer(temp[1]))); FW[row, 2] := Byte((Integer(FW[row-FNk, 2])) xor (Integer(temp[2]))); FW[row, 3] := Byte((Integer(FW[row-FNk, 3])) xor (Integer(temp[3]))); end; end;procedure TAES.MixColumns; var temp: array [0..3, 0..3] of Byte; r, c: Integer; begin for r := 0 to 3 do for c := 0 to 3 do temp[r, c] := FState[r, c]; for c := 0 to 3 do begin FState[0, c] := Byte((Integer(gfmultby02(temp[0, c])) xor (Integer(gfmultby03(temp[1, c]))) xor (Integer(gfmultby01(temp[2, c]))) xor (Integer(gfmultby01(temp[3, c]))))); FState[1, c] := Byte((Integer(gfmultby01(temp[0, c])) xor (Integer(gfmultby02(temp[1, c]))) xor (Integer(gfmultby03(temp[2, c]))) xor (Integer(gfmultby01(temp[3, c]))))); FState[2, c] := Byte((Integer(gfmultby01(temp[0, c])) xor (Integer(gfmultby01(temp[1, c]))) xor (Integer(gfmultby02(temp[2, c]))) xor (Integer(gfmultby03(temp[3, c]))))); FState[3, c] := Byte((Integer(gfmultby03(temp[0, c])) xor (Integer(gfmultby01(temp[1, c]))) xor (Integer(gfmultby01(temp[2, c]))) xor (Integer(gfmultby02(temp[3, c]))))); end; end;function TAES.RotWord(word: LongWord): LongWord; var Tmp: array [0..3] of Byte; B: Byte; L: LongWord; begin CopyMemory(@Tmp, @word, SizeOf(Tmp)); B := Tmp[0]; Tmp[0] := Tmp[1]; Tmp[1] := Tmp[2]; Tmp[2] := Tmp[3]; Tmp[3] := B; CopyMemory(@L, @Tmp, SizeOf(Tmp)); Result := L; end;procedure TAES.SetNbNkNr(keySize: TKeySize); begin FNb := 4; case keySize of Bits128: begin FNk := 4; FNr := 10; end; Bits192: begin FNk := 6; FNr := 12; end; Bits256: begin FNk := 8; FNr := 14; end; end; end;procedure TAES.ShiftRows; var temp: array [0..3, 0..3] of Byte; r, c: Integer; begin for r := 0 to 3 do for c := 0 to 3 do temp[r, c] := FState[r, c]; for r := 1 to 3 do for c := 0 to 3 do FState[r, c] := temp[r, (c + r) mod FNb]; end;procedure TAES.SubBytes; var r, c: Integer; begin for r := 0 to 3 do for c := 0 to 3 do FState[r, c] := FSBox[FState[r, c] shr 4, FState[r, c] and $0F]; end;function TAES.SubWord(word: LongWord): LongWord; var Tmp: array [0..3] of Byte; L: LongWord; begin CopyMemory(@Tmp, @word, SizeOf(Tmp)); Tmp[0] := FSbox[ Tmp[0] shr 4, Tmp[0] and $0f ]; Tmp[1] := FSbox[ Tmp[1] shr 4, Tmp[1] and $0f ]; Tmp[2] := FSbox[ Tmp[2] shr 4, Tmp[2] and $0f ]; Tmp[3] := FSbox[ Tmp[3] shr 4, Tmp[3] and $0f ]; CopyMemory(@L, @Tmp, SizeOf(Tmp)); Result := L; end;function TAES.CipherStream(input: TStream; output: TStream): Boolean; var ai, ao: array [0..15] of Byte; at: array [0..11] of Byte; iSize, iRead, iWrite: Integer; begin Result := True; input.Position := 0; output.Position := 0; iSize := input.Size; CopyMemory(@ai, @iSize, SizeOf(iSize)); iRead := input.Read(at, SizeOf(at)); ZeroMemory(@at[iRead], SizeOf(at) - iRead); CopyMemory(@ai[4], @at, SizeOf(at)); while iRead > 0 do begin FCipher(ai, ao); iWrite := output.Write(ao, SizeOf(ao)); if iWrite <> SizeOf(ao) then Result := False; iRead := input.Read(ai, SizeOf(ai)); ZeroMemory(@ai[iRead], SizeOf(ai) - iRead); end; end;function TAES.InvCipherStream(input: TStream; output: TStream): Boolean; var ai, ao: array [0..15] of Byte; at: array [0..11] of Byte; iSize, iRead, iWrite: Integer; begin Result := True; input.Position := 0; output.Position := 0; iRead := input.Read(ai, SizeOf(ai)); ZeroMemory(@ai[iRead], SizeOf(ai) - iRead); if iRead > 0 then begin FInvCipher(ai, ao); CopyMemory(@iSize, @ao, SizeOf(iSize)); CopyMemory(@at, @ao[4], SizeOf(at)); iWrite := output.Write(at, SizeOf(at)); if iWrite <> SizeOf(at) then Result := False; end; iRead := input.Read(ai, SizeOf(ai)); ZeroMemory(@ai[iRead], SizeOf(ai) - iRead); while iRead > 0 do begin FInvCipher(ai, ao); iWrite := output.Write(ao, SizeOf(ao)); if iWrite <> SizeOf(ao) then Result := False; iRead := input.Read(ai, SizeOf(ai)); ZeroMemory(@ai[iRead], SizeOf(ai) - iRead); end; output.Size := iSize; end;function TAES.CiperString(input: string): string; var _input, _output: TStringStream; a: array [0..1023] of Byte; iRead, i: Integer; begin Result := ''; _input := TStringStream.Create(input); _output := TStringStream.Create(''); try CipherStream(_input, _output); _output.Position := 0; iRead := _output.Read(a, SizeOf(a)); while iRead > 0 do begin for i := 0 to iRead - 1 do Result := Result + Format('%.2X', [a[i]]); iRead := _output.Read(a, SizeOf(a)); end; finally _input.Free; _output.Free; end; end;
转载以前搜集的资料,忘了哪里出处了,呵: 汉字是两个字节的,可以取汉字的区位码,区码为ord(s[1]),位码为ord(s[2]),通过互换实现简单加解密下面的代码是汉字和区位码互换的 procedure TForm1.Button1Click(Sender: TObject); var qu,wei:integer; s:string; begin s:=edit1.Text; qu:=ord(s[1]); wei:=ord(s[2]); s:=''; if (qu-160)<10 then s:='0'; s:=s+inttostr(qu-160); if (wei-160)<10 then s:=s+'0'; s:=s+inttostr(wei-160)+' or '; s:=s+inttohex(qu,2)+inttohex(wei,2); edit2.Text:=s;//汉字转区位码 end;procedure TForm1.Button2Click(Sender: TObject); var ln,qu,wei:integer; ss,s:string; begin ss:='$'+edit3.Text; ln:=strtoint(ss); s:=inttohex(ln,4); s:='$'+s; ln:=strtoint(s); wei:=ln and $ff; qu:=ln shr 8; s:=char( qu )+ char( wei ); edit4.Text:=s;//区位码转汉字 end;
随便在网上搜个啥标准加密算法就是了,如md5、des等
http://topic.csdn.net/t/20041116/12/3557236.html
function TAES.InvCipherString(input: string): string; var _input, _output: TStringStream; a: Byte; i, iSize: Integer;begin Result := ''; _input := TStringStream.Create(''); iSize := Length(input); for I := 0 to iSize div 2 - 1 do begin a := StrToIntDef('$0' + Copy(input, 2 * i + 1, 2), 0); _input.Write(a, SizeOf(a)); end; _output := TStringStream.Create(''); try InvCipherStream(_input, _output); Result := _output.DataString; finally _input.Free; _output.Free; end; end;function TAES.Test(L: LongWord): string; var I, J: Integer; input, output: array [0..15] of Byte; begin Result := ''; for I := 0 to 15 do for J := 0 to 15 do Result := Result + Format('FSBox(%d, %d) = %d'#13#10, [I, J, FSBox[i, j]]); for I := 0 to 15 do for J := 0 to 15 do Result := Result + Format('FIsBox(%d, %d) = %d'#13#10, [I, J, FIsBox[i, j]]); for I := 0 to 10 do for J := 0 to 3 do Result := Result + Format('FRcon(%d, %d) = %d'#13#10, [I, J, FRcon[i, j]]); for I := 0 to 31 do Result := Result + Format('FKey(%d) = %d'#13#10, [i, FKey[i]]); for I := 0 to 59 do for J := 0 to 3 do Result := Result + Format('FW(%d, %d) = %d'#13#10, [I, J, FW[i, j]]); for I := 0 to 15 do begin input[i] := i + $30; if input[i] > $39 then input[i] := input[i] - 10; end; Self.FCipher(input, output); Result := ''; for I := 0 to 15 do begin Result := Result + Format('output(%d) = %.2X'#13#10, [i, output[i]]) end; //Result := Fstr;
end;end.------------------- var AES: TAES; begin AES := TAES.Create(Bits192, 'ABCDEFGHIJKLMNOPQRSTUVWX'); ShowMessage(AES.InvCipherString(AES.CiperString('我'))); AES.Free; end;
用我这个吧,简单,方便,灵活 function Encrypt(const S: String; Key: Word): String; var I: Integer; begin Result := S; for I := 1 to Length(S) do begin Result[I] := Char(Byte(S[I]) xor (Key shr 8)); Key := (Byte(Result[I]) + Key) * 11 + 12; if Result[I] = Chr(0) then Result[I] := S[I]; end; Result := StrToHex(Result); end;function Decrypt(const S: String; Key: Word): String; var I: Integer; S1: String; begin S1 := HexToStr(S); Result := S1; for I := 1 to Length(S1) do begin if Char(Byte(S1[I]) xor (Key shr 8)) = Chr(0) then begin Result[I] := S1[I]; Key := (Byte(Chr(0)) + Key) * 11 + 12; end else begin Result[I] := Char(Byte(S1[I]) xor (Key shr 8)); Key := (Byte(S1[I]) + Key) * 11 + 12; end; end; end;
不好意思,漏了两个自定义函数 function StrToHex(AStr: String): String; var I: Integer; begin Result := ''; for I := 1 to Length(AStr) do begin Result := Result + Format('%2x', [Byte(AStr[I])]); end; I := Pos(' ', Result); while I <> 0 do begin Result[I] := '0'; I:= Pos(' ', Result); end; end;function HexToStr(AStr: String): String; var I: Integer; CharValue: Word; begin Result := ''; for I := 1 to Trunc(Length(Astr) / 2) do begin Result := Result + ' '; CharValue := TransChar(AStr[2 * I - 1]) * 16 + TransChar(AStr[2 * I]); Result[I] := Char(CharValue); end; end;
http://topic.csdn.net/t/20041116/12/3557236.html
这类代码随处都能找到的 编码为Base64即可明文传输
先base64 再 自定义base64码表再来一次base64..哈哈, 想解是麻烦点了...
www.smatrix.org 安全距阵 到那去看一下吧
不少加密算法是针对BIN数据的 保存起来可能没有Base64的方便
不好意思,还漏了一个函数 function TransChar(AChar: Char): Integer; begin if AChar in ['0'..'9'] then Result := Ord(AChar) - Ord('0') else Result := 10 + Ord(AChar) - Ord('A'); end;
unit MyAES;interfaceuses
Windows, Classes, SysUtils;const
FSBox: array [0..15, 0..15] of Byte =
{0} (($63, $7c, $77, $7b, $f2, $6b, $6f, $c5, $30, $01, $67, $2b, $fe, $d7, $ab, $76),
{1} ($ca, $82, $c9, $7d, $fa, $59, $47, $f0, $ad, $d4, $a2, $af, $9c, $a4, $72, $c0),
{2} ($b7, $fd, $93, $26, $36, $3f, $f7, $cc, $34, $a5, $e5, $f1, $71, $d8, $31, $15),
{3} ($04, $c7, $23, $c3, $18, $96, $05, $9a, $07, $12, $80, $e2, $eb, $27, $b2, $75),
{4} ($09, $83, $2c, $1a, $1b, $6e, $5a, $a0, $52, $3b, $d6, $b3, $29, $e3, $2f, $84),
{5} ($53, $d1, $00, $ed, $20, $fc, $b1, $5b, $6a, $cb, $be, $39, $4a, $4c, $58, $cf),
{6} ($d0, $ef, $aa, $fb, $43, $4d, $33, $85, $45, $f9, $02, $7f, $50, $3c, $9f, $a8),
{7} ($51, $a3, $40, $8f, $92, $9d, $38, $f5, $bc, $b6, $da, $21, $10, $ff, $f3, $d2),
{8} ($cd, $0c, $13, $ec, $5f, $97, $44, $17, $c4, $a7, $7e, $3d, $64, $5d, $19, $73),
{9} ($60, $81, $4f, $dc, $22, $2a, $90, $88, $46, $ee, $b8, $14, $de, $5e, $0b, $db),
{a} ($e0, $32, $3a, $0a, $49, $06, $24, $5c, $c2, $d3, $ac, $62, $91, $95, $e4, $79),
{b} ($e7, $c8, $37, $6d, $8d, $d5, $4e, $a9, $6c, $56, $f4, $ea, $65, $7a, $ae, $08),
{c} ($ba, $78, $25, $2e, $1c, $a6, $b4, $c6, $e8, $dd, $74, $1f, $4b, $bd, $8b, $8a),
{d} ($70, $3e, $b5, $66, $48, $03, $f6, $0e, $61, $35, $57, $b9, $86, $c1, $1d, $9e),
{e} ($e1, $f8, $98, $11, $69, $d9, $8e, $94, $9b, $1e, $87, $e9, $ce, $55, $28, $df),
{f} ($8c, $a1, $89, $0d, $bf, $e6, $42, $68, $41, $99, $2d, $0f, $b0, $54, $bb, $16));
FIsBox: array [0..15, 0..15] of Byte =
{0} (($52, $09, $6a, $d5, $30, $36, $a5, $38, $bf, $40, $a3, $9e, $81, $f3, $d7, $fb),
{1} ($7c, $e3, $39, $82, $9b, $2f, $ff, $87, $34, $8e, $43, $44, $c4, $de, $e9, $cb),
{2} ($54, $7b, $94, $32, $a6, $c2, $23, $3d, $ee, $4c, $95, $0b, $42, $fa, $c3, $4e),
{3} ($08, $2e, $a1, $66, $28, $d9, $24, $b2, $76, $5b, $a2, $49, $6d, $8b, $d1, $25),
{4} ($72, $f8, $f6, $64, $86, $68, $98, $16, $d4, $a4, $5c, $cc, $5d, $65, $b6, $92),
{5} ($6c, $70, $48, $50, $fd, $ed, $b9, $da, $5e, $15, $46, $57, $a7, $8d, $9d, $84),
{6} ($90, $d8, $ab, $00, $8c, $bc, $d3, $0a, $f7, $e4, $58, $05, $b8, $b3, $45, $06),
{7} ($d0, $2c, $1e, $8f, $ca, $3f, $0f, $02, $c1, $af, $bd, $03, $01, $13, $8a, $6b),
{8} ($3a, $91, $11, $41, $4f, $67, $dc, $ea, $97, $f2, $cf, $ce, $f0, $b4, $e6, $73),
{9} ($96, $ac, $74, $22, $e7, $ad, $35, $85, $e2, $f9, $37, $e8, $1c, $75, $df, $6e),
{a} ($47, $f1, $1a, $71, $1d, $29, $c5, $89, $6f, $b7, $62, $0e, $aa, $18, $be, $1b),
{b} ($fc, $56, $3e, $4b, $c6, $d2, $79, $20, $9a, $db, $c0, $fe, $78, $cd, $5a, $f4),
{c} ($1f, $dd, $a8, $33, $88, $07, $c7, $31, $b1, $12, $10, $59, $27, $80, $ec, $5f),
{d} ($60, $51, $7f, $a9, $19, $b5, $4a, $0d, $2d, $e5, $7a, $9f, $93, $c9, $9c, $ef),
{e} ($a0, $e0, $3b, $4d, $ae, $2a, $f5, $b0, $c8, $eb, $bb, $3c, $83, $53, $99, $61),
{f} ($17, $2b, $04, $7e, $ba, $77, $d6, $26, $e1, $69, $14, $63, $55, $21, $0c, $7d));
FRcon: array [0..10, 0..3] of Byte =
(($00, $00, $00, $00),
($01, $00, $00, $00),
($02, $00, $00, $00),
($04, $00, $00, $00),
($08, $00, $00, $00),
($10, $00, $00, $00),
($20, $00, $00, $00),
($40, $00, $00, $00),
($80, $00, $00, $00),
($1b, $00, $00, $00),
($36, $00, $00, $00));type
TKeySize = (Bits128, Bits192, Bits256 );
TAES = class(TObject)
private
FNb: Integer;
FNk: Integer;
FNr: Integer;
FKey: array [0..31] of Byte;
FW: array [0..59, 0..3] of Byte;
FState: array [0..3, 0..3] of Byte;
procedure SetNbNkNr(keySize: TKeySize);
procedure AddRoundKey(round: Integer);
procedure KeyExpansion();
procedure SubBytes();
procedure InvSubBytes();
procedure ShiftRows();
procedure InvShiftRows();
procedure MixColumns();
procedure InvMixColumns();
function gfmultby01(b: Byte): Byte;
function gfmultby02(b: Byte): Byte;
function gfmultby03(b: Byte): Byte;
function gfmultby09(b: Byte): Byte;
function gfmultby0b(b: Byte): Byte;
function gfmultby0d(b: Byte): Byte;
function gfmultby0e(b: Byte): Byte;
function SubWord(word: LongWord): LongWord;
function RotWord(word: LongWord): LongWord;
procedure FCipher(input: array of Byte; var output: array of Byte);
procedure FInvCipher(input: array of Byte; var output: array of Byte);
public
constructor Create(keySize: TKeySize; key: string);
function CipherStream(input: TStream; output: TStream): Boolean;
function InvCipherStream(input: TStream; output: TStream): Boolean;
function CiperString(input: string): string;
function InvCipherString(input: string): string;
function Test(L: LongWord): string;
end;implementation{ TAES }procedure TAES.AddRoundKey(round: Integer);
var
r, c: Integer;
begin
for r := 0 to 3 do
for c := 0 to 3 do
FState[r,c] := Byte((Integer(FState[r, c]) xor (Fw[(round*4)+c, r])));
end;procedure TAES.FCipher(input: array of Byte; var output: array of Byte);
var
i, round: Integer;
begin
for i := 0 to 4 * FNb - 1 do
FState[i mod 4, i div 4] := input[i];
AddRoundKey(0);
for round := 1 to FNr - 1 do
begin
SubBytes();
ShiftRows();
MixColumns();
AddRoundKey(round);
end;
SubBytes();
ShiftRows();
AddRoundKey(FNr);
for i := 0 to FNb * 4 - 1 do
output[i] := FState[i mod 4, i div 4];
end;constructor TAES.Create(keySize: TKeySize; key: string);
begin
inherited Create;
SetNbNkNr(keySize);
ZeroMemory(@FKey, SizeOf(FKey));
if Length(key) > FNk * 4 then
CopyMemory(@FKey, @key[1], FNk * 4)
else if Length(key) > 0 then
CopyMemory(@FKey, @key[1], Length(key));
KeyExpansion();
end;function TAES.gfmultby01(b: Byte): Byte;
begin
Result := b;
end;function TAES.gfmultby02(b: Byte): Byte;
begin
if (b < $80) then
Result := Byte(Integer(b shl 1))
else
Result := Byte((Integer(b shl 1)) xor (Integer($1b)));
end;function TAES.gfmultby03(b: Byte): Byte;
begin
Result := Byte((Integer(gfmultby02(b))) xor (Integer(b)));
end;function TAES.gfmultby09(b: Byte): Byte;
begin
Result := Byte((Integer(gfmultby02(gfmultby02(gfmultby02(b))))) xor (Integer(b)));
end;function TAES.gfmultby0b(b: Byte): Byte;
begin
Result := Byte((Integer(gfmultby02(gfmultby02(gfmultby02(b))))) xor (Integer(gfmultby02(b))) xor (Integer(b)));
end;function TAES.gfmultby0d(b: Byte): Byte;
begin
Result := Byte((Integer(gfmultby02(gfmultby02(gfmultby02(b))))) xor (Integer(gfmultby02(gfmultby02(b)))) xor (Integer(b)));
end;function TAES.gfmultby0e(b: Byte): Byte;
begin
Result := Byte((Integer(gfmultby02(gfmultby02(gfmultby02(b))))) xor (Integer(gfmultby02(gfmultby02(b)))) xor (Integer(gfmultby02(b))));
end;procedure TAES.FInvCipher(input: array of Byte; var output: array of Byte);
var
i, round: Integer;
begin
for i := 0 to 4 * FNb - 1 do
FState[i mod 4, i div 4] := input[i];
AddRoundKey(FNr);
for round := FNr - 1 downto 1 do
begin
InvShiftRows();
InvSubBytes();
AddRoundKey(round);
InvMixColumns();
end;
InvShiftRows();
InvSubBytes();
AddRoundKey(0);
for i := 0 to FNb * 4 - 1 do
output[i] := FState[i mod 4, i div 4];
end;
procedure TAES.InvMixColumns;
var
temp: array [0..3, 0..3] of Byte;
r, c: Integer;
begin
for r := 0 to 3 do
for c := 0 to 3 do
temp[r, c] := FState[r, c];
for c := 0 to 3 do
begin
FState[0, c] := Byte((Integer(gfmultby0e(temp[0, c]))
xor (Integer(gfmultby0b(temp[1, c])))
xor (Integer(gfmultby0d(temp[2, c])))
xor (Integer(gfmultby09(temp[3, c])))));
FState[1, c] := Byte((Integer(gfmultby09(temp[0, c]))
xor (Integer(gfmultby0e(temp[1, c])))
xor (Integer(gfmultby0b(temp[2, c])))
xor (Integer(gfmultby0d(temp[3, c])))));
FState[2, c] := Byte((Integer(gfmultby0d(temp[0, c]))
xor (Integer(gfmultby09(temp[1, c])))
xor (Integer(gfmultby0e(temp[2, c])))
xor (Integer(gfmultby0b(temp[3, c])))));
FState[3, c] := Byte((Integer(gfmultby0b(temp[0, c]))
xor (Integer(gfmultby0d(temp[1, c])))
xor (Integer(gfmultby09(temp[2, c])))
xor (Integer(gfmultby0e(temp[3, c])))));
end;
end;procedure TAES.InvShiftRows;
var
temp: array [0..3, 0..3] of Byte;
r, c: Integer;
begin
for r := 0 to 3 do
for c := 0 to 3 do
temp[r, c] := FState[r, c];
for r := 1 to 3 do
for c := 0 to 3 do
FState[r, (c + r) mod FNb] := temp[r, c];
end;procedure TAES.InvSubBytes;
var
r, c: Integer;
begin
for r := 0 to 3 do
for c := 0 to 3 do
FState[r, c] := FIsBox[FState[r, c] shr 4, FState[r, c] and $0F];
end;procedure TAES.KeyExpansion;
var
row: Integer;
temp: array [0..3] of Byte;
TPL: LongWord;
begin
for row := 0 to FNk - 1 do
begin
FW[row, 0] := FKey[4 * row];
FW[row, 1] := FKey[4 * row + 1];
FW[row, 2] := FKey[4 * row + 2];
FW[row, 3] := FKey[4 * row + 3];
end;
for row := FNk to FNb * (FNr + 1) - 1 do
begin
temp[0] := FW[row - 1, 0];
temp[1] := FW[row - 1, 1];
temp[2] := FW[row - 1, 2];
temp[3] := FW[row - 1, 3];
if row mod FNk = 0 then
begin
CopyMemory(@TPL, @temp, SizeOf(TPL));
TPL := SubWord(RotWord(TPL));
CopyMemory(@temp, @TPL, SizeOf(temp));
temp[0] := Byte((Integer(temp[0])) xor (Integer(FRcon[row div FNk, 0])));
temp[1] := Byte((Integer(temp[1])) xor (Integer(FRcon[row div FNk, 1])));
temp[2] := Byte((Integer(temp[2])) xor (Integer(FRcon[row div FNk, 2])));
temp[3] := Byte((Integer(temp[3])) xor (Integer(FRcon[row div FNk, 3])));
end else if (FNK > 6) and ((row mod FNk) = 4) then
begin
CopyMemory(@TPL, @temp, SizeOf(TPL));
TPL := SubWord(TPL);
CopyMemory(@temp, @TPL, SizeOf(temp));
end;
FW[row, 0] := Byte((Integer(FW[row-FNk, 0])) xor (Integer(temp[0])));
FW[row, 1] := Byte((Integer(FW[row-FNk, 1])) xor (Integer(temp[1])));
FW[row, 2] := Byte((Integer(FW[row-FNk, 2])) xor (Integer(temp[2])));
FW[row, 3] := Byte((Integer(FW[row-FNk, 3])) xor (Integer(temp[3])));
end;
end;procedure TAES.MixColumns;
var
temp: array [0..3, 0..3] of Byte;
r, c: Integer;
begin
for r := 0 to 3 do
for c := 0 to 3 do
temp[r, c] := FState[r, c];
for c := 0 to 3 do
begin
FState[0, c] := Byte((Integer(gfmultby02(temp[0, c]))
xor (Integer(gfmultby03(temp[1, c])))
xor (Integer(gfmultby01(temp[2, c])))
xor (Integer(gfmultby01(temp[3, c])))));
FState[1, c] := Byte((Integer(gfmultby01(temp[0, c]))
xor (Integer(gfmultby02(temp[1, c])))
xor (Integer(gfmultby03(temp[2, c])))
xor (Integer(gfmultby01(temp[3, c])))));
FState[2, c] := Byte((Integer(gfmultby01(temp[0, c]))
xor (Integer(gfmultby01(temp[1, c])))
xor (Integer(gfmultby02(temp[2, c])))
xor (Integer(gfmultby03(temp[3, c])))));
FState[3, c] := Byte((Integer(gfmultby03(temp[0, c]))
xor (Integer(gfmultby01(temp[1, c])))
xor (Integer(gfmultby01(temp[2, c])))
xor (Integer(gfmultby02(temp[3, c])))));
end;
end;function TAES.RotWord(word: LongWord): LongWord;
var
Tmp: array [0..3] of Byte;
B: Byte;
L: LongWord;
begin
CopyMemory(@Tmp, @word, SizeOf(Tmp));
B := Tmp[0];
Tmp[0] := Tmp[1];
Tmp[1] := Tmp[2];
Tmp[2] := Tmp[3];
Tmp[3] := B;
CopyMemory(@L, @Tmp, SizeOf(Tmp));
Result := L;
end;procedure TAES.SetNbNkNr(keySize: TKeySize);
begin
FNb := 4;
case keySize of
Bits128: begin FNk := 4; FNr := 10; end;
Bits192: begin FNk := 6; FNr := 12; end;
Bits256: begin FNk := 8; FNr := 14; end;
end;
end;procedure TAES.ShiftRows;
var
temp: array [0..3, 0..3] of Byte;
r, c: Integer;
begin
for r := 0 to 3 do
for c := 0 to 3 do
temp[r, c] := FState[r, c];
for r := 1 to 3 do
for c := 0 to 3 do
FState[r, c] := temp[r, (c + r) mod FNb];
end;procedure TAES.SubBytes;
var
r, c: Integer;
begin
for r := 0 to 3 do
for c := 0 to 3 do
FState[r, c] := FSBox[FState[r, c] shr 4, FState[r, c] and $0F];
end;function TAES.SubWord(word: LongWord): LongWord;
var
Tmp: array [0..3] of Byte;
L: LongWord;
begin
CopyMemory(@Tmp, @word, SizeOf(Tmp));
Tmp[0] := FSbox[ Tmp[0] shr 4, Tmp[0] and $0f ];
Tmp[1] := FSbox[ Tmp[1] shr 4, Tmp[1] and $0f ];
Tmp[2] := FSbox[ Tmp[2] shr 4, Tmp[2] and $0f ];
Tmp[3] := FSbox[ Tmp[3] shr 4, Tmp[3] and $0f ];
CopyMemory(@L, @Tmp, SizeOf(Tmp));
Result := L;
end;function TAES.CipherStream(input: TStream; output: TStream): Boolean;
var
ai, ao: array [0..15] of Byte;
at: array [0..11] of Byte;
iSize, iRead, iWrite: Integer;
begin
Result := True;
input.Position := 0;
output.Position := 0;
iSize := input.Size;
CopyMemory(@ai, @iSize, SizeOf(iSize));
iRead := input.Read(at, SizeOf(at));
ZeroMemory(@at[iRead], SizeOf(at) - iRead);
CopyMemory(@ai[4], @at, SizeOf(at));
while iRead > 0 do
begin
FCipher(ai, ao);
iWrite := output.Write(ao, SizeOf(ao));
if iWrite <> SizeOf(ao) then Result := False;
iRead := input.Read(ai, SizeOf(ai));
ZeroMemory(@ai[iRead], SizeOf(ai) - iRead);
end;
end;function TAES.InvCipherStream(input: TStream; output: TStream): Boolean;
var
ai, ao: array [0..15] of Byte;
at: array [0..11] of Byte;
iSize, iRead, iWrite: Integer;
begin
Result := True;
input.Position := 0;
output.Position := 0;
iRead := input.Read(ai, SizeOf(ai));
ZeroMemory(@ai[iRead], SizeOf(ai) - iRead);
if iRead > 0 then
begin
FInvCipher(ai, ao);
CopyMemory(@iSize, @ao, SizeOf(iSize));
CopyMemory(@at, @ao[4], SizeOf(at));
iWrite := output.Write(at, SizeOf(at));
if iWrite <> SizeOf(at) then Result := False;
end;
iRead := input.Read(ai, SizeOf(ai));
ZeroMemory(@ai[iRead], SizeOf(ai) - iRead);
while iRead > 0 do
begin
FInvCipher(ai, ao);
iWrite := output.Write(ao, SizeOf(ao));
if iWrite <> SizeOf(ao) then Result := False;
iRead := input.Read(ai, SizeOf(ai));
ZeroMemory(@ai[iRead], SizeOf(ai) - iRead);
end;
output.Size := iSize;
end;function TAES.CiperString(input: string): string;
var
_input, _output: TStringStream;
a: array [0..1023] of Byte;
iRead, i: Integer;
begin
Result := '';
_input := TStringStream.Create(input);
_output := TStringStream.Create('');
try
CipherStream(_input, _output);
_output.Position := 0;
iRead := _output.Read(a, SizeOf(a));
while iRead > 0 do
begin
for i := 0 to iRead - 1 do
Result := Result + Format('%.2X', [a[i]]);
iRead := _output.Read(a, SizeOf(a));
end;
finally
_input.Free;
_output.Free;
end;
end;
汉字是两个字节的,可以取汉字的区位码,区码为ord(s[1]),位码为ord(s[2]),通过互换实现简单加解密下面的代码是汉字和区位码互换的
procedure TForm1.Button1Click(Sender: TObject);
var
qu,wei:integer;
s:string;
begin
s:=edit1.Text;
qu:=ord(s[1]);
wei:=ord(s[2]);
s:='';
if (qu-160)<10 then
s:='0';
s:=s+inttostr(qu-160);
if (wei-160)<10 then
s:=s+'0';
s:=s+inttostr(wei-160)+' or ';
s:=s+inttohex(qu,2)+inttohex(wei,2);
edit2.Text:=s;//汉字转区位码
end;procedure TForm1.Button2Click(Sender: TObject);
var
ln,qu,wei:integer;
ss,s:string;
begin
ss:='$'+edit3.Text;
ln:=strtoint(ss);
s:=inttohex(ln,4);
s:='$'+s;
ln:=strtoint(s);
wei:=ln and $ff;
qu:=ln shr 8;
s:=char( qu )+ char( wei );
edit4.Text:=s;//区位码转汉字
end;
var
_input, _output: TStringStream;
a: Byte;
i, iSize: Integer;begin
Result := '';
_input := TStringStream.Create('');
iSize := Length(input);
for I := 0 to iSize div 2 - 1 do
begin
a := StrToIntDef('$0' + Copy(input, 2 * i + 1, 2), 0);
_input.Write(a, SizeOf(a));
end;
_output := TStringStream.Create('');
try
InvCipherStream(_input, _output);
Result := _output.DataString;
finally
_input.Free;
_output.Free;
end;
end;function TAES.Test(L: LongWord): string;
var
I, J: Integer;
input, output: array [0..15] of Byte;
begin
Result := '';
for I := 0 to 15 do
for J := 0 to 15 do
Result := Result + Format('FSBox(%d, %d) = %d'#13#10, [I, J, FSBox[i, j]]);
for I := 0 to 15 do
for J := 0 to 15 do
Result := Result + Format('FIsBox(%d, %d) = %d'#13#10, [I, J, FIsBox[i, j]]);
for I := 0 to 10 do
for J := 0 to 3 do
Result := Result + Format('FRcon(%d, %d) = %d'#13#10, [I, J, FRcon[i, j]]);
for I := 0 to 31 do
Result := Result + Format('FKey(%d) = %d'#13#10, [i, FKey[i]]);
for I := 0 to 59 do
for J := 0 to 3 do
Result := Result + Format('FW(%d, %d) = %d'#13#10, [I, J, FW[i, j]]);
for I := 0 to 15 do
begin
input[i] := i + $30;
if input[i] > $39 then input[i] := input[i] - 10;
end;
Self.FCipher(input, output);
Result := '';
for I := 0 to 15 do
begin
Result := Result + Format('output(%d) = %.2X'#13#10, [i, output[i]])
end;
//Result := Fstr;
end;end.-------------------
var
AES: TAES;
begin
AES := TAES.Create(Bits192, 'ABCDEFGHIJKLMNOPQRSTUVWX');
ShowMessage(AES.InvCipherString(AES.CiperString('我')));
AES.Free;
end;
function Encrypt(const S: String; Key: Word): String;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result[I] := Char(Byte(S[I]) xor (Key shr 8));
Key := (Byte(Result[I]) + Key) * 11 + 12;
if Result[I] = Chr(0) then
Result[I] := S[I];
end;
Result := StrToHex(Result);
end;function Decrypt(const S: String; Key: Word): String;
var
I: Integer;
S1: String;
begin
S1 := HexToStr(S);
Result := S1;
for I := 1 to Length(S1) do
begin
if Char(Byte(S1[I]) xor (Key shr 8)) = Chr(0) then
begin
Result[I] := S1[I];
Key := (Byte(Chr(0)) + Key) * 11 + 12;
end
else
begin
Result[I] := Char(Byte(S1[I]) xor (Key shr 8));
Key := (Byte(S1[I]) + Key) * 11 + 12;
end;
end;
end;
有太多现成的了,而且,加密的方法,和是不是汉字是无关系的,就我知!如果想简单,用:http://sourceforge.net/projects/tplockbox/
function StrToHex(AStr: String): String;
var
I: Integer;
begin
Result := '';
for I := 1 to Length(AStr) do
begin
Result := Result + Format('%2x', [Byte(AStr[I])]);
end;
I := Pos(' ', Result);
while I <> 0 do
begin
Result[I] := '0';
I:= Pos(' ', Result);
end;
end;function HexToStr(AStr: String): String;
var
I: Integer;
CharValue: Word;
begin
Result := '';
for I := 1 to Trunc(Length(Astr) / 2) do
begin
Result := Result + ' ';
CharValue := TransChar(AStr[2 * I - 1]) * 16 + TransChar(AStr[2 * I]);
Result[I] := Char(CharValue);
end;
end;
编码为Base64即可明文传输
保存起来可能没有Base64的方便
function TransChar(AChar: Char): Integer;
begin
if AChar in ['0'..'9'] then
Result := Ord(AChar) - Ord('0')
else
Result := 10 + Ord(AChar) - Ord('A');
end;