Esempio n. 1
0
        public static ByteString DecryptString(Hash em, ECKeyPair keyPair)
        {
#if NETCOREAPP
            throw new NotImplementedException();
#else
            ECParameters parameters = new ECParameters();
            byte[]       ems        = em;
            parameters.Q.X   = ems.Skip(1).Take(32).ToArray();
            parameters.Q.Y   = ems.Skip(33).Take(32).ToArray();
            parameters.D     = keyPair.D;
            parameters.Curve = keyPair.ECParameters.Curve;
            parameters.Validate();
            using (var ecDiffieHellmanCng = new ECDiffieHellmanCng())
            {
                ecDiffieHellmanCng.ImportParameters(parameters);
                var ek = ecDiffieHellmanCng.DeriveKeyFromHash(ecDiffieHellmanCng.PublicKey, HashAlgorithmName.SHA256, null, new byte[] { 0, 0, 0, 1 });
                using (RijndaelManaged aes = new RijndaelManaged())
                {
                    aes.Padding = PaddingMode.PKCS7;
                    var d  = aes.CreateDecryptor(ek, new byte[16]);
                    var bs = d.TransformFinalBlock(ems.Skip(65).Take(ems.Length - 65 - 32).ToArray(), 0,
                                                   ems.Length - 65 - 32);
                    return(bs);
                }
            }
#endif
        }
Esempio n. 2
0
 public static void HashAlgorithm_SupportsOtherECDHImplementations()
 {
     using ECDiffieHellman ecdh       = ECDiffieHellman.Create(ECCurve.NamedCurves.nistP256);
     using ECDiffieHellmanCng ecdhCng = new ECDiffieHellmanCng(ECCurve.NamedCurves.nistP256);
     byte[] key1 = ecdhCng.DeriveKeyFromHash(ecdh.PublicKey, HashAlgorithmName.SHA256);
     byte[] key2 = ecdh.DeriveKeyFromHash(ecdhCng.PublicKey, HashAlgorithmName.SHA256);
     Assert.Equal(key1, key2);
 }
Esempio n. 3
0
        public static void Equivalence_Hash(HashAlgorithmName algorithm, bool prepend, bool append)
        {
            using (ECDiffieHellmanCng ecdh = NewDefaultECDHCng())
                using (ECDiffieHellmanPublicKey publicKey = ecdh.PublicKey)
                {
                    byte[] secretPrepend = prepend ? new byte[3] : null;
                    byte[] secretAppend  = append ? new byte[4] : null;

                    byte[] newWay = ecdh.DeriveKeyFromHash(publicKey, algorithm, secretPrepend, secretAppend);

                    ecdh.HashAlgorithm         = new CngAlgorithm(algorithm.Name);
                    ecdh.SecretPrepend         = secretPrepend;
                    ecdh.SecretAppend          = secretAppend;
                    ecdh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;

                    byte[] oldWay = ecdh.DeriveKeyMaterial(publicKey);

                    Assert.Equal(newWay, oldWay);
                }
        }