Skip to content

Commit 00bb24c

Browse files
Corrected padding algorithm TANSI_X9_23_Padding, TISO10126Padding, and TISO7816Padding: If the data length is a multiply of block size an additional padding is added (identical to PKCS7/PKCS5).
1 parent 61f0007 commit 00bb24c

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

Source/DECCipherFormats.pas

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream;
784784
StartPos := Pos;
785785
doPadding := false;
786786
doStartOnlyPadding := (DataSize = 0) and IsEncode and
787-
(FPaddingMode in [pmPKCS5, pmPKCS7]);
787+
(FPaddingMode <> pmNone);
788788

789789
if (DataSize > 0) or doStartOnlyPadding then
790790
begin
@@ -817,7 +817,7 @@ procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream;
817817
doAdjustBuffer := Bytes > DataSize;
818818
Bytes := DataSize;
819819
// Handle padding mode
820-
doPadding := FPaddingMode <> pmNone;
820+
doPadding := (FPaddingMode <> pmNone) and assigned(FPaddingCLass);
821821
if doPadding and doAdjustBuffer then
822822
if IsEncode then
823823
begin
@@ -836,9 +836,7 @@ procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream;
836836

837837
if IsEncode and doPadding then
838838
begin
839-
if not (FPaddingCLass = nil) then
840-
Buffer := FPaddingClass.AddPadding(Buffer, Context.BlockSize);
841-
839+
Buffer := FPaddingClass.AddPadding(Buffer, Context.BlockSize);
842840
Bytes := length(Buffer);
843841
SetLength(outBuffer, length(Buffer));
844842
end;
@@ -847,9 +845,7 @@ procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream;
847845
CipherProc(Buffer[0], outBuffer[0], Bytes);
848846
if not IsEncode and doPadding then
849847
begin
850-
if not (FPaddingCLass = nil) then
851-
outBuffer := FPaddingCLass.RemovePadding(outBuffer, Context.BlockSize);
852-
848+
outBuffer := FPaddingCLass.RemovePadding(outBuffer, Context.BlockSize);
853849
Bytes := length(outBuffer);
854850
end;
855851
if Bytes > 0 then
@@ -1034,7 +1030,7 @@ function TDECFormattedCipher.DecodeStringToBytes(const Source: RawByteString; Fo
10341030
SetLength(Result, 0);
10351031

10361032
if not (FPaddingCLass = nil) then
1037-
Result := FpaddingClass.RemovePadding(Result, Context.BlockSize);
1033+
Result := FPaddingClass.RemovePadding(Result, Context.BlockSize);
10381034
end;
10391035

10401036
{$IFDEF ANSISTRINGSUPPORTED}

0 commit comments

Comments
 (0)