-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutCypher.java
226 lines (213 loc) · 10.6 KB
/
OutCypher.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//reference code:
// PrivateKeyString = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAwAmm5HCBJDsONi/z\r" +
// "Kt+PFyVz6sxjpksIq+/80Cu8OxTNbV1FPiK93VxCZdzEfN9SpF4bB6fGRSA0eqCr\r" +
// "LmcBoQIDAQABAkByBgkbQJXdblc843Gt9jnfE3TlaGCOv6SxeniRHjbQmLsLVi7+\r" +
// "vHdnYkNGM+zmJRon/O4+pckgVyaQeyDgMSqFAiEA+52HGdpBCRaKx3CHu7VETVsu\r" +
// "Wqc/TRgITBPmhbqaSAcCIQDDYlgwtoIRzTH/86xJt+bpVQINwkslihAXxVoKiVTv\r" +
// "FwIgT5G4aKeJi4syZfGjKuwe5mugVBCxxvqDnTNp4f5pzb8CIDdfDpk3j+MVoP7l\r" +
// "gsUdjh5ATiWyE3PfDbJ+5oan5t8fAiEA7lIowEJj3qzB1xHmbdnWD6D4ZFMYIrPn\r" +
// "+QnTKGNzo8k=\r";
// // Generate new key
// KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
// PrivateKey privateKey = keyPair.getPrivate();
// privateKey = loadPrivateKey(PrivateKeyString);
//
// Log.d(TagName, "privateKey:"+privateKey);
// String plaintext = "test123\n";
//
// // Compute signature
// Signature instance = null;
// instance = Signature.getInstance("SHA1withRSA");
// instance.initSign(privateKey);
// instance.update((plaintext).getBytes());
// byte[] signature = instance.sign();
//
// // Compute digest
// MessageDigest sha1 = MessageDigest.getInstance("SHA1");
// byte[] digest = sha1.digest((plaintext).getBytes());
//
// // Encrypt digest
// Cipher cipher = Cipher.getInstance("RSA");
// cipher.init(Cipher.ENCRYPT_MODE, privateKey);
// byte[] cipherText = cipher.doFinal(digest);
//
// Display results
// Log.d(TagName, "Input data: " + plaintext);
// Log.d(TagName, "Digest: " + bytes2String(digest));
// Log.d(TagName, "Cipher text: " + bytes2String(cipherText));
// Log.d(TagName, "Signature: " + bytes2String(signature));
package edu.nctu.wirelab.testsignalv1;
import android.util.Base64;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class OutCypher {
private static final String TagName = "OutCypher";
private String PrivateKeyString;
private PrivateKey privateKey;
public void setKey(String FileContent){
//Log.d(TagName, "FileContent:"+FileContent);
String[] tokens = FileContent.split("\n");
PrivateKeyString = "";
for( String tt:tokens ){
//Log.d(TagName, "tt:"+tt);
if(tt.equals("-----END PRIVATE KEY-----") || tt.equals("-----BEGIN PRIVATE KEY-----") ){
}
else{
PrivateKeyString = PrivateKeyString+tt+"\r";
}
}
privateKey = loadPrivateKey(PrivateKeyString);
}
public OutCypher(){
// the private key that server provide in pkcs8 format, such as
PrivateKeyString = "MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQCod1qGGKAAWf14\r" +
"0Q8h4indpdBYxwYzoa20bGGUPEowafELXcBrgk87e4jaYMO5EwDHULJeb1UoSgM3\r" +
"uV/etdahHP4TPsMHDSW7PHZY7AoKzE/7HrEzGaYcHrXUv+muvcvYVbm1rigE4klD\r" +
"PovEaxCaKhtKaPRRg0U4g65UKQhAjpMKWDU9eqTkfsnoDy3BCD8Y3OCtSW/nncGX\r" +
"V8pU1Myrpx3ghhT2A4iSDCBGzpLBk3IRHDMd79kEOYF8WCisxRsN1J0wXwif8X+W\r" +
"MYuqAMWXZVBfMqeLvIISl6wUF4vHddZxWYK8P6Z5Xhw385vY9RPeViUSoSljsz5Q\r" +
"YFbnmtdIDWLZBoDUlf5HCFcHFIlwbLKFt445Q2oU1eGf14eQKDai9pHlsjiy0Sl4\r" +
"y+rTZU6F/zOlinWYOKNJonlqqPw2zIbrB1R9U+nT6cpqCqn9aPiyA8cDFWGiA+/k\r" +
"PVbNS89hWWnBb/8ETNJEEJJp+9IN6DYmdOF+t8h/VL0sq/DgYxMfOteGZhjtZ4Jc\r" +
"qlm8iVIPmNbyqe0VDwD2eFw+AlSwCT1tEO4K6JvCWxL4y3ShaRjLslgPvhULrlIX\r" +
"e3ho1J4gkF/1dMrqbQPB8KeW4rurYgPqKm4NgSMMf/lqoGHt4a9jRBIXZgWgGfj2\r" +
"txY9lRR0dP07Ql6gC+cMoc3wWkN9RQIDAQABAoICAGeoUN3z5vdwZ8NgN4Rsyb/l\r" +
"GAaYJ5u7XsuGaKAyXJ/ff/6zaAN+wcvi3jkokyWcqLz7TVVIAcjxiXtPJ8s8bdzi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jlg2JoIIiQZYf/\r" +
"6wKCAQBQAz+qIsDCtd3Jb+k0oub3CPwToov4mqvVRBX5+JwWC/SWvldRM2UVXlz8\r" +
"rlb1yspoeXvByW7SPVnAjp6dsNSIflC78+1yZVz6ZcBMGpxFxuhJssX3jlxIqX6L\r" +
"ewX7riBeyJCJudIFJH0VxEfKV9RxuXHrfNE3nfzevxH7UH1Xs9l/viMuUeJOTgHE\r" +
"qO4wWZe+3a+qKd7yhGhSPAZnIuIS86lgyb/1NgVC08Dimq/nqCHLvvDiFgePqCrH\r" +
"Eu5ha7U7qe72ptaEzSrAXnkFyl7AIkTIPt5G6N7uA9Ia9Y2CONW+aJlsNFBuzJUM\r" +
"XrlOZGKTmOXNiIcG91pHXatm9hbW\r";
privateKey = loadPrivateKey(PrivateKeyString);
}
public byte[] SignString(String plaintext){
byte[] signature = null;
Signature instance = null;
try {
instance = Signature.getInstance("SHA1withRSA");
instance.initSign(privateKey);
instance.update((plaintext).getBytes());
signature = instance.sign();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (SignatureException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return signature;
}
private PrivateKey loadPrivateKey(String key64) {
byte[] clear = Base64.decode(key64, Base64.DEFAULT);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
KeyFactory fact;
try {
fact = KeyFactory.getInstance("RSA");
PrivateKey priv = fact.generatePrivate(keySpec);
Arrays.fill(clear, (byte) 0);
return priv;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return null;
}
private static String bytes2String(byte[] bytes) {
StringBuilder string = new StringBuilder();
for (byte b : bytes) {
String hexString = Integer.toHexString(0x00FF & b);
string.append(hexString.length() == 1 ? "0" + hexString : hexString);
}
return string.toString();
}
private byte[] openAndReadFile(){
File file = new File(MainActivity.LOGPATH+"test");
int size = (int) file.length();
byte[] bytes = new byte[size];
Log.d(TagName, "size: " + size);
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Log.d(TagName, "bytes: " + bytes2String(bytes));
return bytes;
}
public void openAndWriteFileInBytes(byte[] bytes, String FilePath){
File file = new File(FilePath);
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(bytes);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Log.d(TagName, "bytes: " + bytes2String(bytes));
}
}