7
这个rsa 算法哪里出错了呢? 为什么算的不对? - OSCHINA
source link: https://www.oschina.net/question/3775878_2320413
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
这个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>>
osc_07305217
12/17 19:31
引用来自“猴子叫你一声敢答应吗”的评论
N和M的取值太笑了,阁下的逻辑大概看了下是正确的,但是你要加密的是ascii你的欧拉函数的值应该大于你要加密的单个数值。e≡ 1 (mod φ(n))
1. 如猴子所说,明文要小于密钥长度(小于N)。
2. 关于模幂运算,要作为大数参与运算;对byte数组每位进行模幂,有点奇怪;运算容易溢出(显然,你也注意到了)。
tcxu
前天 20:36
引用来自“WindSpeed”的评论
这颜色能不能调一下,我的天 五颜六色的
调一下颜色:
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();
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK