我有一个抽象密码箱加密组件的类层次结构。具体来说,我们对带有 PKCS#5 填充的 AES-265 感兴趣。
使用 CBC 正确设置了类实例,并将编码显式设置为 ANSI,但结果输出是加扰的,这意味着填充已关闭。唉,CBC 支持不止一种填充方案,我认为 Lockbox 没有使用我们需要的填充方案。
你有什么想法我哪里出错了吗?
这是类层次结构的相关代码:
TsmEncryptBase = class(TInterfacedObject, IsmEncryption)
private
FLib: TCryptographicLibrary;
protected
FCodec: TCodec;
function Encrypt: Boolean; virtual; abstract;
function Decrypt: Boolean; virtual; abstract;
public
constructor Create(const APassword: string;
const aCipherId: string = 'native.AES-256';
const aChainModeId:string = 'native.CBC');
destructor Destroy; override;
end;
constructor TsmEncryptBase.Create(const APassword: string; const aCipherId:
string; const aChainModeId: string);
begin
inherited Create;
FLib := TCryptographicLibrary.Create(nil);
// FLib.RegisterBlockChainingModel( TPure_ECB.Create as IBlockChainingModel);
FCodec := TCodec.Create(nil);
FCodec.CryptoLibrary := FLib;
FCodec.StreamCipherId := uTPLb_Constants.BlockCipher_ProgId;
FCodec.BlockCipherId := aCipherId;
FCodec.ChainModeId := uTPLb_Constants.CBC_ProgId;
FCodec.Password := APassword;
end;
TsmFileEncryptAES = class(TsmEncryptBase)
private
FPlainTextFileName: string;
FEncryptedFileName: string;
protected
function Encrypt: boolean; override;
function Decrypt: Boolean; override;
public
constructor Create(const APlainTextFileName, AEncryptedFileName,
APassword: string);
end;
constructor TsmFileEncryptAES.Create(const APlainTextFileName, AEncryptedFileName,
APassword: string);
begin
inherited Create(APassword);
FPlainTextFileName := APlainTextFileName;
FEncryptedFileName := AEncryptedFileName;
FCodec.Encoding := TEncoding.ANSI;
end;
代码是这样使用的:
procedure TForm1.AESFileDecryptClick(Sender: TObject);
var lEncrypt: IsmEncryption;
begin
lEncrypt := TsmFileEncryptAES.Create(AESFileSaveTo.AsString,
AESSourceFile.AsString, AESFileKey.AsString);
lEncrypt.Decrypt;
end;
有任何想法吗?