当前位置:文档之家› AES加、解密算法 (可破解rar密码)

AES加、解密算法 (可破解rar密码)

AES加、解密算法 (可破解rar密码)
AES加、解密算法


AES 是一种使用安全码进行信息加密的标准。
它支持 128 位、192 位和 256 位的密匙。

加密算法的实现在 ElAES.pas 单元中。
本人将其加密方法封装在 AES.pas 单元中,
只需要调用两个标准函数就可以完成字符串的加密和解密。

(* 密匙长度 *)

128 位支持长度为 16 个字符
192 位支持长度为 24 个字符
256 位支持长度为 32 个字符

所有加密和解密操作在默认情况下为 128 位密匙。

(* 文件列表 *)

..\Source\ AES 单元文件
..\Example\ 演示程序

(* 适用平台 *)

这份 Delphi 的执行基于 FIPS 草案标准,
并且 AES 原作者已经通过了以下平台的测试:

Delphi 4
Delphi 5
C++ Builder 5
Kylix 1

本人又重新进行了补充测试,并顺利通过了以下平台:

Delphi 6
Delphi 7

特别说明:

在 Delphi 3 标准版中进行测试时,因为缺少 Longword 数据类型和
Math.pas 文件,并且不支持 overload 指示字,所以不能正常编译。

(* 演示程序 *)

这个示例程序演示了如何使用 AES 模块进行字符串的加密和解密过程。

(* 使用方法 *)

在程序中引用 AES 单元。

调用函数 EncryptString 和 DecryptString 进行字符串的加密和解密。
调用函数 EncryptStream 和 DecryptStream 进行流的加密和解密。
调用过程 EncryptFile 和 DecryptFile 进行文件的加密和解密。

详细参阅 Example 文件夹中的例子。



(**************************************************)
(* *)
(* Advanced Encryption Standard (AES) *)
(* *)
(* Copyright (c) 1998-2001 *)
(* EldoS, Alexander Ionov *)
(* *)
(**************************************************)

unit ELAES;

interface

uses
Classes, SysUtils;

type
EAESError = class(Exception);

PInteger = ^Integer;

TAESBuffer = array [0..15] of byte;
TAESKey128 = array [0..15] of byte;
TAESKey192 = array [0..23] of byte;
TAESKey256 = array [0..31] of byte;
TAESExpandedKey128 = array [0..43] of longword;
TAESExpandedKey192 = array [0..53] of longword;
TAESExpandedKey256 = array [0..63] of longword;

PAESBuffer =^TAESBuffer;
PAESKey128 =^TAESKey128;
PAESKey192 =^TAESKey192;
PAESKey256 =^TAESKey256;
PAESExpandedKey128 =^TAESExpandedKey128;
PAESExpandedKey192 =^TAESExpandedKey192;
PAESExpandedKey256 =^TAESExpandedKey256;

// Key expansion routines for encryption

procedure ExpandAESKeyForEncryption(const Key: TAESKey128;
var ExpandedKey: TAESExpandedKey128); overload;
procedure ExpandAESKeyForEncryption(const Key: TAESKey192;
var ExpandedKey: TAESExpandedKey192); overload;
procedure ExpandA

ESKeyForEncryption(const Key: TAESKey256;
var ExpandedKey: TAESExpandedKey256); overload;

// Block encryption routines

procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey128;
var OutBuf: TAESBuffer); overload;
procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey192;
var OutBuf: TAESBuffer); overload;
procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey256;
var OutBuf: TAESBuffer); overload;

// Stream encryption routines (ECB mode)

procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
const Key: TAESKey128; Dest: TStream); overload;
procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
const ExpandedKey: TAESExpandedKey128; Dest: TStream); overload;

procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
const Key: TAESKey192; Dest: TStream); overload;
procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
const ExpandedKey: TAESExpandedKey192; Dest: TStream); overload;

procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
const Key: TAESKey256; Dest: TStream); overload;
procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
const ExpandedKey: TAESExpandedKey256; Dest: TStream); overload;

// Stream encryption routines (CBC mode)

procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
const Key: TAESKey128; const InitVector: TAESBuffer; Dest: TStream); overload;
procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
const ExpandedKey: TAESExpandedKey128; const InitVector: TAESBuffer;
Dest: TStream); overload;

procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
const Key: TAESKey192; const InitVector: TAESBuffer; Dest: TStream); overload;
procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
const ExpandedKey: TAESExpandedKey192; const InitVector: TAESBuffer;
Dest: TStream); overload;

procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
const Key: TAESKey256; const InitVector: TAESBuffer; Dest: TStream); overload;
procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
const ExpandedKey: TAESExpandedKey256; const InitVector: TAESBuffer;
Dest: TStream); overload;

// Key transformation routines for decryption

procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey128); overload;
procedure ExpandAESKeyForDecryption(const Key: TAESKey128;
var ExpandedKey: TAESExpandedKey128); overload;

procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey192); overloa


2007-12-12 9:44:45 (**************************************************)
(* *)
(* Advanced Encryption Standard (AES) *)
(* Interface Unit v1.3 *)
(* *)
(* *)
(* Copyright (c) 2002 Jorlen Young

*)
(* *)
(* *)
(* *)
(*说明: *)
(* *)
(* 基于 ElASE.pas 单元封装 *)
(* *)
(* 这是一个 AES 加密算法的标准接口。 *)
(* *)
(* *)
(* 作者:杨泽晖 2004.12.04 *)
(* *)
(* 支持 128 / 192 / 256 位的密匙 *)
(* 默认情况下按照 128 位密匙操作 *)
(* *)
(**************************************************)

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;

{ -- 字符串加密函数 默认按照 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: TA

相关主题
文本预览
相关文档 最新文档