/// <summary>
        /// Derives the symmetric key from the shared secret.
        /// </summary>
        /// <returns>The symmetric key.</returns>
        /// <param name="sharedSecret">Shared secret.</param>
        private byte[] DeriveSymmetricKeyFromSharedSecret(byte[] sharedSecret)
        {
            ECDHKekGenerator egH = new ECDHKekGenerator(DigestUtilities.GetDigest("SHA256"));

            egH.Init(new DHKdfParameters(NistObjectIdentifiers.Aes, sharedSecret.Length, sharedSecret));
            byte[] symmetricKey = new byte[DigestUtilities.GetDigest("SHA256").GetDigestSize()];
            egH.GenerateBytes(symmetricKey, 0, symmetricKey.Length);
            return(symmetricKey);
        }
示例#2
0
        public static byte[] AsymmetricDecrypt(byte[] data, ref AsymmetricCipherKeyPair keypair)
        {
            //create the key agreement
            ECDHBasicAgreement ag = new ECDHBasicAgreement();

            ag.Init(keypair.Private);

            //calculate the shared secret key
            BigInteger a = ag.CalculateAgreement(keypair.Public);

            byte[] secret = a.ToByteArray();

            //derive the symmetric encryption key
            ECDHKekGenerator topkek = new ECDHKekGenerator(DigestUtilities.GetDigest("SHA256"));

            topkek.Init(new DHKdfParameters(NistObjectIdentifiers.Aes, secret.Length, secret));
            byte[] symKey = new byte[DigestUtilities.GetDigest("SHA256").GetDigestSize()];
            topkek.GenerateBytes(symKey, 0, symKey.Length);

            //decrypt the data
            KeyParameter    parm   = ParameterUtilities.CreateKeyParameter("DES", symKey);
            IBufferedCipher cipher = CipherUtilities.GetCipher("DES/ECB/ISO7816_4PADDING");

            cipher.Init(false, parm);
            byte[] ret = null;
            try
            {
                ret = cipher.DoFinal(data);
            }
            catch (Exception e)
            {
                if (e != null)
                {
                    return(null);
                }
            }

            //erase the keys
            Eraser.SecureErase(secret);
            Eraser.SecureErase(symKey);

            return(ret);
        }
        public static byte[] simetricKey(string pvtKey, string pubKey)
        {
            AsymmetricKeyParameter publicKey  = (ECPublicKeyParameters)importKey(ReadOnlyParam.PemWritter.beginPublic + pubKey + ReadOnlyParam.PemWritter.endPublic);
            AsymmetricKeyParameter privateKey = ((AsymmetricCipherKeyPair)importKey(ReadOnlyParam.PemWritter.beginPrivate + pvtKey + ReadOnlyParam.PemWritter.endPrivate)).Private;

            ECDHCBasicAgreement acAgreement = new ECDHCBasicAgreement();

            acAgreement.Init(privateKey);
            BigInteger a = acAgreement.CalculateAgreement(publicKey);

            byte[] sharedSecret = a.ToByteArray();

            ECDHKekGenerator egH = new ECDHKekGenerator(DigestUtilities.GetDigest(ReadOnlyParam.sha));

            egH.Init(new DHKdfParameters(NistObjectIdentifiers.Aes, sharedSecret.Length, sharedSecret));
            byte[] symmetricKey = new byte[DigestUtilities.GetDigest(ReadOnlyParam.sha).GetDigestSize()];
            egH.GenerateBytes(symmetricKey, 0, symmetricKey.Length);

            return(symmetricKey);
        }