-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbedding.java
More file actions
92 lines (79 loc) · 2.3 KB
/
Embedding.java
File metadata and controls
92 lines (79 loc) · 2.3 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Embedding class to convert message to point.
*/
package org.Stan.Crypt.ECC;
import java.util.Vector;
import java.math.BigInteger;
import java.security.SecureRandom;
/**
*
* @author Stan Chen
*/
public class Embedding {
BigInteger k, n, p, j;
private SecureRandom random = new SecureRandom();
public Embedding() {
}
public Embedding(Curve curve) {
this.p = curve.getZp();
this.k = new BigInteger("20");
getP();
j = new BigInteger(3, random);
}
public Vector embeddingMessage(String message, Curve curve) {
Vector msgInt = new Vector();
msgInt = msgBlocking(message);
// System.out.println("msgInt:"+msgInt);
Vector messagePoints = new Vector();
for (int i = 0; i < msgInt.size(); i++) {
messagePoints.addElement(embedding(
(BigInteger) msgInt.elementAt(i), curve));
}
return messagePoints;
}
private void getP() {
while (!p.mod(BigInteger.valueOf(4)).equals(BigInteger.valueOf(3))) {
p = BigInteger.probablePrime(p.bitLength() - 1, random);
}
}
private int getBlockSize() {
return (p.subtract(k)).divide(k).toString().length() / 3 - 1;
}
private Point embedding(BigInteger message, Curve curve) {
Point messagePoint = new Point();
messagePoint.setX(getXj(message));
messagePoint.setY(getZj(messagePoint.getX(), curve));
return messagePoint;
}
// For j=0, …, k-1
// Set xj = m*k + j ; wj = x j 3 + a xj + b; zj = wj((p+1)/4)
private BigInteger getXj(BigInteger message) {
return message.multiply(k).add(j);
}
private BigInteger getZj(BigInteger xj, Curve curve) {
BigInteger wj = xj.modPow(BigInteger.valueOf(3), curve.getZp())
.add(xj.multiply(curve.getA())).add(curve.getB())
.mod(curve.getZp());
return wj.modPow(
(p.add(BigInteger.valueOf(1)).divide(BigInteger.valueOf(4))),
curve.getZp());
}
private Vector msgBlocking(String message) {
int blocksize = getBlockSize();
Vector msgs = new Vector();
for (int i = 0; i < message.length() / blocksize + 1; i++) {
BigInteger tempInt;
if (message.length() >= (i + 1) * blocksize) {
tempInt = new BigInteger(message.substring(i * blocksize,
(i + 1) * blocksize).getBytes());
} else {
tempInt = new BigInteger(message.substring(i * blocksize,
message.length()).getBytes());
}
if (tempInt != null) {
msgs.addElement(tempInt);
}
}
return msgs;
}
}