init commit

This commit is contained in:
2025-10-09 09:57:24 +09:00
commit 4d551bd74f
6636 changed files with 1218703 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Paddings;
public interface IBlockCipherPadding
{
string PaddingName { get; }
void Init(SecureRandom random);
int AddPadding(byte[] input, int inOff);
int PadCount(byte[] input);
}

View File

@@ -0,0 +1,37 @@
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Paddings;
public class ISO10126d2Padding : IBlockCipherPadding
{
private SecureRandom random;
public string PaddingName => "ISO10126-2";
public void Init(SecureRandom random)
{
this.random = ((random != null) ? random : new SecureRandom());
}
public int AddPadding(byte[] input, int inOff)
{
byte b = (byte)(input.Length - inOff);
while (inOff < input.Length - 1)
{
input[inOff] = (byte)random.NextInt();
inOff++;
}
input[inOff] = b;
return b;
}
public int PadCount(byte[] input)
{
int num = input[^1] & 0xFF;
if (num > input.Length)
{
throw new InvalidCipherTextException("pad block corrupted");
}
return num;
}
}

View File

@@ -0,0 +1,37 @@
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Paddings;
public class ISO7816d4Padding : IBlockCipherPadding
{
public string PaddingName => "ISO7816-4";
public void Init(SecureRandom random)
{
}
public int AddPadding(byte[] input, int inOff)
{
int result = input.Length - inOff;
input[inOff] = 128;
for (inOff++; inOff < input.Length; inOff++)
{
input[inOff] = 0;
}
return result;
}
public int PadCount(byte[] input)
{
int num = input.Length - 1;
while (num > 0 && input[num] == 0)
{
num--;
}
if (input[num] != 128)
{
throw new InvalidCipherTextException("pad block corrupted");
}
return input.Length - num;
}
}

View File

@@ -0,0 +1,151 @@
using System;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Paddings;
public class PaddedBufferedBlockCipher : BufferedBlockCipher
{
private readonly IBlockCipherPadding padding;
public PaddedBufferedBlockCipher(IBlockCipher cipher, IBlockCipherPadding padding)
{
base.cipher = cipher;
this.padding = padding;
buf = new byte[cipher.GetBlockSize()];
bufOff = 0;
}
public PaddedBufferedBlockCipher(IBlockCipher cipher)
: this(cipher, new Pkcs7Padding())
{
}
public override void Init(bool forEncryption, ICipherParameters parameters)
{
base.forEncryption = forEncryption;
SecureRandom random = null;
if (parameters is ParametersWithRandom)
{
ParametersWithRandom parametersWithRandom = (ParametersWithRandom)parameters;
random = parametersWithRandom.Random;
parameters = parametersWithRandom.Parameters;
}
Reset();
padding.Init(random);
cipher.Init(forEncryption, parameters);
}
public override int GetOutputSize(int length)
{
int num = length + bufOff;
int num2 = num % buf.Length;
if (num2 == 0)
{
if (forEncryption)
{
return num + buf.Length;
}
return num;
}
return num - num2 + buf.Length;
}
public override int GetUpdateOutputSize(int length)
{
int num = length + bufOff;
int num2 = num % buf.Length;
if (num2 == 0)
{
return num - buf.Length;
}
return num - num2;
}
public override int ProcessByte(byte input, byte[] output, int outOff)
{
int result = 0;
if (bufOff == buf.Length)
{
result = cipher.ProcessBlock(buf, 0, output, outOff);
bufOff = 0;
}
buf[bufOff++] = input;
return result;
}
public override int ProcessBytes(byte[] input, int inOff, int length, byte[] output, int outOff)
{
if (length < 0)
{
throw new ArgumentException("Can't have a negative input length!");
}
int blockSize = GetBlockSize();
int updateOutputSize = GetUpdateOutputSize(length);
if (updateOutputSize > 0)
{
Check.OutputLength(output, outOff, updateOutputSize, "output buffer too short");
}
int num = 0;
int num2 = buf.Length - bufOff;
if (length > num2)
{
Array.Copy(input, inOff, buf, bufOff, num2);
num += cipher.ProcessBlock(buf, 0, output, outOff);
bufOff = 0;
length -= num2;
inOff += num2;
while (length > buf.Length)
{
num += cipher.ProcessBlock(input, inOff, output, outOff + num);
length -= blockSize;
inOff += blockSize;
}
}
Array.Copy(input, inOff, buf, bufOff, length);
bufOff += length;
return num;
}
public override int DoFinal(byte[] output, int outOff)
{
int blockSize = cipher.GetBlockSize();
int num = 0;
if (forEncryption)
{
if (bufOff == blockSize)
{
if (outOff + 2 * blockSize > output.Length)
{
Reset();
throw new OutputLengthException("output buffer too short");
}
num = cipher.ProcessBlock(buf, 0, output, outOff);
bufOff = 0;
}
padding.AddPadding(buf, bufOff);
num += cipher.ProcessBlock(buf, 0, output, outOff + num);
Reset();
}
else
{
if (bufOff != blockSize)
{
Reset();
throw new DataLengthException("last block incomplete in decryption");
}
num = cipher.ProcessBlock(buf, 0, buf, 0);
bufOff = 0;
try
{
num -= padding.PadCount(buf);
Array.Copy(buf, 0, output, outOff, num);
}
finally
{
Reset();
}
}
return num;
}
}

View File

@@ -0,0 +1,41 @@
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Paddings;
public class Pkcs7Padding : IBlockCipherPadding
{
public string PaddingName => "PKCS7";
public void Init(SecureRandom random)
{
}
public int AddPadding(byte[] input, int inOff)
{
byte b = (byte)(input.Length - inOff);
while (inOff < input.Length)
{
input[inOff] = b;
inOff++;
}
return b;
}
public int PadCount(byte[] input)
{
byte b = input[^1];
int num = b;
if (num < 1 || num > input.Length)
{
throw new InvalidCipherTextException("pad block corrupted");
}
for (int i = 2; i <= num; i++)
{
if (input[^i] != b)
{
throw new InvalidCipherTextException("pad block corrupted");
}
}
return num;
}
}

View File

@@ -0,0 +1,35 @@
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Paddings;
public class TbcPadding : IBlockCipherPadding
{
public string PaddingName => "TBC";
public virtual void Init(SecureRandom random)
{
}
public virtual int AddPadding(byte[] input, int inOff)
{
int result = input.Length - inOff;
byte b = ((inOff <= 0) ? ((byte)(((input[^1] & 1) == 0) ? 255u : 0u)) : ((byte)(((input[inOff - 1] & 1) == 0) ? 255u : 0u)));
while (inOff < input.Length)
{
input[inOff] = b;
inOff++;
}
return result;
}
public virtual int PadCount(byte[] input)
{
byte b = input[^1];
int num = input.Length - 1;
while (num > 0 && input[num - 1] == b)
{
num--;
}
return input.Length - num;
}
}

View File

@@ -0,0 +1,44 @@
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Paddings;
public class X923Padding : IBlockCipherPadding
{
private SecureRandom random;
public string PaddingName => "X9.23";
public void Init(SecureRandom random)
{
this.random = random;
}
public int AddPadding(byte[] input, int inOff)
{
byte b = (byte)(input.Length - inOff);
while (inOff < input.Length - 1)
{
if (random == null)
{
input[inOff] = 0;
}
else
{
input[inOff] = (byte)random.NextInt();
}
inOff++;
}
input[inOff] = b;
return b;
}
public int PadCount(byte[] input)
{
int num = input[^1] & 0xFF;
if (num > input.Length)
{
throw new InvalidCipherTextException("pad block corrupted");
}
return num;
}
}

View File

@@ -0,0 +1,33 @@
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Paddings;
public class ZeroBytePadding : IBlockCipherPadding
{
public string PaddingName => "ZeroBytePadding";
public void Init(SecureRandom random)
{
}
public int AddPadding(byte[] input, int inOff)
{
int result = input.Length - inOff;
while (inOff < input.Length)
{
input[inOff] = 0;
inOff++;
}
return result;
}
public int PadCount(byte[] input)
{
int num = input.Length;
while (num > 0 && input[num - 1] == 0)
{
num--;
}
return input.Length - num;
}
}