Skip to content

Instantly share code, notes, and snippets.

@LanHao0
Created October 6, 2022 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LanHao0/43cafa676ce19b21d1f20ee58f119b5d to your computer and use it in GitHub Desktop.
Save LanHao0/43cafa676ce19b21d1f20ee58f119b5d to your computer and use it in GitHub Desktop.
信息安全课上简单写的RSA加密算法
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