Update AnsiX923Padding.cs, PKCS5Padding.cs
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
Namhyeon Go 2024-12-10 18:31:31 +09:00
parent 3ed38eec6a
commit de4016100a
2 changed files with 35 additions and 23 deletions

View File

@ -48,6 +48,8 @@ namespace WelsonJS.Cryptography
} }
byte[] paddedData = new byte[data.Length + paddingLength]; byte[] paddedData = new byte[data.Length + paddingLength];
// Copy original data into the padded array
Array.Copy(data, paddedData, data.Length); Array.Copy(data, paddedData, data.Length);
// Fill with 0x00 bytes, and the last byte is the padding length // Fill with 0x00 bytes, and the last byte is the padding length
@ -99,33 +101,30 @@ namespace WelsonJS.Cryptography
// Validate padding length // Validate padding length
if (paddingLength <= 0 || paddingLength > blockSize) if (paddingLength <= 0 || paddingLength > blockSize)
{ {
if (ignoreErrors) if (!ignoreErrors)
{ {
throw new ArgumentException($"Invalid padding length: {paddingLength}. Must be between 1 and {blockSize}.");
}
// Treat padding length as 0 and return the full data // Treat padding length as 0 and return the full data
return data; return data;
} }
throw new ArgumentException($"Invalid padding length: {paddingLength}. Must be between 1 and {blockSize}.");
}
// Validate the padding region (last paddingLength - 1 bytes must be 0x00) // Validate the padding region (last paddingLength - 1 bytes must be 0x00)
for (int i = data.Length - paddingLength; i < data.Length - 1; i++) for (int i = data.Length - paddingLength; i < data.Length - 1; i++)
{ {
if (data[i] != 0x00) 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 // Extract unpadded data
byte[] unpaddedData = new byte[data.Length - paddingLength]; byte[] unpaddedData = new byte[data.Length - paddingLength];
Array.Copy(data, 0, unpaddedData, 0, unpaddedData.Length); Array.Copy(data, unpaddedData, unpaddedData.Length);
return unpaddedData; return unpaddedData;
} }

View File

@ -30,10 +30,22 @@ namespace WelsonJS.Cryptography
{ {
public class PKCS5Padding public class PKCS5Padding
{ {
// Add padding to the data based on the block size. /// <summary>
/// Add PKCS#5 padding to the input data to make it a multiple of the block size.
/// </summary>
/// <param name="data">The data to be padded.</param>
/// <param name="blockSize">The block size to pad to.</param>
/// <returns>Padded data with PKCS#5 padding.</returns>
public static byte[] AddPadding(byte[] data, int blockSize) public static byte[] AddPadding(byte[] data, int blockSize)
{ {
int paddingLength = blockSize - (data.Length % 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]; byte[] paddedData = new byte[data.Length + paddingLength];
// Copy original data into the padded array // Copy original data into the padded array
@ -48,7 +60,14 @@ namespace WelsonJS.Cryptography
return paddedData; return paddedData;
} }
// Remove padding based on the block size. /// <summary>
/// Removes PKCS#5 padding from the given data.
/// </summary>
/// <param name="data">The input data, including padding.</param>
/// <param name="blockSize">The block size used for padding.</param>
/// <param name="ignoreErrors">If true, ignores errors and attempts to process the input data as-is.</param>
/// <returns>The unpadded data as a byte array.</returns>
/// <exception cref="ArgumentException">Thrown if the input data or padding is invalid and ignoreErrors is false.</exception>
public static byte[] RemovePadding(byte[] data, int blockSize, bool ignoreErrors = false) public static byte[] RemovePadding(byte[] data, int blockSize, bool ignoreErrors = false)
{ {
// If data length is 0, return empty array // If data length is 0, return empty array
@ -67,16 +86,14 @@ namespace WelsonJS.Cryptography
int paddingLength = data[data.Length - 1]; int paddingLength = data[data.Length - 1];
// Validate padding length // Validate padding length
if (paddingLength < 1 || paddingLength > blockSize) if (paddingLength <= 0 || paddingLength > blockSize)
{ {
if (!ignoreErrors) if (!ignoreErrors)
{ {
throw new ArgumentException("Invalid padding length."); throw new ArgumentException($"Invalid padding length: {paddingLength}. Must be between 1 and {blockSize}.");
}
else
{
return data; // Return data as is if error is ignored
} }
return data;
} }
// Check if the padding is correct (i.e., all padding bytes must be equal to paddingLength) // 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."); throw new ArgumentException("Invalid padding detected.");
} }
else
{
return data; // Return data as is if error is ignored
}
} }
} }