@@ -75,7 +75,7 @@ TPadding = class abstract
75
75
// </para>
76
76
// / </remarks>
77
77
class function AddPadding (const Data : string;
78
- BlockSize : Integer): string; overload; virtual ;
78
+ BlockSize : Integer): string; overload; virtual ; abstract ;
79
79
// <summary>
80
80
// / Adds padding to a raw byte string.
81
81
// / </summary>
@@ -95,7 +95,7 @@ TPadding = class abstract
95
95
// / </para>
96
96
// / </remarks>
97
97
class function AddPadding (const Data : RawByteString;
98
- BlockSize : Integer): RawByteString; overload; virtual ;
98
+ BlockSize : Integer): RawByteString; overload; virtual ; abstract ;
99
99
// / <summary>
100
100
// / Checks if the specified data contains valid padding.
101
101
// / </summary>
@@ -154,7 +154,7 @@ TPadding = class abstract
154
154
// / </para>
155
155
// / </remarks>
156
156
class function RemovePadding (const Data : RawByteString;
157
- BlockSize : Integer): RawByteString; overload; virtual ;
157
+ BlockSize : Integer): RawByteString; overload; virtual ; abstract ;
158
158
// <summary>
159
159
// / Removes padding from a string.
160
160
// / </summary>
@@ -179,7 +179,7 @@ TPadding = class abstract
179
179
// / </para>
180
180
// / </remarks>
181
181
class function RemovePadding (const Data : string;
182
- BlockSize : Integer): string; overload; virtual ;
182
+ BlockSize : Integer): string; overload; virtual ; abstract ;
183
183
end ;
184
184
185
185
// / <summary>
@@ -318,6 +318,60 @@ TFixedBytePadding = class abstract(TPadding)
318
318
// / </returns>
319
319
class function RemovePadding (const Data : TBytes;
320
320
BlockSize : Integer): TBytes; override;
321
+ // <summary>
322
+ // / Removes a fixed byte padding from a raw byte string.
323
+ // / </summary>
324
+ // / <param name="data">
325
+ // / The padded byte raw byte string.
326
+ // / </param>
327
+ // / <param name="BlockSize">
328
+ // / The block size in bytes used for padding.
329
+ // / </param>
330
+ // / <returns>
331
+ // / A new raw byte string with the padding removed. Raises an exception
332
+ // / if the padding is invalid.
333
+ // / </returns>
334
+ // / <exception cref="EDECCipherException">
335
+ // / Raised if the padding is invalid or missing.
336
+ // / </exception>
337
+ // / <remarks>
338
+ // / This function checks for valid a fixed byte padding (depending on the
339
+ // / derrived class) and raises an `EDECCipherException` exception if the
340
+ // / padding is incorrect. This includes cases where the final bytes do not
341
+ // / match the pad count or if the pad count is greater than the block size.
342
+ // / <para>
343
+ // / Call this method after decryption.
344
+ // / </para>
345
+ // / </remarks>
346
+ class function RemovePadding (const Data : RawByteString;
347
+ BlockSize : Integer): RawByteString; override;
348
+ // <summary>
349
+ // / Removes a fixed byte padding from a string.
350
+ // / </summary>
351
+ // / <param name="data">
352
+ // / The padded byte raw byte string.
353
+ // / </param>
354
+ // / <param name="BlockSize">
355
+ // / The block size in bytes used for padding.
356
+ // / </param>
357
+ // / <returns>
358
+ // / A new raw byte string with the padding removed. Raises an exception
359
+ // / if the padding is invalid.
360
+ // / </returns>
361
+ // / <exception cref="EDECCipherException">
362
+ // / Raised if the padding is invalid or missing.
363
+ // / </exception>
364
+ // / <remarks>
365
+ // / This function checks for valid a fixed byte padding (depending on the
366
+ // / derrived class) and raises an `EDECCipherException` exception if the
367
+ // / padding is incorrect. This includes cases where the final bytes do not
368
+ // / match the pad count or if the pad count is greater than the block size.
369
+ // / <para>
370
+ // / Call this method after decryption.
371
+ // / </para>
372
+ // / </remarks>
373
+ class function RemovePadding (const Data : string;
374
+ BlockSize : Integer): string; override;
321
375
end ;
322
376
323
377
// / <summary>
@@ -397,8 +451,7 @@ TPKCS5Padding = class(TPKCS7Padding)
397
451
end ;
398
452
399
453
// / <summary>
400
- // / Implementation of the ANSI X9.23 padding algorithm. This is the more
401
- // / commonly used variant of this algorithm.
454
+ // / Implementation of the ANSI X9.23 padding algorithm.
402
455
// / </summary>
403
456
// / <remarks>
404
457
// / ANSI X9.23 padding is a standard algorithm used in symmetric cryptosystems
@@ -588,6 +641,26 @@ class function TFixedBytePadding.AddPadding(const Data : TBytes;
588
641
Result[I] := GetPaddingByte(PadLength, I = High(Result));
589
642
end ;
590
643
644
+ class function TFixedBytePadding.AddPadding (const Data : string;
645
+ BlockSize : Integer): string;
646
+ var
647
+ Buf: TBytes;
648
+ begin
649
+ Buf := AddPadding(StringToBytes(Data), BlockSize);
650
+ Result := BytesToString(Buf);
651
+ ProtectBytes(Buf);
652
+ end ;
653
+
654
+ class function TFixedBytePadding.AddPadding (const Data : RawByteString;
655
+ BlockSize : Integer): RawByteString;
656
+ var
657
+ Buf: TBytes;
658
+ begin
659
+ Buf := AddPadding(RawStringToBytes(Data), BlockSize);
660
+ Result := BytesToRawString(Buf);
661
+ ProtectBytes(Buf);
662
+ end ;
663
+
591
664
class function TFixedBytePadding.HasValidPadding (const Data : TBytes;
592
665
BlockSize : Integer): Boolean;
593
666
var
@@ -625,75 +698,18 @@ class function TFixedBytePadding.RemovePadding(const Data : TBytes;
625
698
Move(Data[0 ], Result[0 ], Length(Result));
626
699
end ;
627
700
628
- { TANSI_X9_23_Padding }
629
-
630
- class function TANSI_X9_23_Padding.GetPaddingByte (PaddingLength: Integer): UInt8;
631
- begin
632
- Result := 0 ;
633
- end ;
634
-
635
- { TANSI_X9_23_Padding_Legacy }
636
-
637
- class function TANSI_X9_23_Padding_Legacy.AddPadding (const Data : TBytes;
638
- BlockSize : Integer): TBytes;
639
- var
640
- PadLength: Integer;
641
- I: Integer;
642
- begin
643
- PadLength := BlockSize - (Length(Data) mod BlockSize);
644
- SetLength(Result, Length(Data) + PadLength);
645
- if Length(Data) > 0 then
646
- Move(Data[0 ], Result[0 ], Length(Data));
647
-
648
- for I := Length(Data) to High(Result) do
649
- Result[I] := GetPaddingByte(PadLength);
650
- end ;
651
-
652
- class function TANSI_X9_23_Padding_Legacy.GetPaddingByte (PaddingLength: Integer): UInt8;
653
- begin
654
- Result := Random(256 );
655
- end ;
656
-
657
- class function TANSI_X9_23_Padding_Legacy.HasValidPadding (const Data : TBytes;
658
- BlockSize : Integer): Boolean;
659
- var
660
- PadLength : Integer;
661
- begin
662
- if Length(Data) = 0 then
663
- exit(false);
664
-
665
- PadLength := Data[High(Data)];
666
- if (PadLength <= 0 ) or (PadLength > BlockSize) then
667
- exit(false);
668
-
669
- // do not check padding byte, as that one is random for this variant
670
- Result := true;
671
- end ;
672
-
673
- { TPadding }
674
-
675
- class function TPadding.AddPadding (const Data : string;
676
- BlockSize : Integer): string;
677
- var
678
- Buf: TBytes;
679
- begin
680
- Buf := AddPadding(StringToBytes(Data), BlockSize);
681
- Result := BytesToString(Buf);
682
- ProtectBytes(Buf);
683
- end ;
684
-
685
- class function TPadding.AddPadding (const Data : RawByteString;
686
- BlockSize : Integer): RawByteString;
701
+ class function TFixedBytePadding.RemovePadding (const Data : RawByteString;
702
+ BlockSize : Integer): RawByteString;
687
703
var
688
704
Buf: TBytes;
689
705
begin
690
- Buf := AddPadding (RawStringToBytes(Data), BlockSize);
706
+ Buf := RemovePadding (RawStringToBytes(Data), BlockSize);
691
707
Result := BytesToRawString(Buf);
692
708
ProtectBytes(Buf);
693
709
end ;
694
710
695
- class function TPadding .RemovePadding (const Data : string;
696
- BlockSize : Integer): string;
711
+ class function TFixedBytePadding .RemovePadding (const Data : string;
712
+ BlockSize : Integer): string;
697
713
var
698
714
Buf: TBytes;
699
715
begin
0 commit comments