Created
October 6, 2022 10:49
-
-
Save LanHao0/43cafa676ce19b21d1f20ee58f119b5d to your computer and use it in GitHub Desktop.
信息安全课上简单写的RSA加密算法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.util.Arrays; | |
public class Main { | |
public static void main(String[] args) { | |
int p = 34; | |
int q = 151; | |
int e = 7; | |
int msg = 2022; | |
int d = 0; | |
int k = 0; | |
int tmp = 65535; | |
int n = p * q; | |
int fn = (p - 1) * (q - 1); | |
boolean condition = false; | |
for (int i = 0; i < tmp; i++) { | |
k = 0; | |
for (int j = 0; j < tmp; j++) { | |
if (e * d == k * fn + 1) { | |
condition = true; | |
break; | |
} else { | |
k++; | |
} | |
} | |
if (condition) { | |
break; | |
} | |
d++; | |
} | |
System.out.println("d = " + d); | |
System.out.println("k = " + k); | |
int[] pk = {n, e}; | |
int[] sk = {n, d}; | |
BigInteger b_m=new BigInteger(String.valueOf(msg)); | |
BigInteger b_n=new BigInteger(String.valueOf(n)); | |
BigInteger encd= b_m.pow(e).mod(b_n); | |
BigInteger b_de=encd.pow(d).mod(b_n); | |
System.out.println("bencrypted:"+encd); | |
System.out.println("bendecrypted:"+b_de); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment