avt.DynamicFlashRotator.Net.RegCore.Cryptography.BigInteger.modPow C# (CSharp) Method

modPow() public method

public modPow ( BigInteger exp, BigInteger n ) : BigInteger
exp BigInteger
n BigInteger
return BigInteger
        public BigInteger modPow(BigInteger exp, BigInteger n)
        {
            if ((exp.data[maxLength - 1] & 0x80000000) != 0)
                throw (new ArithmeticException("Positive exponents only."));

            BigInteger resultNum = 1;
            BigInteger tempNum;
            bool thisNegative = false;

            if ((this.data[maxLength - 1] & 0x80000000) != 0)   // negative this
	        {
                tempNum = -this % n;
                thisNegative = true;
            } else
                tempNum = this % n;  // ensures (tempNum * tempNum) < b^(2k)

            if ((n.data[maxLength - 1] & 0x80000000) != 0)   // negative n
                n = -n;

            // calculate constant = b^(2k) / m
            BigInteger constant = new BigInteger();

            int i = n.dataLength << 1;
            constant.data[i] = 0x00000001;
            constant.dataLength = i + 1;

            constant = constant / n;
            int totalBits = exp.bitCount();
            int count = 0;

            // perform squaring and multiply exponentiation
            for (int pos = 0; pos < exp.dataLength; pos++) {
                uint mask = 0x01;
                //Console.WriteLine("pos = " + pos);

                for (int index = 0; index < 32; index++) {
                    if ((exp.data[pos] & mask) != 0)
                        resultNum = BarrettReduction(resultNum * tempNum, n, constant);

                    mask <<= 1;

                    tempNum = BarrettReduction(tempNum * tempNum, n, constant);


                    if (tempNum.dataLength == 1 && tempNum.data[0] == 1) {
                        if (thisNegative && (exp.data[0] & 0x1) != 0)    //odd exp
                            return -resultNum;
                        return resultNum;
                    }
                    count++;
                    if (count == totalBits)
                        break;
                }
            }

            if (thisNegative && (exp.data[0] & 0x1) != 0)    //odd exp
                return -resultNum;

            return resultNum;
        }

Usage Example

コード例 #1
0
ファイル: ezrsa.cs プロジェクト: dnnsharp/DynamicRotator
        // Perform an RSA public key operation on input
        public byte[] DoPublic(byte[] input)
        {
            if (input.Length != this.keylen)
                throw new ArgumentException("input.Length does not match keylen");

            if (ReferenceEquals(this.N, null))
                throw new ArgumentException("no key set!");

            BigInteger T = new BigInteger(input);
            if (T >= this.N)
                throw new ArgumentException("input exceeds modulus");

            T = T.modPow(this.E, this.N);

            byte[] b = T.getBytes();
            return pad_bytes(b, this.keylen);
        }
All Usage Examples Of avt.DynamicFlashRotator.Net.RegCore.Cryptography.BigInteger::modPow