计算φ=(p-1)(q-1);在java中

时间:2015-02-19 23:37:31

标签: java algorithm cryptography rsa

我有2个大整数,现在我需要在java中计算φ。这里是我现有的代码,我在编写实际公式时难以计算φ=(p-1)(q-1);在java中。感谢

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class RawRSA {

public static void main(String args[]) throws Exception {

BigInteger p = BigInteger.probablePrime(128, new Random());
BigInteger q = BigInteger.probablePrime(128, new Random());
BigInteger n = p.multiply(q);
BigInteger phi = // this is where the formula needs to go;

System.out.println(p);
System.out.println(q);
System.out.println(n);

}

}

1 个答案:

答案 0 :(得分:2)

分步建立。

p-1是p.subtract(BigInteger.ONE)。 q-1是q.subtract(BigInteger.ONE)。你已经知道如何繁殖:

BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));