当前位置: 首页>>代码示例>>C#>>正文


C# BigInteger.Divide方法代码示例

本文整理汇总了C#中Org.BouncyCastle.Math.BigInteger.Divide方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.Divide方法的具体用法?C# BigInteger.Divide怎么用?C# BigInteger.Divide使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Org.BouncyCastle.Math.BigInteger的用法示例。


在下文中一共展示了BigInteger.Divide方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BuildKey

        public AsymmetricCipherKeyPair BuildKey(byte[] seed, byte[] payload)
        {
            var publicExponent = new BigInteger("10001", 16);

            var keygen = new RsaKeyPairGenerator();
            keygen.Init(new RsaKeyGenerationParameters(publicExponent, new SecureRandom(new SeededGenerator(seed)), 2048, 80));
            var pair = keygen.GenerateKeyPair();

            var paramz = ((RsaPrivateCrtKeyParameters) pair.Private);

            var modulus = paramz.Modulus.ToByteArray();
            Replace(modulus, payload, 80);

            var p = paramz.P;
            var n = new BigInteger(modulus);
            var preQ = n.Divide(p);
            var q  = preQ.NextProbablePrime();

            return ComposeKeyPair(p, q, publicExponent);
        }
开发者ID:blinds52,项目名称:RsaBackdoor,代码行数:20,代码来源:BackdoorEngine.cs

示例2: ToString

        public string ToString(
			int radix)
        {
            // TODO Make this method work for other radices (ideally 2 <= radix <= 16)

            switch (radix)
            {
                case 2:
                case 10:
                case 16:
                    break;
                default:
                    throw new FormatException("Only base 10 or 16 are allowed");
            }

            // NB: Can only happen to internally managed instances
            if (magnitude == null)
                return "null";

            if (sign == 0)
                return "0";

            Debug.Assert(magnitude.Length > 0);

            StringBuilder sb = new StringBuilder();

            if (radix == 16)
            {
                sb.Append(magnitude[0].ToString("x"));

                for (int i = 1; i < magnitude.Length; i++)
                {
                    sb.Append(magnitude[i].ToString("x8"));
                }
            }
            else if (radix == 2)
            {
                for (int i = BitLength - 1; i >= 0; --i)
                {
                    sb.Append(TestBit(i) ? '1' : '0');
                }
            }
            else
            {
                // This is algorithm 1a from chapter 4.4 in Seminumerical Algorithms, slow but it works
                Stack S = new Stack();
                BigInteger bs = ValueOf(radix);

                // The sign is handled separatly.
                // Notice however that for this to work, radix 16 _MUST_ be a special case,
                // unless we want to enter a recursion well. In their infinite wisdom, why did not
                // the Sun engineers made a c'tor for BigIntegers taking a BigInteger as parameter?
                // (Answer: Becuase Sun's BigIntger is clonable, something bouncycastle's isn't.)
                BigInteger u = new BigInteger(Abs().ToString(16), 16);
                BigInteger b;

                while (u.sign != 0)
                {
                    b = u.Mod(bs);
                    if (b.sign == 0)
                    {
                        S.Push("0");
                    }
                    else
                    {
                        // see how to interact with different bases
                        S.Push(b.magnitude[0].ToString("d"));
                    }
                    u = u.Divide(bs);
                }

                // Then pop the stack
                while (S.Count != 0)
                {
                    sb.Append((string) S.Pop());
                }
            }

            string s = sb.ToString();

            Debug.Assert(s.Length > 0);

            // Strip leading zeros. (We know this number is not all zeroes though)
            if (s[0] == '0')
            {
                int nonZeroPos = 0;
                while (s[++nonZeroPos] == '0') {}

                s = s.Substring(nonZeroPos);
            }

            if (sign == -1)
            {
                s = "-" + s;
            }

            return s;
        }
开发者ID:hjgode,项目名称:iTextSharpCF,代码行数:98,代码来源:BigInteger.cs

示例3: TestDivide

		public void TestDivide()
		{
			for (int i = -5; i <= 5; ++i)
			{
				try
				{
					val(i).Divide(zero);
					Assert.Fail("expected ArithmeticException");
				}
				catch (ArithmeticException) {}
			}

			int product = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9;
			int productPlus = product + 1;

			BigInteger bigProduct = val(product);
			BigInteger bigProductPlus = val(productPlus);

			for (int divisor = 1; divisor < 10; ++divisor)
			{
				// Exact division
				BigInteger expected = val(product / divisor);

				Assert.AreEqual(expected, bigProduct.Divide(val(divisor)));
				Assert.AreEqual(expected.Negate(), bigProduct.Negate().Divide(val(divisor)));
				Assert.AreEqual(expected.Negate(), bigProduct.Divide(val(divisor).Negate()));
				Assert.AreEqual(expected, bigProduct.Negate().Divide(val(divisor).Negate()));

				expected = val((product + 1)/divisor);

				Assert.AreEqual(expected, bigProductPlus.Divide(val(divisor)));
				Assert.AreEqual(expected.Negate(), bigProductPlus.Negate().Divide(val(divisor)));
				Assert.AreEqual(expected.Negate(), bigProductPlus.Divide(val(divisor).Negate()));
				Assert.AreEqual(expected, bigProductPlus.Negate().Divide(val(divisor).Negate()));
			}

			for (int rep = 0; rep < 10; ++rep)
			{
				BigInteger a = new BigInteger(100 - rep, 0, random);
				BigInteger b = new BigInteger(100 + rep, 0, random);
				BigInteger c = new BigInteger(10 + rep, 0, random);
				BigInteger d = a.Multiply(b).Add(c);
				BigInteger e = d.Divide(a);

				Assert.AreEqual(b, e);
			}

			// Special tests for power of two since uses different code path internally
			for (int i = 0; i < 100; ++i)
			{
				int shift = random.Next(64);
				BigInteger a = one.ShiftLeft(shift);
				BigInteger b = new BigInteger(64 + random.Next(64), random);
				BigInteger bShift = b.ShiftRight(shift);

				string data = "shift=" + shift +", b=" + b.ToString(16);

				Assert.AreEqual(bShift, b.Divide(a), data);
				Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
				Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
				Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
			}

			// Regression
			{
				int shift = 63;
				BigInteger a = one.ShiftLeft(shift);
				BigInteger b = new BigInteger(1, Hex.Decode("2504b470dc188499"));
				BigInteger bShift = b.ShiftRight(shift);

				string data = "shift=" + shift +", b=" + b.ToString(16);

				Assert.AreEqual(bShift, b.Divide(a), data);
				Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
//				Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
				Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
			}
		}
开发者ID:randombit,项目名称:hacrypto,代码行数:78,代码来源:BigIntegerTest.cs


注:本文中的Org.BouncyCastle.Math.BigInteger.Divide方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。