diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/Cryptography/AnsiX923Padding.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/Cryptography/AnsiX923Padding.cs
index 86690d9..335ffcb 100644
--- a/WelsonJS.Toolkit/WelsonJS.Toolkit/Cryptography/AnsiX923Padding.cs
+++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/Cryptography/AnsiX923Padding.cs
@@ -48,6 +48,8 @@ namespace WelsonJS.Cryptography
}
byte[] paddedData = new byte[data.Length + paddingLength];
+
+ // Copy original data into the padded array
Array.Copy(data, paddedData, data.Length);
// Fill with 0x00 bytes, and the last byte is the padding length
@@ -99,12 +101,13 @@ namespace WelsonJS.Cryptography
// Validate padding length
if (paddingLength <= 0 || paddingLength > blockSize)
{
- if (ignoreErrors)
+ if (!ignoreErrors)
{
- // Treat padding length as 0 and return the full data
- return data;
+ throw new ArgumentException($"Invalid padding length: {paddingLength}. Must be between 1 and {blockSize}.");
}
- throw new ArgumentException($"Invalid padding length: {paddingLength}. Must be between 1 and {blockSize}.");
+
+ // Treat padding length as 0 and return the full data
+ return data;
}
// Validate the padding region (last paddingLength - 1 bytes must be 0x00)
@@ -112,20 +115,16 @@ namespace WelsonJS.Cryptography
{
if (data[i] != 0x00)
{
- if (ignoreErrors)
+ if (!ignoreErrors)
{
- // Ignore invalid padding and return data up to the detected length
- byte[] fallbackData = new byte[data.Length - paddingLength];
- Array.Copy(data, 0, fallbackData, 0, fallbackData.Length);
- return fallbackData;
+ throw new ArgumentException("Invalid padding detected. Expected padding bytes to be 0x00.");
}
- throw new ArgumentException("Invalid padding detected. Expected padding bytes to be 0x00.");
}
}
// Extract unpadded data
byte[] unpaddedData = new byte[data.Length - paddingLength];
- Array.Copy(data, 0, unpaddedData, 0, unpaddedData.Length);
+ Array.Copy(data, unpaddedData, unpaddedData.Length);
return unpaddedData;
}
diff --git a/WelsonJS.Toolkit/WelsonJS.Toolkit/Cryptography/PKCS5Padding.cs b/WelsonJS.Toolkit/WelsonJS.Toolkit/Cryptography/PKCS5Padding.cs
index cb16f8c..0b914aa 100644
--- a/WelsonJS.Toolkit/WelsonJS.Toolkit/Cryptography/PKCS5Padding.cs
+++ b/WelsonJS.Toolkit/WelsonJS.Toolkit/Cryptography/PKCS5Padding.cs
@@ -30,10 +30,22 @@ namespace WelsonJS.Cryptography
{
public class PKCS5Padding
{
- // Add padding to the data based on the block size.
+ ///
+ /// Add PKCS#5 padding to the input data to make it a multiple of the block size.
+ ///
+ /// The data to be padded.
+ /// The block size to pad to.
+ /// Padded data with PKCS#5 padding.
public static byte[] AddPadding(byte[] data, int blockSize)
{
int paddingLength = blockSize - (data.Length % blockSize);
+
+ // If the data is already a multiple of the block size, no padding is needed
+ if (paddingLength == blockSize)
+ {
+ return data;
+ }
+
byte[] paddedData = new byte[data.Length + paddingLength];
// Copy original data into the padded array
@@ -48,7 +60,14 @@ namespace WelsonJS.Cryptography
return paddedData;
}
- // Remove padding based on the block size.
+ ///
+ /// Removes PKCS#5 padding from the given data.
+ ///
+ /// The input data, including padding.
+ /// The block size used for padding.
+ /// If true, ignores errors and attempts to process the input data as-is.
+ /// The unpadded data as a byte array.
+ /// Thrown if the input data or padding is invalid and ignoreErrors is false.
public static byte[] RemovePadding(byte[] data, int blockSize, bool ignoreErrors = false)
{
// If data length is 0, return empty array
@@ -67,16 +86,14 @@ namespace WelsonJS.Cryptography
int paddingLength = data[data.Length - 1];
// Validate padding length
- if (paddingLength < 1 || paddingLength > blockSize)
+ if (paddingLength <= 0 || paddingLength > blockSize)
{
if (!ignoreErrors)
{
- throw new ArgumentException("Invalid padding length.");
- }
- else
- {
- return data; // Return data as is if error is ignored
+ throw new ArgumentException($"Invalid padding length: {paddingLength}. Must be between 1 and {blockSize}.");
}
+
+ return data;
}
// Check if the padding is correct (i.e., all padding bytes must be equal to paddingLength)
@@ -88,10 +105,6 @@ namespace WelsonJS.Cryptography
{
throw new ArgumentException("Invalid padding detected.");
}
- else
- {
- return data; // Return data as is if error is ignored
- }
}
}