示例#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>
        public byte[] RSAProcess(byte[] PlainText, bool usePrivate)
        {
            if (usePrivate && !rsaParams.Has_PRIVATE_Info)
            {
                throw new CryptographicException("RSA Process: Incomplete Private Key Info");
            }

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

            BigInteger _E;

            if (usePrivate)
            {
                _E = rsaParams.D;
            }
            else
            {
                _E = rsaParams.E;
            }

            var PT = RSAProviderUtils.OS2IP(PlainText, false);
            var M  = BigInteger.ModPow(PT, _E, rsaParams.N);

            if (M.Sign == -1)
            {
                return(RSAProviderUtils.I2OSP(M + rsaParams.N, rsaParams.OctetsInModulus, false));
            }
            return(RSAProviderUtils.I2OSP(M, rsaParams.OctetsInModulus, false));
        }
示例#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>
        public byte[] RSADecryptPrivateCRT(byte[] Data)
        {
            if (rsaParams.Has_PRIVATE_Info && rsaParams.HasCRTInfo)
            {
                BigInteger C = RSAProviderUtils.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(RSAProviderUtils.I2OSP(M + rsaParams.N, rsaParams.OctetsInModulus, false));
                }
                else
                {
                    return(RSAProviderUtils.I2OSP(M, rsaParams.OctetsInModulus, false));
                }
            }
            else
            {
                throw new CryptographicException("RSA Decrypt CRT: Incomplete Key Info");
            }
        }
 /// <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 RSAProviderParameters(byte[] Modulus, byte[] Exponent, int ModulusSize)
 {
     // rsaParams;
     OctetsInModulus = ModulusSize / 8;
     E = RSAProviderUtils.OS2IP(Exponent, false);
     N = RSAProviderUtils.OS2IP(Modulus, false);
     Has_PUBLIC_Info = true;
 }
 /// <summary>
 /// Initialize the RSA class.
 /// </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="ModulusSize">Modulus size in number of bits. Ex: 512, 1024, 2048, 4096 etc.</param>
 public RSAProviderParameters(byte[] Modulus, byte[] Exponent, byte [] D, int ModulusSize)
 {
     // rsaParams;
     _ModulusOctets    = ModulusSize / 8;
     _E                = RSAProviderUtils.OS2IP(Exponent, false);
     _N                = RSAProviderUtils.OS2IP(Modulus, false);
     _D                = RSAProviderUtils.OS2IP(D, false);
     _Has_PUBLIC_Info  = true;
     _Has_PRIVATE_Info = true;
 }
 /// <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 RSAProviderParameters(RSAParameters rsaParams, int ModulusSize)
 {
     // rsaParams;
     OctetsInModulus = ModulusSize / 8;
     E                = RSAProviderUtils.OS2IP(rsaParams.Exponent, false);
     D                = RSAProviderUtils.OS2IP(rsaParams.D, false);
     N                = RSAProviderUtils.OS2IP(rsaParams.Modulus, false);
     P                = RSAProviderUtils.OS2IP(rsaParams.P, false);
     Q                = RSAProviderUtils.OS2IP(rsaParams.Q, false);
     DP               = RSAProviderUtils.OS2IP(rsaParams.DP, false);
     DQ               = RSAProviderUtils.OS2IP(rsaParams.DQ, false);
     InverseQ         = RSAProviderUtils.OS2IP(rsaParams.InverseQ, false);
     HasCRTInfo       = true;
     Has_PUBLIC_Info  = true;
     Has_PRIVATE_Info = true;
 }
 /// <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 RSAProviderParameters(byte[] Modulus, byte[] Exponent, byte[] D, byte[] P, byte [] Q, byte [] DP, byte [] DQ, byte [] InverseQ, int ModulusSize)
 {
     // rsaParams;
     _ModulusOctets    = ModulusSize / 8;
     _E                = RSAProviderUtils.OS2IP(Exponent, false);
     _N                = RSAProviderUtils.OS2IP(Modulus, false);
     _D                = RSAProviderUtils.OS2IP(D, false);
     _P                = RSAProviderUtils.OS2IP(P, false);
     _Q                = RSAProviderUtils.OS2IP(Q, false);
     _DP               = RSAProviderUtils.OS2IP(DP, false);
     _DQ               = RSAProviderUtils.OS2IP(DQ, false);
     _InverseQ         = RSAProviderUtils.OS2IP(InverseQ, false);
     _Has_CRT_Info     = true;
     _Has_PUBLIC_Info  = true;
     _Has_PRIVATE_Info = true;
 }
 /// <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 RSAProviderParameters(RSAParameters rsaParams, int ModulusSize)
 {
     // rsaParams;
     _ModulusOctets    = ModulusSize / 8;
     _E                = RSAProviderUtils.OS2IP(rsaParams.Exponent, false);
     _D                = RSAProviderUtils.OS2IP(rsaParams.D, false);
     _N                = RSAProviderUtils.OS2IP(rsaParams.Modulus, false);
     _P                = RSAProviderUtils.OS2IP(rsaParams.P, false);
     _Q                = RSAProviderUtils.OS2IP(rsaParams.Q, false);
     _DP               = RSAProviderUtils.OS2IP(rsaParams.DP, false);
     _DQ               = RSAProviderUtils.OS2IP(rsaParams.DQ, false);
     _InverseQ         = RSAProviderUtils.OS2IP(rsaParams.InverseQ, false);
     _Has_CRT_Info     = true;
     _Has_PUBLIC_Info  = true;
     _Has_PRIVATE_Info = true;
 }
 /// <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 RSAProviderParameters(byte[] Modulus, byte[] Exponent, byte[] D, byte[] P, byte[] Q, byte[] DP, byte[] DQ,
                              byte[] InverseQ, int ModulusSize)
 {
     // rsaParams;
     OctetsInModulus = ModulusSize / 8;
     E                = RSAProviderUtils.OS2IP(Exponent, false);
     N                = RSAProviderUtils.OS2IP(Modulus, false);
     this.D           = RSAProviderUtils.OS2IP(D, false);
     this.P           = RSAProviderUtils.OS2IP(P, false);
     this.Q           = RSAProviderUtils.OS2IP(Q, false);
     this.DP          = RSAProviderUtils.OS2IP(DP, false);
     this.DQ          = RSAProviderUtils.OS2IP(DQ, false);
     this.InverseQ    = RSAProviderUtils.OS2IP(InverseQ, false);
     HasCRTInfo       = true;
     Has_PUBLIC_Info  = true;
     Has_PRIVATE_Info = true;
 }