-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMHKCrypto.java
More file actions
133 lines (121 loc) · 3.48 KB
/
Copy pathMHKCrypto.java
File metadata and controls
133 lines (121 loc) · 3.48 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package linkedListCrypto;
import java.math.BigInteger;
import java.util.*;
public class MHKCrypto {
private DoublyLinkedList <BigInteger> w;
public DoublyLinkedList <BigInteger> B;
private BigInteger q = new BigInteger("0");
private BigInteger r = new BigInteger("0");
private int listSize = 0;
public MHKCrypto(){
w = new DoublyLinkedList<>();
B = new DoublyLinkedList<>();
}
// Generate private key
public void genPrivateKey(int length) {
BigInteger sum = new BigInteger("0");
Random random = new Random();
for(int i=0;i<(length*8);i++){
DoubleNode<BigInteger> temp = w.getHead();
if(temp == null){
sum = BigInteger.ZERO;
} else {
while(temp!=null) {
sum = sum.add(temp.getC());
temp = temp.getNext();
}
}
sum = sum.add(new BigInteger(random.nextInt(5)+1+""));
w.addAtEnd(sum);
}
}
//Generate public key
public void genPublicKey(){
q = genQ();
r = genR();
DoubleNode<BigInteger> temp = w.getHead();
while(temp!=null) {
BigInteger x;
x = temp.getC().multiply(r).mod(q);
B.addAtEnd(x);
temp = temp.getNext();
}
}
private BigInteger genQ(){
DoubleNode<BigInteger> temp = w.getHead();
BigInteger sum = new BigInteger("0");
Random r = new Random();
while(temp!=null) {
sum = sum.add(temp.getC());
temp = temp.getNext();
}
return sum.add(new BigInteger(""+r.nextInt(5)+1));
}
private BigInteger genR(){
Random rand = new Random();
BigInteger r;
do {
r = q.subtract(new BigInteger(rand.nextInt(1000) + ""));
} while ((r.compareTo(new BigInteger("0")) > 0) && (q.gcd(r).intValue() != 1));
return r;
}
//Encrypt(private key, message)
public BigInteger encrypt(String inputStr) {
listSize = inputStr.length()*8;
BigInteger encrpytedmsg = new BigInteger("0");
int count =7;
int[] input = new int[8];
Arrays.fill(input,0);
for(int i=0;i<inputStr.length();i++){
int x = inputStr.charAt(i);
while(x!=0){
input[count] = x%2;
x = x/2;
count = count -1;
}
for(int j=0;j<8;j++){
BigInteger value = B.get(i * 8 + j);
if(input[j]!=0){
encrpytedmsg = encrpytedmsg.add(value);
}
}
Arrays.fill(input,0);
count =7;
}
return encrpytedmsg;
}
//decrypt
public String decrypt(BigInteger encrypt) {
BigInteger b1 = encrypt.mod(q);
BigInteger b2 = r.modInverse(q);
BigInteger b3 = b1.multiply(b2);
BigInteger value = b3.mod(q);
int[] bitmask = new int[listSize];
int i = listSize -1;
while(value.compareTo(BigInteger.ZERO) != 0) {
BigInteger encrpt_key = w.get(i);
if(value.compareTo(encrpt_key) >=0) {
value = value.subtract(encrpt_key);
bitmask[i] = 1;
}
i=i-1;
}
int len = bitmask.length;
String concat="";
double x=0;
while(len > 0) {
len = len - 1;
if(len%8==0) {
concat = concat + (char)x;
x=0;
} else {
if (bitmask[len]==1) {
x = x+Math.pow(2,8-(len%8)-1);
}
}
}
StringBuffer buffer = new StringBuffer(concat);
StringBuffer x1 = buffer.reverse();
return x1.toString();
}
}