在这工作了3天后,我终于让它工作了。关键是完全消除字符串的使用,只使用 DCPCrypt 中基于 TBytes 的例程。下面的代码是一个测试程序,用于测试 DCPCrypt 支持的所有不同的链接模式。我还添加了一个函数来实现我在这里找到的四种填充模式(用于 CBC 和 ECB):
https://en.wikipedia.org/wiki/Padding_(cryptography)
以及零填充和随机填充。我选择不使用 DCPCrypt 自己的 Base64 函数,因为它们与 FMX 不兼容。相反,我使用 System.NetEncoding 单元中的对应项。拜托,我认为自己只是一个普通的程序员,所以我希望你们中真正的德尔福奇才能够找到很多批评。但没关系。如果给出良好的反馈,我会修改代码。就像现在一样,代码工作并产生与 PHP 的 openssl 函数兼容的结果(使用 CTR 模式测试)。我只是在这里发布这个,希望它对寻找与我相同的解决方案的人有用。
unit MainUnit;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.NetEncoding,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Layouts,
FMX.ScrollBox, FMX.Memo, FMX.Edit, FMX.Controls.Presentation,
DCPcrypt2, DCPsha256, DCPblockciphers, DCPrijndael;
type
TChainingMode = (cmCBC, cmCFB8bit, cmCFBblock, cmOFB, cmCTR, cmECB);
TPaddingMode = (pmZeroPadding, pmANSIX923, pmISO10126, pmISO7816, pmPKCS7, pmRandomPadding);
type
TMainForm = class(TForm)
ScrollBox: TScrollBox;
KeySizeLabel: TLabel;
ChainingLabel: TLabel;
EncodingLabel: TLabel;
PaddingLabel: TLabel;
KeyLabel: TLabel;
InitVectorLabel: TLabel;
DataLabel: TLabel;
DecryptedLabel: TLabel;
CipherLabel: TLabel;
EncryptedLabel: TLabel;
rbRijndael: TRadioButton;
rb128bit: TRadioButton;
rb256bit: TRadioButton;
rbANSI: TRadioButton;
rbUTF8: TRadioButton;
rbUnicode: TRadioButton;
rbCBC: TRadioButton;
rbOFB: TRadioButton;
rbCTR: TRadioButton;
rbECB: TRadioButton;
rbCFB8bit: TRadioButton;
rbCFBblock: TRadioButton;
rbZeroPadding: TRadioButton;
rbANSIX923: TRadioButton;
rbISO10126: TRadioButton;
rbISO7816: TRadioButton;
rbPKCS7: TRadioButton;
rbRandomPadding: TRadioButton;
KeyEdit: TEdit;
InitVectorEdit: TEdit;
DataMemo: TMemo;
EncryptedMemo: TMemo;
DecryptedMemo: TMemo;
EncryptButton: TButton;
DecryptButton: TButton;
procedure FormCreate(Sender: TObject);
procedure EncryptButtonClick(Sender: TObject);
procedure DecryptButtonClick(Sender: TObject);
public
procedure GetOptions(var Key: TBytes; var KeySize: integer; var InitVector: TBytes;
var Encoding: TEncoding; var ChainingMode: TChainingMode; var PaddingMode: TPaddingMode);
end;
var
MainForm: TMainForm;
implementation
{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}
function BytesToHex(B: TBytes): string;
var
I: integer;
begin
Result := '';
for I := Low(B) to High(B) do Result := Result + IntToHex(B[I]) + ' ';
end;
procedure BytePadding(var Data: TBytes; BlockSize: integer; PaddingMode: TPaddingMode);
// Supports: ANSI X.923, ISO 10126, ISO 7816, PKCS7, zero padding and random padding
var
I, DataBlocks, DataLength, PaddingStart, PaddingCount: integer;
begin
BlockSize := BlockSize div 8; // convert bits to bytes
// Zero and Random padding do not use end-markers, so if Length(Data) is a multiple of BlockSize, no padding is needed
if PaddingMode in [pmZeroPadding, pmRandomPadding] then
if Length(Data) mod BlockSize = 0 then Exit;
DataBlocks := (Length(Data) div BlockSize) + 1;
DataLength := DataBlocks * BlockSize;
PaddingCount := DataLength - Length(Data);
// ANSIX923, ISO10126 and PKCS7 store the padding length in a 1 byte end-marker, so any padding length > $FF is not supported
if PaddingMode in [pmANSIX923, pmISO10126, pmPKCS7] then
if PaddingCount > $FF then Exit;
PaddingStart := Length(Data);
SetLength(Data, DataLength);
case PaddingMode of
pmZeroPadding, pmANSIX923, pmISO7816: // fill with $00 bytes
FillChar(Data[PaddingStart], PaddingCount, 0);
pmPKCS7: // fill with PaddingCount bytes
FillChar(Data[PaddingStart], PaddingCount, PaddingCount);
pmRandomPadding, pmISO10126: // fill with random bytes
for I := PaddingStart to DataLength-1 do Data[I] := Random($FF);
end;
case PaddingMode of
pmANSIX923, pmISO10126:
Data[DataLength-1] := PaddingCount; // set end-marker with number of bytes added
pmISO7816:
Data[PaddingStart] := $80; // set fixed end-markder $80
end;
end;
procedure EncryptAES(const Data: TBytes; var Crypt: TBytes; const Key: TBytes; KeySize: integer;
const InitVector: TBytes; ChainingMode: TChainingMode; PaddingMode: TPaddingMode); overload;
var
Cipher: TDCP_rijndael;
begin
Cipher := TDCP_rijndael.Create(nil);
try
Cipher.Init(Key[0], KeySize, @InitVector[0]);
// Copy Data => Crypt
Crypt := Copy(Data, 0, Length(Data));
// Padd Crypt to required length (for Block based algorithms)
if ChainingMode in [cmCBC, cmECB] then
BytePadding(Crypt, Cipher.BlockSize, PaddingMode);
// Encrypt Crypt using the algorithm specified in ChainingMode
case ChainingMode of
cmCBC: Cipher.EncryptCBC(Crypt[0], Crypt[0], Length(Crypt));
cmCFB8bit: Cipher.EncryptCFB8bit(Crypt[0], Crypt[0], Length(Crypt));
cmCFBblock: Cipher.EncryptCFBblock(Crypt[0], Crypt[0], Length(Crypt));
cmOFB: Cipher.EncryptOFB(Crypt[0], Crypt[0], Length(Crypt));
cmCTR: Cipher.EncryptCTR(Crypt[0], Crypt[0], Length(Crypt));
cmECB: Cipher.EncryptECB(Crypt[0], Crypt[0]);
end;
finally
Cipher.Free;
end;
end;
procedure DecryptAES(const Crypt: TBytes; var Data: TBytes; const Key: TBytes; KeySize: integer;
const InitVector: TBytes; ChainingMode: TChainingMode; PaddingMode: TPaddingMode); overload;
var
Cipher: TDCP_rijndael;
I: integer;
begin
Cipher := TDCP_rijndael.Create(nil);
try
Cipher.Init(Key[0], KeySize, @InitVector[0]);
// Copy Crypt => Data
Data := Copy(Crypt, 0, Length(Crypt));
// Decrypt Data using the algorithm specified in ChainingMode
case ChainingMode of
cmCBC: Cipher.DecryptCBC(Data[0], Data[0], Length(Data));
cmCFB8bit: Cipher.DecryptCFB8bit(Data[0], Data[0], Length(Data));
cmCFBblock: Cipher.DecryptCFBblock(Data[0], Data[0], Length(Data));
cmOFB: Cipher.DecryptOFB(Data[0], Data[0], Length(Data));
cmCTR: Cipher.DecryptCTR(Data[0], Data[0], Length(Data));
cmECB: Cipher.DecryptECB(Data[0], Data[0]);
end;
// Correct the length of Data, based on the used PaddingMode (only for Block based algorithms)
if ChainingMode in [cmCBC, cmECB] then
case PaddingMode of
pmANSIX923, pmISO10126, pmPKCS7: // these modes store the original Padding count in the last byte
SetLength(Data, Length(Data) - Data[Length(Data)-1]);
pmISO7816: // this mode uses a fixed end-marker. Find it and correct length accordingly.
for I := Length(Data)-1 downto 0 do
if Data[I] = $80 then
begin
SetLength(Data, I);
Break;
end;
end;
finally
Cipher.Free;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
EncryptedMemo.Lines.Clear;
DecryptedMemo.Lines.Clear;
end;
procedure TMainForm.GetOptions(var Key: TBytes; var KeySize: integer; var InitVector: TBytes;
var Encoding: TEncoding; var ChainingMode: TChainingMode; var PaddingMode: TPaddingMode);
begin
KeySize := 256;
Encoding := TEncoding.ANSI;
ChainingMode := cmCBC;
PaddingMode := pmPKCS7;
if rb128bit.IsChecked then KeySize := 128;
if rb256bit.IsChecked then KeySize := 256;
if rbCBC.IsChecked then ChainingMode := cmCBC;
if rbCFB8bit.IsChecked then ChainingMode := cmCFB8bit;
if rbCFBblock.IsChecked then ChainingMode := cmCFBblock;
if rbOFB.IsChecked then ChainingMode := cmOFB;
if rbCTR.IsChecked then ChainingMode := cmCTR;
if rbECB.IsChecked then ChainingMode := cmECB;
if rbZeroPadding.IsChecked then PaddingMode := pmZeroPadding;
if rbANSIX923.IsChecked then PaddingMode := pmANSIX923;
if rbISO10126.IsChecked then PaddingMode := pmISO10126;
if rbISO7816.IsChecked then PaddingMode := pmISO7816;
if rbPKCS7.IsChecked then PaddingMode := pmPKCS7;
if rbRandomPadding.IsChecked then PaddingMode := pmRandomPadding;
if rbANSI.IsChecked then Encoding := TEncoding.ANSI;
if rbUTF8.IsChecked then Encoding := TEncoding.UTF8;
if rbUnicode.IsChecked then Encoding := TEncoding.Unicode;
Key := Encoding.GetBytes(KeyEdit.Text);
InitVector := Encoding.GetBytes(InitVectorEdit.Text);
end;
procedure TMainForm.EncryptButtonClick(Sender: TObject);
var
Keysize: integer;
Encoding: TEncoding;
ChainingMode: TChainingMode;
PaddingMode: TPaddingMode;
Key, InitVector, Data, Crypt: TBytes;
begin
GetOptions(Key, KeySize, InitVector, Encoding, ChainingMode, PaddingMode);
Data := Encoding.GetBytes(DataMemo.Text);
EncryptAES(Data, Crypt, Key, KeySize, InitVector, ChainingMode, PaddingMode);
EncryptedMemo.Text := TNetEncoding.Base64.EncodeBytesToString(Crypt);
end;
procedure TMainForm.DecryptButtonClick(Sender: TObject);
var
Keysize: integer;
Encoding: TEncoding;
ChainingMode: TChainingMode;
PaddingMode: TPaddingMode;
Key, InitVector, Data, Crypt: TBytes;
begin
GetOptions(Key, KeySize, InitVector, Encoding, ChainingMode, PaddingMode);
Crypt := TNetEncoding.Base64.DecodeStringToBytes(EncryptedMemo.Text);
DecryptAES(Crypt, Data, Key, KeySize, InitVector, ChainingMode, PaddingMode);
DecryptedMemo.Text := Encoding.GetString(Data);
end;
end.