Exemplo n.º 1
0
        /// <summary>
        /// Low level RSA Process function for use with private key.
        /// Should never be used; Because without padding RSA is vulnerable to attacks.  Use with caution.
        /// </summary>
        /// <param name="plainText">Data to encrypt. Length must be less than Modulus size in octets.</param>
        /// <param name="usePrivate">True to use Private key, else Public.</param>
        /// <returns>Encrypted Data</returns>
        private byte[] RSAProcess(byte[] plainText, bool usePrivate)
        {
            if (usePrivate && !rsaParams.HasPrivateInfo)
            {
                throw new CryptographicException("RSA Process: Incomplete Private Key Info");
            }

            if (usePrivate == false && !rsaParams.HasPublicInfo)
            {
                throw new CryptographicException("RSA Process: Incomplete Public Key Info");
            }

            BigInteger e = usePrivate ? rsaParams.D : rsaParams.E;

            BigInteger pt = RSAxUtils.OS2IP(plainText, false);
            BigInteger m  = BigInteger.ModPow(pt, e, rsaParams.N);

            if (m.Sign == -1)
            {
                return(RSAxUtils.I2OSP(m + rsaParams.N, rsaParams.OctetsInModulus, false));
            }
            else
            {
                return(RSAxUtils.I2OSP(m, rsaParams.OctetsInModulus, false));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Low level RSA Decryption function for use with private key. Uses CRT and is Much faster.
        /// Should never be used; Because without padding RSA is vulnerable to attacks. Use with caution.
        /// </summary>
        /// <param name="data">Data to encrypt. Length must be less than Modulus size in octets.</param>
        /// <returns>Encrypted Data</returns>
        private byte[] RSADecryptPrivateCRT(byte[] data)
        {
            if (rsaParams.HasPrivateInfo && rsaParams.HasCRTInfo)
            {
                BigInteger c = RSAxUtils.OS2IP(data, false);

                BigInteger m1 = BigInteger.ModPow(c, rsaParams.DP, rsaParams.P);
                BigInteger m2 = BigInteger.ModPow(c, rsaParams.DQ, rsaParams.Q);
                BigInteger h  = (m1 - m2) * rsaParams.InverseQ % rsaParams.P;
                BigInteger m  = m2 + rsaParams.Q * h;

                if (m.Sign == -1)
                {
                    return(RSAxUtils.I2OSP(m + rsaParams.N, rsaParams.OctetsInModulus, false));
                }
                else
                {
                    return(RSAxUtils.I2OSP(m, rsaParams.OctetsInModulus, false));
                }
            }
            else
            {
                throw new CryptographicException("RSA Decrypt CRT: Incomplete Key Info");
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initialize the RSA class. Only the public parameters.
 /// </summary>
 /// <param name="modulus">Modulus of the RSA key.</param>
 /// <param name="exponent">Exponent of the RSA key</param>
 /// <param name="modulusSize">Modulus size in number of bits. Ex: 512, 1024, 2048, 4096 etc.</param>
 public RSAxParameters(byte[] modulus, byte[] exponent, int modulusSize)
 {
     // rsaParams;
     OctetsInModulus = modulusSize / 8;
     E             = RSAxUtils.OS2IP(exponent, false);
     N             = RSAxUtils.OS2IP(modulus, false);
     HasPublicInfo = true;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initialize the RSA class. For CRT.
 /// </summary>
 /// <param name="modulus">Modulus of the RSA key.</param>
 /// <param name="exponent">Exponent of the RSA key</param>
 /// /// <param name="d">Exponent of the RSA key</param>
 /// <param name="p">P paramater of RSA Algorithm.</param>
 /// <param name="q">Q paramater of RSA Algorithm.</param>
 /// <param name="dp">DP paramater of RSA Algorithm.</param>
 /// <param name="dq">DQ paramater of RSA Algorithm.</param>
 /// <param name="inverseQ">InverseQ paramater of RSA Algorithm.</param>
 /// <param name="modulusSize">Modulus size in number of bits. Ex: 512, 1024, 2048, 4096 etc.</param>
 public RSAxParameters(byte[] modulus, byte[] exponent, byte[] d, byte[] p, byte [] q, byte [] dp, byte [] dq, byte [] inverseQ, int modulusSize)
 {
     // rsaParams;
     OctetsInModulus = modulusSize / 8;
     E              = RSAxUtils.OS2IP(exponent, false);
     N              = RSAxUtils.OS2IP(modulus, false);
     D              = RSAxUtils.OS2IP(d, false);
     P              = RSAxUtils.OS2IP(p, false);
     Q              = RSAxUtils.OS2IP(q, false);
     DP             = RSAxUtils.OS2IP(dp, false);
     DQ             = RSAxUtils.OS2IP(dq, false);
     InverseQ       = RSAxUtils.OS2IP(inverseQ, false);
     HasCRTInfo     = true;
     HasPublicInfo  = true;
     HasPrivateInfo = true;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Initialize the RSA class. It's assumed that both the Public and Extended Private info are there.
 /// </summary>
 /// <param name="rsaParams">Preallocated RSAParameters containing the required keys.</param>
 /// <param name="modulusSize">Modulus size in bits</param>
 public RSAxParameters(RSAParameters rsaParams, int modulusSize)
 {
     // rsaParams;
     OctetsInModulus = modulusSize / 8;
     E              = RSAxUtils.OS2IP(rsaParams.Exponent, false);
     D              = RSAxUtils.OS2IP(rsaParams.D, false);
     N              = RSAxUtils.OS2IP(rsaParams.Modulus, false);
     P              = RSAxUtils.OS2IP(rsaParams.P, false);
     Q              = RSAxUtils.OS2IP(rsaParams.Q, false);
     DP             = RSAxUtils.OS2IP(rsaParams.DP, false);
     DQ             = RSAxUtils.OS2IP(rsaParams.DQ, false);
     InverseQ       = RSAxUtils.OS2IP(rsaParams.InverseQ, false);
     HasCRTInfo     = true;
     HasPublicInfo  = true;
     HasPrivateInfo = true;
 }