You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.3 KiB
58 lines
1.3 KiB
2 months ago
|
using System;
|
||
|
|
||
|
using Org.BouncyCastle.Crypto.Parameters;
|
||
|
|
||
|
namespace Org.BouncyCastle.Crypto.Generators
|
||
|
{
|
||
|
public class DesKeyGenerator
|
||
|
: CipherKeyGenerator
|
||
|
{
|
||
|
public DesKeyGenerator()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
internal DesKeyGenerator(
|
||
|
int defaultStrength)
|
||
|
: base(defaultStrength)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* initialise the key generator - if strength is set to zero
|
||
|
* the key generated will be 64 bits in size, otherwise
|
||
|
* strength can be 64 or 56 bits (if you don't count the parity bits).
|
||
|
*
|
||
|
* @param param the parameters to be used for key generation
|
||
|
*/
|
||
|
protected override void engineInit(
|
||
|
KeyGenerationParameters parameters)
|
||
|
{
|
||
|
base.engineInit(parameters);
|
||
|
|
||
|
if (strength == 0 || strength == (56 / 8))
|
||
|
{
|
||
|
strength = DesParameters.DesKeyLength;
|
||
|
}
|
||
|
else if (strength != DesParameters.DesKeyLength)
|
||
|
{
|
||
|
throw new ArgumentException(
|
||
|
"DES key must be " + (DesParameters.DesKeyLength * 8) + " bits long.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override byte[] engineGenerateKey()
|
||
|
{
|
||
|
byte[] newKey = new byte[DesParameters.DesKeyLength];
|
||
|
|
||
|
do
|
||
|
{
|
||
|
random.NextBytes(newKey);
|
||
|
DesParameters.SetOddParity(newKey);
|
||
|
}
|
||
|
while (DesParameters.IsWeakKey(newKey, 0));
|
||
|
|
||
|
return newKey;
|
||
|
}
|
||
|
}
|
||
|
}
|