Java中的Koblitz方法
我试图使用Koblitz的方法将Java中的Koblitz方法,java,eclipse,elliptic-curve,Java,Eclipse,Elliptic Curve,我试图使用Koblitz的方法将String转换为ECPoint,Eclipse说我的代码中没有错误,但当我尝试运行我的应用程序时,每次代码到达Koblitz方法时,它都会出错 对不起,我知道这种帖子没有教育意义,但我似乎在我的源代码中找不到问题 public static ECPoint ConvertToPoint(String S){ BigInteger neg = new BigInteger("-1"); BigInteger one = BigInteger.ONE
String
转换为ECPoint
,Eclipse说我的代码中没有错误,但当我尝试运行我的应用程序时,每次代码到达Koblitz方法时,它都会出错
对不起,我知道这种帖子没有教育意义,但我似乎在我的源代码中找不到问题
public static ECPoint ConvertToPoint(String S){
BigInteger neg = new BigInteger("-1");
BigInteger one = BigInteger.ONE;
BigInteger adder = BigInteger.ONE;
BigInteger two = new BigInteger("2");
BigInteger M,MK,Y,s = null, n = BigInteger.ONE,x,x2,x3 = null,B,B2,B3,g,g2,g3;
BigInteger X = null;
BigInteger k = new BigInteger("20");
BigInteger P = new BigInteger("751"); //Prime number (P)
BigInteger a = new BigInteger("-1"); //Curve Parameter a
BigInteger b = new BigInteger("188"); //Curve Parameter b
int e = 1;
int m = 0;
int r;
if (S=="B"){
M = new BigInteger("11");
MK = M.multiply(k);
X = MK.add(adder); // x = m.k + i
Y = (X.multiply(X).multiply(X)).add(a.multiply(X)).add(b); // y = (x^3 + ax + b)
BigInteger C = (P.subtract(one)).divide(two);
while((Y.mod(one) != BigInteger.ZERO) && (P.gcd(Y) != one) && Y.modPow(C, P) != one){
adder = adder.add(one);
X = MK.add(adder);
Y = (X.multiply(X).multiply(X)).add(a.multiply(X)).add(b);
}
s = (P.subtract(one)).divide(two.pow(e));
while (s.mod(two)== BigInteger.ZERO){
e = e+1;
s = (P.subtract(one)).divide(two.pow(e));
}
while (((n.modPow(C, P)) != neg.mod(P)) || (sqrt(n).mod(one)== BigInteger.ZERO)){
n = n.add(one);
}
x = Y.modPow((s.add(one).divide(two)), P);
B = Y.modPow(s, P);
g = n.modPow(s, P);
r = e;
while (B.modPow(two.pow(m), P) != BigInteger.ONE){
m = m+1;
}
if (m>0){
x2 = x.multiply(g.pow(2^(r-m-1)));
x3 = x2.mod(P);
B2 = B.multiply(g.pow(2^(r-m)));
B3 = B2.mod(P);
g2 = g.pow(2^(r-m));
g3 = g.mod(P);
r = m;
}
else if (m==0){
x3 = x;
B3 = B;
g3 = g;
r = e;
}
}
//This is when the program crashes
ECPoint T = new ECPoint(X,x3);
return T;
我看到您的代码存在两个问题,尽管我不知道这是否是导致错误的原因:
- 同样,如我在评论中所述,不要使用
比较字符串。请改用==
或equals(…)
方法。了解equalsIgnoreCase(…)
检查两个对象是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的问题所在李>==
- 大整数也是如此。如果要测试是否相等/不相等,应使用
方法或equals(…)
方法;如果要询问有关所看到的错误或异常的问题,应使用compareTo(…)
或,您将要发布带有问题的消息,并为我们指出是哪一行引起了问题。此外,千万不要这样做:
。而是使用字符串方法if(S==“B”)
来比较字符串,equals
。了解if(“B”.equals(S))
检查两个对象是否相同,这不是您感兴趣的。另一方面,==
和equals
方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的问题所在。@hovercraftfullofels:我已经尝试了你的建议,刚刚调试了程序。我发现问题似乎出现在第一个while中:equalsIgnoreCase
我的代码正确吗?日志说明如下:while((Y.mod(one)!=biginger.ZERO)&&(P.gcd(Y)!=one)&&Y.modPow(C,P)!=one)
和FATAL EXCEPTION:main
以及更多,但是前两行错误是正确的,我在第二次查看时确实看到了,谢谢,但是,您看到了什么错误消息或堆栈跟踪?请将整个消息作为对您的问题的编辑发布。我已经按照您的说明修复了我的代码,但现在我从logcat收到了这样一条消息:java.lang.NullPointerException:affineX==null
和等待\u并发\u GC阻塞94ms
。我得到了很多。我在论坛里搜索了一下,找到了。我似乎不知道我的源代码在哪里创建gcI。我在第一次GC\u并发释放
时遇到了问题。程序似乎在第一个
中循环,而
无法退出。为了使koblitz方法成功,是否需要满足特定的曲线参数规范?我使用什么语法检查不平等性?例如,我想检查
,我可以使用如果(A!=B)
?这是真的吗?如果(!A.equals(B))