TizzyT

ECDHx -TizzyT

Sep 27th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1.     public class ECDHx : IDisposable
  2.     {
  3.         public enum ECDiffieHellmanModes
  4.         {
  5.             MD5,
  6.             SHA1,
  7.             SHA256,
  8.             SHA384,
  9.             SHA512
  10.         }
  11.  
  12.         internal readonly ECDiffieHellmanCng ecdh;
  13.  
  14.         public byte[] PublicKey { get; }
  15.        
  16.         public ECDHx(ECDiffieHellmanModes Mode)
  17.         {
  18.             ecdh = new ECDiffieHellmanCng();
  19.             ecdh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
  20.             switch (Mode)
  21.             {
  22.                 case ECDiffieHellmanModes.MD5:
  23.                     ecdh.HashAlgorithm = CngAlgorithm.MD5;
  24.                     break;
  25.                 case ECDiffieHellmanModes.SHA1:
  26.                     ecdh.HashAlgorithm = CngAlgorithm.Sha1;
  27.                     break;
  28.                 case ECDiffieHellmanModes.SHA256:
  29.                     ecdh.HashAlgorithm = CngAlgorithm.Sha256;
  30.                     break;
  31.                 case ECDiffieHellmanModes.SHA384:
  32.                     ecdh.HashAlgorithm = CngAlgorithm.Sha384;
  33.                     break;
  34.                 case ECDiffieHellmanModes.SHA512:
  35.                     ecdh.HashAlgorithm = CngAlgorithm.Sha512;
  36.                     break;
  37.             }
  38.             PublicKey = ecdh.PublicKey.ToByteArray();
  39.         }
  40.  
  41.         public byte[] DeriveSharedKey(byte[] PublicKey)
  42.         {
  43.             byte[] SharedKey = ecdh.DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromByteArray(PublicKey, CngKeyBlobFormat.EccPublicBlob));
  44.             return SharedKey;
  45.         }
  46.  
  47.         public void Dispose() => ecdh.Dispose();
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment