import java.net.*;
import java.util.Arrays;
import javax.crypto.*;
+import javax.crypto.spec.*;
+import java.security.SecureRandom;
class CloudComm {
String baseurl;
Cipher encryptcipher;
Cipher decryptcipher;
Mac mac;
+ byte[] salt;
+ SecretKeySpec key;
+ static final int SALT_SIZE = 8;
+
- CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt, Mac _mac) {
+ CloudComm() {
+ }
+
+ CloudComm(String _baseurl, String password) {
this.baseurl=_baseurl;
- this.encryptcipher = _encrypt;
- this.decryptcipher = _decrypt;
- this.mac = _mac;
+ initCloud(password);
+ }
+
+ private void initKey(String password) {
+ try {
+ salt=new byte[SALT_SIZE];
+ PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
+ SecretKey tmpkey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
+ this.key = new SecretKeySpec(tmpkey.getEncoded(), "AES");
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new Error("Failed generating key.");
+ }
+ }
+
+ private void initCloud(String password) {
+ try {
+ initKey(password);
+ mac = Mac.getInstance("HmacSHA256");
+ mac.init(key);
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new Error("Failed To Initialize Ciphers");
+ }
}
private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
return new URL(urlstr);
}
- public Slot[] putSlot(Slot slot, int max) throws IOException{
+ public Slot[] putSlot(Slot slot, int max) {
try {
long sequencenumber=slot.getSequenceNumber();
byte[] bytes=slot.encode(mac);
-
+
URL url=buildRequest(true, sequencenumber, max);
URLConnection con=url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
OutputStream os=http.getOutputStream();
os.write(bytes);
System.out.println(http.getResponseMessage());
-
+
InputStream is=http.getInputStream();
DataInputStream dis=new DataInputStream(is);
byte[] resptype=new byte[7];
throw new Error("putSlot failed");
}
}
+
+ /*
+ Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+ encryptCipher.init(Cipher.ENCRYPT_MODE, secret);
+ Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+ decryptCipher.init(Cipher.DECRYPT_MODE, secret);
+ */
public Slot[] getSlots(long sequencenumber) {
try {
http.connect();
System.out.println(http.getResponseMessage());
InputStream is=http.getInputStream();
-
+
DataInputStream dis=new DataInputStream(is);
byte[] resptype=new byte[7];
dis.readFully(resptype);
throw new Error("getSlots failed");
}
}
-
+
Slot[] processSlots(DataInputStream dis) throws IOException {
int numberofslots=dis.readInt();
int[] sizesofslots=new int[numberofslots];
Slot[] slots=new Slot[numberofslots];
- for(int i=0;i<numberofslots;i++)
+ for(int i=0; i<numberofslots; i++)
sizesofslots[i]=dis.readInt();
- for(int i=0;i<numberofslots;i++) {
+ for(int i=0; i<numberofslots; i++) {
byte[] data=new byte[sizesofslots[i]];
dis.readFully(data);
slots[i]=Slot.decode(data, mac);