Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

这个rsa算法哪里出错了呢? 为什么算的不对

public static void main(String[] args) {
 int x = 3;
 int y = 11;
 int n = x * y;
 int m = (x-1) * (y-1); // 20
 //e * d - 1 = y * m 
 int e = 3;
 int d = 7;
 System.out.println((e * d) % m); // 1
 //public  n  e //private n d
 byte[] bytes = "a".getBytes(StandardCharsets.UTF_8);
 //a^e % n = b 加密
 pbyte(bytes);
 int[] bytesne = new int[bytes.length];
 for (int i = 0; i <bytes.length ; i++) {
 long c = bytes[i];
 for (int j = 1; j <e; j++) {
 c*=bytes[i];
 }
 bytesne[i] = (byte)(c % n);
 }
 pint(bytesne);
 //b^d % n = a
 //a^d % n = b 解密
 byte[] rr = new byte[bytes.length];
 for (int i = 0; i <bytesne.length ; i++) {
 long c = bytesne[i];
 for (int j = 1; j <d; j++) {
 c*=bytesne[i];
 }
 rr[i] = (byte)(c % n);
 }
 pbyte(rr);
 System.out.println(new String(rr));
 Integer.toBinaryString(n);
}
public static void pbyte(byte[] bytes){
 for (byte aByte : bytes) {
 System.out.print(aByte + ">>");
 }
 System.out.println();
}
public static void pint(int[] bytes){
 for (int aByte : bytes) {
 System.out.print(aByte+ ">>");
 }
 System.out.println();
}

1
97>>
25>>
31>>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

RSA要求明文m<n,而"a"=97>33。
计算没问题,解密出来31≡97 mod 33。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...