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


C# BigInteger.First方法代码示例

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


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

示例1: Decrypt

        public byte[] Decrypt(byte[] message, PrivateKey privateKey, PublicKey publicKey)
        {
            var keySize = publicKey.n.ToByteArray().Length;
            var encryptedBlockSize = keySize;
            var sourceBlockSize = keySize - preffix.Length - 2;

            var numberBlocks = message.Length / encryptedBlockSize;
            if (message.Length % encryptedBlockSize != 0)
                throw new ArgumentException("Wrong size of encrypted message");

            var decryptedMessage = new byte[numberBlocks * sourceBlockSize];

            Parallel.For(0, numberBlocks, (i) =>
            //for (var i = 0; i < numberBlocks; i++)
            {
                var part = new byte[encryptedBlockSize];

                Array.Copy(message, i * encryptedBlockSize, part, 0, encryptedBlockSize);
                var Mi = new BigInteger(part);
                var D = (publicKey.b * publicKey.b + 4 * Mi) % publicKey.n;
                //var s = BigInteger.ModPow(D, (privateKey.p + 1) / 4, privateKey.p);
                //var r = BigInteger.ModPow(D, (privateKey.q + 1) / 4, privateKey.q);
                var s = BigIntegerHelper.fast_exp(D, (privateKey.p + 1) / 4, privateKey.p);
                var r = BigIntegerHelper.fast_exp(D, (privateKey.q + 1) / 4, privateKey.q);
                BigInteger yp, yq;
                BigInteger d = 1;
                ExtendedEuclid(privateKey.p, privateKey.q, out yp, out yq, out d);
                var roots = new BigInteger[4];

                roots[0] = BigInteger.Abs(yp * privateKey.p * r + yq * privateKey.q * s);
                roots[1] = (-roots[0]) % publicKey.n + publicKey.n;
                roots[0] = roots[0] % publicKey.n;

                roots[2] = BigInteger.Abs(yp * privateKey.p * r - yq * privateKey.q * s);
                roots[3] = (-roots[2]) % publicKey.n + publicKey.n;
                roots[2] = roots[2] % publicKey.n;

                for (var j = 0; j < 4; j++)
                {
                    roots[j] = ((-publicKey.b + roots[j]) / 2) % publicKey.n;
                    roots[j] = roots[j] < 0 ? roots[j] + publicKey.n : roots[j];
                }

                var rightRoot = roots.First(num => num.ToByteArray().Skip(sourceBlockSize).Take(preffix.Length).SequenceEqual(preffix));
                Array.Copy(rightRoot.ToByteArray(), 0, decryptedMessage, i * sourceBlockSize, sourceBlockSize);
            });

            return decryptedMessage;
        }
开发者ID:Tlalok,项目名称:CandPCI,代码行数:49,代码来源:RabinCryptosystem.cs


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