Пример #1
0
        /// <summary>
        ///  Creates a new modulus
        /// </summary>
        /// <param name="path">filepath where modulus is written, default is</param>
        /// <param name="length">modulus length in bits</param>
        /// <returns></returns>
        static byte[] CreateNewModulus(string path, int length = 2048)
        {
            MakwaPrivateKey privateKey = MakwaPrivateKey.Generate(length);

            byte[] modulus = Tools.I2OSP(privateKey.Modulus);
            WriteToFile(path, modulus);
            return(modulus);
        }
Пример #2
0
        /// <summary>
        /// Creates a new private key class, writes modulus and primes p,q to file
        /// </summary>
        /// <param name="path">
        /// filepath, primes are appended with "-p" and "-q" respectively
        /// </param>
        /// <param name="length">modulus length in bits, default is 2048</param>
        /// <returns>MakwaPrivateKey</returns>
        static MakwaPrivateKey CreateNewPrivateKey(string path, int length = 2048)
        {
            MakwaPrivateKey privateKey = MakwaPrivateKey.Generate(length);

            byte[] modulus = Tools.I2OSP(privateKey.Modulus);
            byte[] p       = Tools.I2OSP(privateKey.p);
            byte[] q       = Tools.I2OSP(privateKey.q);
            WriteToFile(path, modulus);
            WriteToFile(path + "-p", p);
            WriteToFile(path + "-q", q);
            return(privateKey);
        }
Пример #3
0
        /// <summary>
        /// Generate a new private key. A secure PRNG is used to produce
        /// the new private key. The target modulus size (in bits) is
        /// provided as parameter; it must be no smaller than 1273 bits,
        /// and no greater than 32768 bits. The normal and recommended
        /// modulus size is 2048 bits.
        /// </summary>
        /// <param name="size">   the target modulus size </param>
        /// <returns>  the new private key </returns>
        /// <exception cref="Exception">  on error </exception>
        public static MakwaPrivateKey Generate(int size)
        {
            if (size < 1273 || size > 32768)
            {
                throw new Exception("invalid modulus size: " + size);
            }
            int             sizeP = (size + 1) >> 1;
            int             sizeQ = size - sizeP;
            BigInteger      p     = MakeRandPrime(sizeP);
            BigInteger      q     = MakeRandPrime(sizeQ);
            MakwaPrivateKey k     = new MakwaPrivateKey(p, q);

            if (k.Modulus.BitLength != size)
            {
                throw new Exception("key generation error");
            }
            return(k);
        }