java delphi aes加密算法_谁有C#与delphi通用的AES加密算法

这篇博客详细介绍了如何在Delphi中实现AES加密算法,包括128、192和256位密钥的加密和解密函数,并提供了与Java兼容的AES加密方法,有助于跨平台的加密数据传输。
摘要由CSDN通过智能技术生成

展开全部

unit AES;

interface

uses

SysUtils, Classes, Math, ElAES;

type

TKeyBit = (kb128, kb192, kb256);

function StrToHex(Value: string): string;

function HexToStr(Value: string): string;

function EncryptString(Value: string; Key: string;

KeyBit: TKeyBit = kb128): string;

function DecryptString(Value: string; Key: string;

KeyBit: TKeyBit = kb128): string;

function EncryptStream(Stream: TStream; Key: string;

KeyBit: TKeyBit = kb128): TStream;

function DecryptStream(Stream: TStream; Key: string;

KeyBit: TKeyBit = kb128): TStream;

procedure EncryptFile(SourceFile, DestFile: string;

Key: string; KeyBit: TKeyBit = kb128);

procedure DecryptFile(SourceFile, DestFile: string;

Key: string; KeyBit: TKeyBit = kb128);

implementation

function StrToHex(Value: string): string;

var

I: Integer;

begin

Result := '';

for I := 1 to Length(Value) do

Result := Result + IntToHex(Ord(Value[I]), 2);

end;

function HexToStr(Value: string): string;

var

I: Integer;

begin

Result := '';

for I := 1 to Length(Value) do

begin

if ((I mod 2) = 1) then

Result := Result + Chr(StrToInt('0x'+ Copy(Value, I, 2)));

end;

end;

{  --  字符62616964757a686964616fe4b893e5b19e31333363363438串加密函数 默认按照 128 位密匙加密 --  }

function EncryptString(Value: string; Key: string;

KeyBit: TKeyBit = kb128): string;

var

SS, DS: TStringStream;

Size: Int64;

AESKey128: TAESKey128;

AESKey192: TAESKey192;

AESKey256: TAESKey256;

begin

Result := '';

SS := TStringStream.Create(Value);

DS := TStringStream.Create('');

try

Size := SS.Size;

DS.WriteBuffer(Size, SizeOf(Size));

{  --  128 位密匙最大长度为 16 个字符 --  }

if KeyBit = kb128 then

begin

FillChar(AESKey128, SizeOf(AESKey128), 0 );

Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key)));

EncryptAESStreamECB(SS, 0, AESKey128, DS);

end;

{  --  192 位密匙最大长度为 24 个字符 --  }

if KeyBit = kb192 then

begin

FillChar(AESKey192, SizeOf(AESKey192), 0 );

Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key)));

EncryptAESStreamECB(SS, 0, AESKey192, DS);

end;

{  --  256 位密匙最大长度为 32 个字符 --  }

if KeyBit = kb256 then

begin

FillChar(AESKey256, SizeOf(AESKey256), 0 );

Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));

EncryptAESStreamECB(SS, 0, AESKey256, DS);

end;

Result := StrToHex(DS.DataString);

finally

SS.Free;

DS.Free;

end;

end;

{  --  字符串解密函数 默认按照 128 位密匙解密 --  }

function DecryptString(Value: string; Key: string;

KeyBit: TKeyBit = kb128): string;

var

SS, DS: TStringStream;

Size: Int64;

AESKey128: TAESKey128;

AESKey192: TAESKey192;

AESKey256: TAESKey256;

begin

Result := '';

SS := TStringStream.Create(HexToStr(Value));

DS := TStringStream.Create('');

try

Size := SS.Size;

SS.ReadBuffer(Size, SizeOf(Size));

{  --  128 位密匙最大长度为 16 个字符 --  }

if KeyBit = kb128 then

begin

FillChar(AESKey128, SizeOf(AESKey128), 0 );

Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key)));

DecryptAESStreamECB(SS, SS.Size - SS.Position, AESKey128, DS);

end;

{  --  192 位密匙最大长度为 24 个字符 --  }

if KeyBit = kb192 then

begin

FillChar(AESKey192, SizeOf(AESKey192), 0 );

Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key)));

DecryptAESStreamECB(SS, SS.Size - SS.Position, AESKey192, DS);

end;

{  --  256 位密匙最大长度为 32 个字符 --  }

if KeyBit = kb256 then

begin

FillChar(AESKey256, SizeOf(AESKey256), 0 );

Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));

DecryptAESStreamECB(SS, SS.Size - SS.Position, AESKey256, DS);

end;

Result := DS.DataString;

finally

SS.Free;

DS.Free;

end;

end;

{  --  流加密函数 默认按照 128 位密匙解密 --  }

function EncryptStream(Stream: TStream; Key: string;

KeyBit: TKeyBit = kb128): TStream;

var

Count: Int64;

OutStrm: TStream;

AESKey128: TAESKey128;

AESKey192: TAESKey192;

AESKey256: TAESKey256;

begin

OutStrm := TStream.Create;

Stream.Position := 0;

Count := Stream.Size;

OutStrm.Write(Count, SizeOf(Count));

try

{  --  128 位密匙最大长度为 16 个字符 --  }

if KeyBit = kb128 then

begin

FillChar(AESKey128, SizeOf(AESKey128), 0 );

Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key)));

EncryptAESStreamECB(Stream, 0, AESKey128, OutStrm);

end;

{  --  192 位密匙最大长度为 24 个字符 --  }

if KeyBit = kb192 then

begin

FillChar(AESKey192, SizeOf(AESKey192), 0 );

Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key)));

EncryptAESStreamECB(Stream, 0, AESKey192, OutStrm);

end;

{  --  256 位密匙最大长度为 32 个字符 --  }

if KeyBit = kb256 then

begin

FillChar(AESKey256, SizeOf(AESKey256), 0 );

Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));

EncryptAESStreamECB(Stream, 0, AESKey256, OutStrm);

end;

Result := OutStrm;

finally

OutStrm.Free;

end;

end;

{  --  流解密函数 默认按照 128 位密匙解密 --  }

function DecryptStream(Stream: TStream; Key: string;

KeyBit: TKeyBit = kb128): TStream;

var

Count, OutPos: Int64;

OutStrm: TStream;

AESKey128: TAESKey128;

AESKey192: TAESKey192;

AESKey256: TAESKey256;

begin

OutStrm := TStream.Create;

Stream.Position := 0;

OutPos :=OutStrm.Position;

Stream.ReadBuffer(Count, SizeOf(Count));

try

{  --  128 位密匙最大长度为 16 个字符 --  }

if KeyBit = kb128 then

begin

FillChar(AESKey128, SizeOf(AESKey128), 0 );

Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key)));

DecryptAESStreamECB(Stream, Stream.Size - Stream.Position,

AESKey128, OutStrm);

end;

{  --  192 位密匙最大长度为 24 个字符 --  }

if KeyBit = kb192 then

begin

FillChar(AESKey192, SizeOf(AESKey192), 0 );

Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key)));

DecryptAESStreamECB(Stream, Stream.Size - Stream.Position,

AESKey192, OutStrm);

end;

{  --  256 位密匙最大长度为 32 个字符 --  }

if KeyBit = kb256 then

begin

FillChar(AESKey256, SizeOf(AESKey256), 0 );

Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));

DecryptAESStreamECB(Stream, Stream.Size - Stream.Position,

AESKey256, OutStrm);

end;

OutStrm.Size := OutPos + Count;

OutStrm.Position := OutPos;

Result := OutStrm;

finally

OutStrm.Free;

end;

end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值