我的RSA解密不返回纯文本

时间:2019-07-14 14:21:01

标签: java

我使用JavaFX制作了RSA加密/解密GUI程序 生成公钥和私钥似乎效果很好,但是解密不返回我加密的纯文本。 我不知道为什么会这样,也许我对RSA算法有误解,所以请查找mt代码有任何问题。 文本文件“ Pnumlist”包含我从Wikipedia复制的素数列表。

我将私钥设置为n,e,将公钥设置为n,d 据我所知,如果我们将纯文本设置为M,将加密文本设置为C,(整数)

C = M^e (mod n)

M = C^d (mod n)

我对吗?

package application; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Random; 
import java.util.Scanner; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.control.TextArea; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 
public class Main extends Application { 
  static int[] arr = new int[120000]; 
  private static int n; 
  private static int e; 
  private static int d; 
  private static int C; 
  private static int M; 
  private static int n1; 
  private static int n2; 
  private static void SWAPint(int a, int b) { 
    if(a<b) { 
      int tmp = a; 
      a=b; 
      b=tmp; 
   } 
} 
private static int getd() { 
  int D; 
  for(D=1; D<n-1; D++) { 
    if((e*D)%(n-1)==1) { 
      break; 
   } 
} 
return D; 
} 
private static int GCDr(int a, int b) { 
  if(b==0) { 
    return a; 
    }else { 
    return GCDr(b, a%b); 
 } 
} 
private static int getCoprime(int E) { 
  int j; 
  for(j=2; j<E; j++) { 
    if(GCDr(E, j)==1) { 
      break; 
   } 
} 
return j; 
} 
private static void keymaker() { 
  File f = new File("Pnumlist.txt"); 
  try { 
    Scanner scan = new Scanner(f); 
    int i=0; 
    while(scan.hasNext()) { 
      arr[i]=scan.nextInt(); 
      i++; 
   } 
   int Pnum[] = new int[i]; 
   for(int j=0; j<Pnum.length; j++) { 
     Pnum[j]=arr[j]; 
  } 
  Random slct = new Random(); 
  n1 = Pnum[slct.nextInt(Pnum.length)]; 
  n2 = Pnum[slct.nextInt(Pnum.length)]; 
  SWAPint(n1, n2); 
  n=n1*n2; 
  int Onum = (n1-1)*(n2-1); 
  e=getCoprime(Onum); 
  d=getd(); 
  } catch (FileNotFoundException e) { 
  e.printStackTrace(); 
} 
} 
@Override 
public void start(Stage primaryStage) { 
  try { 
    Pane root = new Pane(); 
    Scene scene = new Scene(root,800,400); 
    TextField Ptext = new TextField(); 
    Label Pl = new Label("Enter PlainText(Integer)"); 
    Button Encrypt = new Button("Encryption"); 
    Encrypt.setPrefSize(100, 20); 
    Encrypt.setLayoutX(30); 
    Encrypt.setLayoutY(100); 
    Pl.setLayoutX(30); 
    Pl.setLayoutY(30); 
    Ptext.setPrefSize(200, 20); 
    Ptext.setLayoutX(30); 
    Ptext.setLayoutY(60); 
    root.getChildren().add(Ptext); 
    root.getChildren().add(Pl); 
    root.getChildren().add(Encrypt); 
    TextField Ctext = new TextField(); 
    Label Cl = new Label("Enter CryptoText(Integer)"); 
    Button Decrypt = new Button("Decryption"); 
    Decrypt.setPrefSize(100, 20); 
    Decrypt.setLayoutX(30); 
    Decrypt.setLayoutY(340); 
    Cl.setLayoutX(30); 
    Cl.setLayoutY(270); 
    Ctext.setPrefSize(200, 20); 
    Ctext.setLayoutX(30); 
    Ctext.setLayoutY(300); 
    root.getChildren().add(Ctext); 
    root.getChildren().add(Cl); 
    root.getChildren().add(Decrypt); 
    TextArea console = new TextArea(); 
    ScrollPane con = new ScrollPane(console); 
    console.setPrefSize(300, 380); 
    console.setLayoutX(250); 
    console.setLayoutY(10); 
    root.getChildren().add(console); 
    Encrypt.setOnAction(rct->{ 
      String tmpP = Ptext.getText(); 
      console.appendText("Input Integer: "+tmpP+"\n"); 
      console.appendText("--------Process--------"+"\n"); 
      M = Integer.parseInt(tmpP); 
      keymaker(); 
      console.appendText("generated primes: "+n1+", "+n2+"\n"); 
      console.appendText("Calculated key n: "+n+"\n"); 
      console.appendText("Calculated key e: "+e+"\n"); 
      console.appendText("Calculated key d: "+d+"\n"); 
      C= (int) Math.pow(M, e)%n; 
      console.appendText("--------Crypted Integer--------"+"\n"); 
      console.appendText(Integer.toString(C)); 
   }); 
   Decrypt.setOnAction(rct->{ 
     String tmpP = Ctext.getText(); 
     console.appendText("\n\nInput Integer: "+tmpP+"\n"); 
     console.appendText("--------Process--------"+"\n"); 
     console.appendText("--------Decrypted Integer--------"+"\n"); 
     int cusM = (int) Math.pow(C, d)%n; 
     console.appendText(Integer.toString(cusM)); 
  }); 
  primaryStage.setResizable(false); 
  scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
  primaryStage.setScene(scene); 
  primaryStage.show(); 
  } catch(Exception e) { 
  e.printStackTrace(); 
} 
} 
public static void main(String[] args) { 
  launch(args); 
} 
}

0 个答案:

没有答案