1 (**************************************************) 2 (* *) 3 (* Advanced Encryption Standard (AES) *) 4 (* Interface Unit v1.3 *) 5 (* *) 6 (* *) 7 (* Copyright (c) 2002 Jorlen Young *) 8 (* *) 9 (* *) 10 (* *) 11 (*说明: *) 12 (* *) 13 (* 基于 ElASE.pas 单元封装 *) 14 (* *) 15 (* 这是一个 AES 加密算法的标准接口。 *) 16 (* *) 17 (* *) 18 (* 19 (* *) 20 (* 支持 128 / 192 / 256 位的密匙 *) 21 (* 默认情况下按照 128 位密匙操作 *) 22 (* *) 23 (**************************************************) 24 25 unit AES; 26 27 interface 28 29 uses 30 SysUtils, Classes, Math, ElAES; 31 32 type 33 TKeyBit = (kb128, kb192, kb256); 34 35 function StrToHex(Value: string): string; 36 function HexToStr(Value: string): string; 37 function EncryptString(Value: string; Key: string; 38 KeyBit: TKeyBit = kb128): string; 39 function DecryptString(Value: string; Key: string; 40 KeyBit: TKeyBit = kb128): string; 41 function EncryptStream(Stream: TStream; Key: string; 42 KeyBit: TKeyBit = kb128): TStream; 43 function DecryptStream(Stream: TStream; Key: string; 44 KeyBit: TKeyBit = kb128): TStream; 45 procedure EncryptFile(SourceFile, DestFile: string; 46 Key: string; KeyBit: TKeyBit = kb128); 47 procedure DecryptFile(SourceFile, DestFile: string; 48 Key: string; KeyBit: TKeyBit = kb128); 49 50 implementation 51 52 function StrToHex(Value: string): string; 53 var 54 I: Integer; 55 begin 56 Result := ''; 57 for I := 1 to Length(Value) do 58 Result := Result + IntToHex(Ord(Value[I]), 2); 59 end; 60 61 function HexToStr(Value: string): string; 62 var 63 I: Integer; 64 begin 65 Result := ''; 66 for I := 1 to Length(Value) do 67 begin 68 if ((I mod 2) = 1) then 69 Result := Result + Chr(StrToInt('0x'+ Copy(Value, I, 2))); 70 end; 71 end; 72 73 { -- 字符串加密函数 默认按照 128 位密匙加密 -- } 74 function EncryptString(Value: string; Key: string; 75 KeyBit: TKeyBit = kb128): string; 76 var 77 SS, DS: TStringStream; 78 Size: Int64; 79 AESKey128: TAESKey128; 80 AESKey192: TAESKey192; 81 AESKey256: TAESKey256; 82 begin 83 Result := ''; 84 if Value = '' then Exit; 85 SS := TStringStream.Create(Value); 86 DS := TStringStream.Create(''); 87 try 88 Size := SS.Size; 89 DS.WriteBuffer(Size, SizeOf(Size)); 90 { -- 128 位密匙最大长度为 16 个字符 -- } 91 if KeyBit = kb128 then 92 begin 93 FillChar(AESKey128, SizeOf(AESKey128), 0 ); 94 Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key))); 95 EncryptAESStreamECB(SS, 0, AESKey128, DS); 96 end; 97 { -- 192 位密匙最大长度为 24 个字符 -- } 98 if KeyBit = kb192 then 99 begin 100 FillChar(AESKey192, SizeOf(AESKey192), 0 ); 101 Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key))); 102 EncryptAESStreamECB(SS, 0, AESKey192, DS); 103 end; 104 { -- 256 位密匙最大长度为 32 个字符 -- } 105 if KeyBit = kb256 then 106 begin 107 FillChar(AESKey256, SizeOf(AESKey256), 0 ); 108 Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key))); 109 EncryptAESStreamECB(SS, 0, AESKey256, DS); 110 end; 111 Result := StrToHex(DS.DataString); 112 finally 113 SS.Free; 114 DS.Free; 115 end; 116 end; 117 118 { -- 字符串解密函数 默认按照 128 位密匙解密 -- } 119 function DecryptString(Value: string; Key: string; 120 KeyBit: TKeyBit = kb128): string; 121 var 122 SS, DS: TStringStream; 123 Size: Int64; 124 AESKey128: TAESKey128; 125 AESKey192: TAESKey192; 126 AESKey256: TAESKey256; 127 begin 128 Result := ''; 129 if Value = '' then Exit; 130 SS := TStringStream.Create(HexToStr(Value)); 131 DS := TStringStream.Create(''); 132 try 133 Size := SS.Size; 134 SS.ReadBuffer(Size, SizeOf(Size)); 135 { -- 128 位密匙最大长度为 16 个字符 -- } 136 if KeyBit = kb128 then 137 begin 138 FillChar(AESKey128, SizeOf(AESKey128), 0 ); 139 Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key))); 140 DecryptAESStreamECB(SS, SS.Size - SS.Position, AESKey128, DS); 141 end; 142 { -- 192 位密匙最大长度为 24 个字符 -- } 143 if KeyBit = kb192 then 144 begin 145 FillChar(AESKey192, SizeOf(AESKey192), 0 ); 146 Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key))); 147 DecryptAESStreamECB(SS, SS.Size - SS.Position, AESKey192, DS); 148 end; 149 { -- 256 位密匙最大长度为 32 个字符 -- } 150 if KeyBit = kb256 then 151 begin 152 FillChar(AESKey256, SizeOf(AESKey256), 0 ); 153 Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key))); 154 DecryptAESStreamECB(SS, SS.Size - SS.Position, AESKey256, DS); 155 end; 156 Result := DS.DataString; 157 finally 158 SS.Free; 159 DS.Free; 160 end; 161 end; 162 163 { -- 流加密函数 默认按照 128 位密匙解密 -- } 164 function EncryptStream(Stream: TStream; Key: string; 165 KeyBit: TKeyBit = kb128): TStream; 166 var 167 Count: Int64; 168 OutStrm: TStream; 169 AESKey128: TAESKey128; 170 AESKey192: TAESKey192; 171 AESKey256: TAESKey256; 172 begin 173 OutStrm := TStream.Create; 174 Stream.Position := 0; 175 Count := Stream.Size; 176 OutStrm.Write(Count, SizeOf(Count)); 177 try 178 { -- 128 位密匙最大长度为 16 个字符 -- } 179 if KeyBit = kb128 then 180 begin 181 FillChar(AESKey128, SizeOf(AESKey128), 0 ); 182 Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key))); 183 EncryptAESStreamECB(Stream, 0, AESKey128, OutStrm); 184 end; 185 { -- 192 位密匙最大长度为 24 个字符 -- } 186 if KeyBit = kb192 then 187 begin 188 FillChar(AESKey192, SizeOf(AESKey192), 0 ); 189 Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key))); 190 EncryptAESStreamECB(Stream, 0, AESKey192, OutStrm); 191 end; 192 { -- 256 位密匙最大长度为 32 个字符 -- } 193 if KeyBit = kb256 then 194 begin 195 FillChar(AESKey256, SizeOf(AESKey256), 0 ); 196 Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key))); 197 EncryptAESStreamECB(Stream, 0, AESKey256, OutStrm); 198 end; 199 Result := OutStrm; 200 finally 201 OutStrm.Free; 202 end; 203 end; 204 205 { -- 流解密函数 默认按照 128 位密匙解密 -- } 206 function DecryptStream(Stream: TStream; Key: string; 207 KeyBit: TKeyBit = kb128): TStream; 208 var 209 Count, OutPos: Int64; 210 OutStrm: TStream; 211 AESKey128: TAESKey128; 212 AESKey192: TAESKey192; 213 AESKey256: TAESKey256; 214 begin 215 OutStrm := TStream.Create; 216 Stream.Position := 0; 217 OutPos :=OutStrm.Position; 218 Stream.ReadBuffer(Count, SizeOf(Count)); 219 try 220 { -- 128 位密匙最大长度为 16 个字符 -- } 221 if KeyBit = kb128 then 222 begin 223 FillChar(AESKey128, SizeOf(AESKey128), 0 ); 224 Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key))); 225 DecryptAESStreamECB(Stream, Stream.Size - Stream.Position, 226 AESKey128, OutStrm); 227 end; 228 { -- 192 位密匙最大长度为 24 个字符 -- } 229 if KeyBit = kb192 then 230 begin 231 FillChar(AESKey192, SizeOf(AESKey192), 0 ); 232 Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key))); 233 DecryptAESStreamECB(Stream, Stream.Size - Stream.Position, 234 AESKey192, OutStrm); 235 end; 236 { -- 256 位密匙最大长度为 32 个字符 -- } 237 if KeyBit = kb256 then 238 begin 239 FillChar(AESKey256, SizeOf(AESKey256), 0 ); 240 Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key))); 241 DecryptAESStreamECB(Stream, Stream.Size - Stream.Position, 242 AESKey256, OutStrm); 243 end; 244 OutStrm.Size := OutPos + Count; 245 OutStrm.Position := OutPos; 246 Result := OutStrm; 247 finally 248 OutStrm.Free; 249 end; 250 end; 251 252 { -- 文件加密函数 默认按照 128 位密匙解密 -- } 253 procedure EncryptFile(SourceFile, DestFile: string; 254 Key: string; KeyBit: TKeyBit = kb128); 255 var 256 SFS, DFS: TFileStream; 257 Size: Int64; 258 AESKey128: TAESKey128; 259 AESKey192: TAESKey192; 260 AESKey256: TAESKey256; 261 begin 262 SFS := TFileStream.Create(SourceFile, fmOpenRead); 263 try 264 DFS := TFileStream.Create(DestFile, fmCreate); 265 try 266 Size := SFS.Size; 267 DFS.WriteBuffer(Size, SizeOf(Size)); 268 { -- 128 位密匙最大长度为 16 个字符 -- } 269 if KeyBit = kb128 then 270 begin 271 FillChar(AESKey128, SizeOf(AESKey128), 0 ); 272 Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key))); 273 EncryptAESStreamECB(SFS, 0, AESKey128, DFS); 274 end; 275 { -- 192 位密匙最大长度为 24 个字符 -- } 276 if KeyBit = kb192 then 277 begin 278 FillChar(AESKey192, SizeOf(AESKey192), 0 ); 279 Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key))); 280 EncryptAESStreamECB(SFS, 0, AESKey192, DFS); 281 end; 282 { -- 256 位密匙最大长度为 32 个字符 -- } 283 if KeyBit = kb256 then 284 begin 285 FillChar(AESKey256, SizeOf(AESKey256), 0 ); 286 Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key))); 287 EncryptAESStreamECB(SFS, 0, AESKey256, DFS); 288 end; 289 finally 290 DFS.Free; 291 end; 292 finally 293 SFS.Free; 294 end; 295 end; 296 297 { -- 文件解密函数 默认按照 128 位密匙解密 -- } 298 procedure DecryptFile(SourceFile, DestFile: string; 299 Key: string; KeyBit: TKeyBit = kb128); 300 var 301 SFS, DFS: TFileStream; 302 Size: Int64; 303 AESKey128: TAESKey128; 304 AESKey192: TAESKey192; 305 AESKey256: TAESKey256; 306 begin 307 SFS := TFileStream.Create(SourceFile, fmOpenRead); 308 try 309 SFS.ReadBuffer(Size, SizeOf(Size)); 310 DFS := TFileStream.Create(DestFile, fmCreate); 311 try 312 { -- 128 位密匙最大长度为 16 个字符 -- } 313 if KeyBit = kb128 then 314 begin 315 FillChar(AESKey128, SizeOf(AESKey128), 0 ); 316 Move(PChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key))); 317 DecryptAESStreamECB(SFS, SFS.Size - SFS.Position, AESKey128, DFS); 318 end; 319 { -- 192 位密匙最大长度为 24 个字符 -- } 320 if KeyBit = kb192 then 321 begin 322 FillChar(AESKey192, SizeOf(AESKey192), 0 ); 323 Move(PChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key))); 324 DecryptAESStreamECB(SFS, SFS.Size - SFS.Position, AESKey192, DFS); 325 end; 326 { -- 256 位密匙最大长度为 32 个字符 -- } 327 if KeyBit = kb256 then 328 begin 329 FillChar(AESKey256, SizeOf(AESKey256), 0 ); 330 Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key))); 331 DecryptAESStreamECB(SFS, SFS.Size - SFS.Position, AESKey256, DFS); 332 end; 333 DFS.Size := Size; 334 finally 335 DFS.Free; 336 end; 337 finally 338 SFS.Free; 339 end; 340 end; 341 end.
相关文章: