package bignumTester; import java.math.BigInteger; import java.util.ArrayList; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextArea; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.RowConstraints; import javafx.scene.text.Text; import javafx.stage.Stage; public class BigNumTester extends Application { private BigInteger rsaVal; private BigInteger dVal; private BigInteger ddVal; private BigInteger eVal; private BigInteger xVal; private BigInteger bigNVal; private static final int WIDTH = 1024; private static final int HEIGHT = 768; private static final int NUMCOLS = 5; private static final int NUMROWS = 5; private static final int COL1_PERCENT_WIDTH = 10; private static final int OTHERCOLS_PERCENT_WIDTH = (100 - COL1_PERCENT_WIDTH); private static final int PADDING= 10; @Override public void start(Stage primaryStage) { GridPane root = new GridPane(); setupGrid(root); addComponents(root); Scene scene = new Scene(root, WIDTH, HEIGHT); scene.getStylesheets().add (BigNumTester.class.getResource("bigNumTester.css").toExternalForm()); primaryStage.setTitle("BigNumTester"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } private void setupGrid(GridPane root){ ArrayList ccArray = new ArrayList<>(); ColumnConstraints c1 = new ColumnConstraints(); c1.setPercentWidth(COL1_PERCENT_WIDTH); ccArray.add(c1); for(int i = 1; i < NUMCOLS; i++){ ColumnConstraints temp = new ColumnConstraints(); temp.setPercentWidth(OTHERCOLS_PERCENT_WIDTH/(NUMCOLS - 1)); ccArray.add(temp); } ArrayList rcArray = new ArrayList<>(); for(int j = 0; j < NUMROWS; j++){ RowConstraints temp = new RowConstraints(); temp.setPercentHeight(100/NUMROWS); rcArray.add(temp); } root.getColumnConstraints().addAll(ccArray); root.getRowConstraints().addAll(rcArray); } private void addComponents(GridPane root){ Text rsaLbl = new Text("RSA2048:"); TextArea rsaIn = new TextArea(); rsaIn.setPrefColumnCount(WIDTH * OTHERCOLS_PERCENT_WIDTH / 100); rsaIn.setPrefRowCount(100/NUMROWS * 1 * HEIGHT); rsaIn.setWrapText(true); Text dLbl = new Text("d: "); TextArea dResult = new TextArea(""); dResult.setPrefColumnCount(WIDTH * OTHERCOLS_PERCENT_WIDTH / 100); dResult.setPrefRowCount(100/NUMROWS * 1 * HEIGHT); dResult.setWrapText(true); Text eLbl = new Text("e: "); TextArea eResult = new TextArea(""); eResult.setPrefColumnCount(WIDTH * OTHERCOLS_PERCENT_WIDTH / 100); eResult.setPrefRowCount(100/NUMROWS * 1 * HEIGHT); eResult.setWrapText(true); Text bigNLbl = new Text("N: "); TextArea bigNResult = new TextArea(""); bigNResult.setPrefColumnCount(WIDTH * OTHERCOLS_PERCENT_WIDTH / 100); bigNResult.setPrefRowCount(100/NUMROWS * 1 * HEIGHT); bigNResult.setWrapText(true); Button runBtn = new Button("Go!"); runBtn.setAlignment(Pos.BOTTOM_RIGHT); runBtn.setOnAction(new EventHandler(){ @Override public void handle(ActionEvent e){ rsaVal = new BigInteger(rsaIn.getText()); dVal = Sqrt(rsaVal); dResult.setText(dVal.toString()); ddVal = dVal.multiply(dVal); eVal = rsaVal.subtract(ddVal); eResult.setText(eVal.toString()); xVal = dVal.subtract(BigInteger.ONE); bigNVal = rsaVal.subtract(BigInteger.ONE).divide(new BigInteger("2")).subtract(xVal); bigNResult.setText(bigNVal.toString()); } }); root.add(rsaLbl,0,0); root.add(rsaIn,1,0,4,1); root.add(dLbl,0,1); root.add(dResult,1,1,4,1); root.add(eLbl,0,2); root.add(eResult,1,2,4,1); root.add(bigNLbl,0,3); root.add(bigNResult,1,3,4,1); root.add(runBtn,4,4,2,1); } //https://stackoverflow.com/questions/4407839/how-can-i-find-the-square-root-of-a-java-biginteger public static BigInteger Sqrt(BigInteger number){ BigInteger high = BigInteger.ZERO.setBit(number.bitLength()/2); BigInteger low = high; if (number == BigInteger.ZERO){ return BigInteger.ZERO; } for(;;){ BigInteger n = high.add(number.divide(low)).shiftRight(1); //This conditional has been modified; it may have to be reevaluated if there are problems. //As it was, it was seeking the closest of -either- the higher or lower perfect square: //if(n.equals(high) || n.equals(low)){ if(n.equals(low) || n.equals(BigInteger.ZERO)){ return n; } low = high; high = n; } } } css file (name it "bigNumTester.css", place in the same file as the above file (which should be named "BigNumTester.java"): .root { -fx-background-color: gainsboro; -fx-font-family: "Century Gothic"; -fx-font-size: 14pt; } .text { -fx-font-size: 16pt; -fx-font-family: "Century Gothic"; } .text-area { -fx-font-size: 10pt; -fx-font-family: "Century Gothic"; }