-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathKoblitz.java
72 lines (56 loc) · 2.32 KB
/
Koblitz.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*BASED ON PAPER: https://informatika.stei.itb.ac.id/~rinaldi.munir/Kriptografi/2014-2015/IJCSE10-02-05-08.pdf*/
package cryptography.encoding.koblitz;
import java.math.BigDecimal;
import cryptography.ciphers.ellipticCurve.EllipticCurve;
import java.math.RoundingMode;
public class Koblitz {
public static String[] encode(String input, int k, EllipticCurve E) {
BigDecimal m = stringEncoding(input, E); // m = sum( input[a] * b^a );
BigDecimal x = BigDecimal.ZERO;
BigDecimal y2 = BigDecimal.ZERO;
BigDecimal y = BigDecimal.ZERO;
BigDecimal s = BigDecimal.ZERO;
for (int a = 1; a < k; a++) {
x = m.multiply(new BigDecimal(k)).add(new BigDecimal("4")); // x = m * k + a;
s = x.pow(3).add(E.A.multiply(x)).add(E.B).remainder(E.P); // s = (x^3 + A * x + B) % P;
//noinspection ComparatorResultComparison
while (y.compareTo(E.P) == -1) {
y2 = y.pow(2).remainder(E.P); //y2 = y^2 % P
if (y2.equals(s)) {
break;
}
y = y.add(BigDecimal.ONE);
}
}
return new String[]{x.toString(), y.toString()};
}
public static String decode(String[] point, int k, EllipticCurve E) {
BigDecimal x = new BigDecimal(point[0]);
BigDecimal m = x.divide(new BigDecimal(Integer.toString(k)), 0, RoundingMode.FLOOR);
StringBuilder output = new StringBuilder();
while (!m.equals(BigDecimal.ZERO)) {
output.append((char) m.remainder(E.b).intValueExact());
m = m.divide(E.b, RoundingMode.FLOOR);
}
return output.toString();
}
public static String decode(String point, int k, EllipticCurve E) {
BigDecimal x = new BigDecimal(point);
BigDecimal m = x.divide(new BigDecimal(Integer.toString(k)), 0, RoundingMode.FLOOR);
StringBuilder output = new StringBuilder();
while (!m.equals(BigDecimal.ZERO)) {
output.append((char) (m.remainder(E.b).intValueExact()));
m = m.divide(E.b, RoundingMode.FLOOR);
}
return output.toString();
}
public static BigDecimal stringEncoding(String str, EllipticCurve E) {
BigDecimal mappedStringValue = BigDecimal.ZERO;
for (int a = 0; a < str.length(); a++) {
int ascii = str.charAt(a);
String stringedInteger = Integer.toString(ascii);
mappedStringValue = mappedStringValue.add(E.BIT_MAX_VALUE_128.pow(a).multiply(new BigDecimal(stringedInteger)));
}
return mappedStringValue;
}
}