示例#1
0
        public static API.ECDH_Struct GetECDHKeys()
        {
            API.ECDH_Struct ECDH       = new API.ECDH_Struct();
            byte[]          PrivateKey = new byte[1024];
            byte[]          PublicKey  = new byte[1024];
            byte[]          Sharekey   = new byte[16];
            byte[]          SvrPubKey  = API.HexStrToByteArray("04EBCA94D733E399B2DB96EACDD3F69A8BB0F74224E2B44E3357812211D2E62EFBC91BB553098E25E33A799ADC7F76FEB208DA7C6522CDB0719A305180CC54A82E");
            var             eckey      = OpenSSL.EC_KEY_new_by_curve_name(415);

            if (eckey == IntPtr.Zero)
            {
                return(ECDH);
            }
            var res          = OpenSSL.EC_KEY_generate_key(eckey);
            var ec_group     = OpenSSL.EC_KEY_get0_group(eckey);
            var ec_point     = OpenSSL.EC_KEY_get0_public_key(eckey);
            var PublicKeyLen = OpenSSL.EC_POINT_point2oct(ec_group, (System.IntPtr)ec_point, 4, PublicKey, 65, (System.IntPtr) 0);

            Array.Resize(ref PublicKey, PublicKeyLen);
            ECDH.PublicKey = PublicKey;
            ec_point       = (int)OpenSSL.EC_KEY_get0_private_key(eckey);
            var PrivateKeyLen = OpenSSL.BN_bn2mpi((System.IntPtr)ec_point, PrivateKey);

            Array.Resize(ref PrivateKey, (System.Int32)PrivateKeyLen);
            ECDH.PrivateKey = PrivateKey;
            eckey           = OpenSSL.EC_KEY_new_by_curve_name(415);
            if (eckey == IntPtr.Zero)
            {
                return(ECDH);
            }
            var bn = OpenSSL.BN_new();

            OpenSSL.BN_mpi2bn(ECDH.PrivateKey, ECDH.PrivateKey.Length, bn);
            OpenSSL.EC_KEY_set_private_key(eckey, bn);
            OpenSSL.BN_free(bn);
            ec_group = OpenSSL.EC_KEY_get0_group(eckey);
            ec_point = (int)OpenSSL.EC_POINT_new(ec_group);
            OpenSSL.EC_POINT_oct2point(ec_group, (System.IntPtr)ec_point, SvrPubKey, SvrPubKey.Length, (System.IntPtr) 0);
            OpenSSL.ECDH_compute_key(Sharekey, 16, (System.IntPtr)ec_point, eckey, IntPtr.Zero);
            ECDH.Sharekey = API.MD5Hash(Sharekey);
            return(ECDH);
        }
示例#2
0
        public static byte[] GetECDHKeysEx(byte[] peerRawPublicKey, byte[] PublicKey, byte[] PrivateKey)
        {
            API.ECDH_Struct ECDH = new API.ECDH_Struct();
            //Dim PrivateKey(1023) As Byte
            // Dim PublicKey(1023) As Byte
            //Dim Sharekey(15) As Byte

            var ec_key = OpenSSL.EC_KEY_new_by_curve_name(415);
            var bn     = OpenSSL.BN_new();

            OpenSSL.BN_mpi2bn(PrivateKey, PrivateKey.Length, bn);
            OpenSSL.EC_KEY_set_private_key(ec_key, bn);
            OpenSSL.BN_free(bn);
            var ec_group = OpenSSL.EC_KEY_get0_group(ec_key);
            var ec_point = OpenSSL.EC_POINT_new(ec_group);

            OpenSSL.EC_POINT_oct2point(ec_group, ec_point, peerRawPublicKey, peerRawPublicKey.Length, (System.IntPtr) 0);
            OpenSSL.ECDH_compute_key(PublicKey, 16, ec_point, ec_key, (System.IntPtr) 0);
            return(API.MD5Hash(PublicKey));
        }