mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-02-06 06:54:58 +00:00
Update AnsiX923Padding.cs, PKCS5Padding.cs
This commit is contained in:
parent
3ed38eec6a
commit
de4016100a
|
@ -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,33 +101,30 @@ namespace WelsonJS.Cryptography
|
|||
// Validate padding length
|
||||
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
|
||||
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)
|
||||
for (int i = data.Length - paddingLength; i < data.Length - 1; i++)
|
||||
{
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
|
|
@ -30,10 +30,22 @@ namespace WelsonJS.Cryptography
|
|||
{
|
||||
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)
|
||||
{
|
||||
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.
|
||||
/// <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)
|
||||
{
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user