-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverskip.java
97 lines (87 loc) · 2.66 KB
/
serverskip.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
// code inspired by BurrisJavaCrypto
import java.util.*;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyAgreement;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.*;
import java.security.spec.*;
import java.io.*;
import java.net.Socket;
// key set up done, add file read, encrypt, decrypt, and send
public class serverskip{
//file stuff
PrintWriter pw;
//keystuff
byte[] barr;
KeyPairGenerator kpg;
KeyFactory kf;
KeyPair kp;
PublicKey pk;
X509EncodedKeySpec spec509;
KeyAgreement secretK;
// filekey stuff
DESKeySpec dks;
SecretKeyFactory skf;
SecretKey skDES;
FileInputStream fis;
FileOutputStream fos;
// key exchange client/server
public KeyPair createServerKey(){
try {
//create server key pair
kpg=KeyPairGenerator.getInstance("DH");
kpg.initialize(1024);
kp=kpg.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return kp;
}
public byte[] getSecretSessionKey(KeyPair serverkeypair, PublicKey pk){
try {
secretK=KeyAgreement.getInstance("DH");
try {
secretK.init(kp.getPrivate());
secretK.doPhase(pk,true);
barr=secretK.generateSecret();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return barr;
}
// file encryption and transmission
public SecretKey getDeskey(byte[] secret){
try {
dks=new DESKeySpec(secret);
try {
skf=SecretKeyFactory.getInstance("DES");
try {
skDES=skf.generateSecret(dks);
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return skDES;
}
public void createFile(){
}
}