Beispiel #1
0
        /// <summary>
        /// 根据私钥创建公钥
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        unsafe public static PublicKey CreatePublicKey(ReadOnlySpan <byte> privateKey)
        {
            if (privateKey.Length != 32)
            {
                throw new InvalidPrivateKeyException("私钥长度必须是32字节");
            }
            var k = new U256(privateKey, bigEndian: true);

            if (k.IsZero || k >= ModN.N)
            {
                throw new InvalidPrivateKeyException();
            }
            var retPoint = ModP.MulG(k);

            Clear(&k);
            return(new PublicKey(ModP.ToU256(retPoint.X), ModP.ToU256(retPoint.Y)));
        }
Beispiel #2
0
        /// <summary>
        /// 使用自己的私钥与对方公钥进行密钥交换(私钥A×公钥B = 私钥B×公钥A)
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        unsafe public static EncryptionKey CreateEncryptionKey(ReadOnlySpan <byte> privateKey, PublicKey publicKey)
        {
            if (privateKey.Length != 32)
            {
                throw new InvalidPrivateKeyException("私钥长度必须是32字节");
            }
            var k = new U256(privateKey, bigEndian: true);

            if (k.IsZero || k >= ModN.N)
            {
                throw new InvalidPrivateKeyException();
            }

            var p = ModP.Mul(publicKey.ToPoint(), k);

            Clear(&k);
            return(new EncryptionKey(ModP.ToU256(p.X), ModP.ToU256(p.Y)));
        }