Created a key derivation interface and initial Pkcs5 implementation

This commit is contained in:
David Sparer
2016-10-13 17:17:34 -06:00
parent be59392814
commit f6c490fc62
3 changed files with 41 additions and 0 deletions

View File

@@ -0,0 +1,8 @@

namespace mRemoteNG.Security.KeyDerivation
{
public interface IKeyDerivationFunction
{
byte[] DeriveKey(string password, byte[] salt);
}
}

View File

@@ -0,0 +1,31 @@
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
namespace mRemoteNG.Security.KeyDerivation
{
public class Pkcs5S2KeyGenerator : IKeyDerivationFunction
{
private readonly int _iterations;
private readonly int _keyBitSize;
public Pkcs5S2KeyGenerator(int keyBitSize = 256, int iterations = 1000)
{
_keyBitSize = keyBitSize;
_iterations = iterations;
}
public byte[] DeriveKey(string password, byte[] salt)
{
var passwordInBytes = PbeParametersGenerator.Pkcs5PasswordToBytes(password.ToCharArray());
var keyGenerator = new Pkcs5S2ParametersGenerator();
keyGenerator.Init(passwordInBytes, salt, _iterations);
var keyParameter = (KeyParameter) keyGenerator.GenerateDerivedMacParameters(_keyBitSize);
var keyBytes = keyParameter.GetKey();
return keyBytes;
}
}
}

View File

@@ -192,6 +192,8 @@
<Compile Include="Security\EncryptedSecureString.cs" />
<Compile Include="Security\Authentication\IAuthenticator.cs" />
<Compile Include="Security\EncryptionException.cs" />
<Compile Include="Security\KeyDerivation\IKeyDerivationFunction.cs" />
<Compile Include="Security\KeyDerivation\Pkcs5S2KeyGenerator.cs" />
<Compile Include="Security\SymmetricEncryption\AeadCryptographyProvider.cs" />
<Compile Include="Security\SymmetricEncryption\LegacyRijndaelCryptographyProvider.cs" />
<Compile Include="Security\ICryptographyProvider.cs" />