Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k 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 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...