简体   繁体   English

有没有办法在java中pow 2 BigInteger Numbers?

[英]Is there a way to pow 2 BigInteger Numbers in java?

I have to pow a bigInteger number with another BigInteger number.我必须用另一个 BigInteger 数字来 pow 一个 bigInteger 数字。

Unfortunately, only one BigInteger.pow(int) is allowed.不幸的是,只允许使用一个 BigInteger.pow(int)。

I have no clue on how I can solve this problem.我不知道如何解决这个问题。

I have to pow a bigInteger number with another BigInteger number.我必须用另一个 BigInteger 数字来 pow 一个 bigInteger 数字。

No, you don't.不,你没有。

You read a crypto spec and it seemed to say that.你阅读了一个加密规范,它似乎是这么说的。 But that's not what it said;但这不是它所说的; you didn't read carefully enough.你读得不够仔细。 The mathematical 'universe' that the math in the paper / spec you're reading operates in is different from normal math.您正在阅读的论文/规范中的数学运算所在的数学“宇宙”与普通数学不同。 It's a modulo-space.这是一个模空间。 All operations are implicitly performed modulo X, where X is some number the crypto algorithm explains.所有操作都以 X 为模隐式执行,其中 X 是加密算法解释的某个数字。

You can do that just fine .你可以这样做就好了

Alternatively, the spec is quite clear and says something like: C = (A^B) % M and you've broken that down in steps (... first, I must calculate A to the power of B. I'll worry about what the % M part is all about later).或者,规范非常清楚,并说如下: C = (A^B) % M并且您已将其分步分解(...首​​先,我必须将 A 计算为 B 的幂。我会担心关于 % M 部分的全部内容稍后)。 That's not how that works - you can't lop that operation into parts.这不是它的工作原理——你不能把那个操作分成几部分。 (A^B) % M is quite doable, and has its own efficient algorithm. (A^B) % M非常可行,并且有自己的高效算法。 (A^B) is simply not calculable without a few years worth of the planet's entire energy and GDP output. (A^B)如果没有几年的地球全部能源和 GDP 产出,根本无法计算。

The reason I know that must be what you've been reading, is because (A ^ B) % M is a common operation in crypto.我知道这一定是您一直在阅读的原因,因为(A ^ B) % M是加密中的常见操作。 (Well, that, and the simple fact that A^B can't be done). (嗯,那个,以及无法完成 A^B 的简单事实)。

Just to be crystal clear: When I say impossible, I mean it in the same way 'travelling faster than the speed of light' is impossible.明确地说:当我说不可能时,我的意思是“以超过光速的速度行进”是不可能的。 It's a law in the physics sense of the word: If you really just want to do A^B and not in a modspace where B is so large it doesn't fit in an int, a computer cannot calculate it, and the result will be gigabytes large.这是物理意义上的定律:如果你真的只想做 A^B 而不是在 B 太大以至于不能放入 int 的 modspace 中,计算机无法计算它,结果将是千兆字节大。 int can hold about 9 digits worth. int 可以容纳大约 9 位数字。 Just for fun, imagine doing X^Y where both X and Y are 20 digit numbers.只是为了好玩,想象一下 X^Y,其中 X 和 Y 都是 20 位数字。

The result would have 10^21 digits.结果将有 10^21 位数字。

That's roughly equal to the total amount of disk space available worldwide.这大致等于全球可用的磁盘空间总量。 10^12 is a terabyte. 10^12 是 TB。 You're asking to calculate a number where, forget about calculating it, merely storing it requires one thousand million harddisks each of 1TB.你要求计算一个数字,忘记计算它,仅仅存储它需要一亿个硬盘,每个 1TB。

Thus, I'm 100% certain that you do not want what you think you want.因此,我 100% 肯定你不想要你认为你想要的。

TIP: If you can't follow the math (which is quite bizarre; it's not like you get modulo-space math in your basic AP math class!), generally rolling your own implementation of a crypto algorithm isn't going to work out.提示:如果你不能遵循数学(这很奇怪;这不像你在基本的 AP 数学课中获得模空间数学!),通常滚动你自己的加密算法实现是行不通的. The problem with crypto is, if you mess up, often a unit test cannot catch it.加密的问题是,如果你搞砸了,单元测试通常无法捕捉到它。 No;不; someone will hack your stuff and then you know, and that's a high price to pay.有人会破解你的东西,然后你就知道了,这是一个很高的代价。 Rely on experts to build the algorithm, spend your time ensuring the protocol is correct (which is still quite difficult to get right, don't take that lightly!).依靠专家来构建算法,花时间确保协议是正确的(这仍然很难做到,不要掉以轻心!)。 If you insist, make dang sure you have a heap of plaintext+keys / encrypted (or plaintext / hashed, or whatever it is you're doing) pairs to test against, and assume that whatever you wrote, even if it passes those tests, is still insecure because eg it is trivial to leak the key out of your algorithm using timing attacks.如果您坚持,请确保您有一堆明文+密钥/加密(或明文/散列,或您正在做的任何事情)对进行测试,并假设您编写的任何内容,即使它通过了这些测试, 仍然不安全,因为例如使用定时攻击将密钥从算法中泄露出来是微不足道的。

Since you anyway want to use it in a modulo operation with a prime number, like @Progman said in the comments, you can use modPow()由于您无论如何都想在带有质数的模运算中使用它,就像@Progman 在评论中所说的那样,您可以使用modPow()

Below is an example code:下面是一个示例代码:

        // Create BigInteger objects
        BigInteger biginteger1, biginteger2, exponent, result;
  
        //prime number
        int pNumber = 5;
        // Intializing all BigInteger Objects
        biginteger1 = new BigInteger("23895");
        biginteger2 = BigInteger.valueOf(pNumber);
        exponent = new BigInteger("15");
  
        // Perform modPow operation on the objects and exponent
        result = biginteger1.modPow(exponent, biginteger2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM