diff --git a/Cryptonite/src/Client/Client_File_Upload.java b/Cryptonite/src/Client/Client_File_Upload.java index c2b3ba71..23153070 100644 --- a/Cryptonite/src/Client/Client_File_Upload.java +++ b/Cryptonite/src/Client/Client_File_Upload.java @@ -104,7 +104,7 @@ public void run() if (_mod == 0) { - key = new Client_Get_Group_Key().running(_gpCode); + key = new Client_Get_Group_Key().running(_gpCode,0); _crypto = new Crypto(Crypto_Factory.create("AES256", Cipher.ENCRYPT_MODE, key)); } else diff --git a/Cryptonite/src/Client/Client_Get_Group_Key.java b/Cryptonite/src/Client/Client_Get_Group_Key.java index 0956a0c8..8c57625d 100644 --- a/Cryptonite/src/Client/Client_Get_Group_Key.java +++ b/Cryptonite/src/Client/Client_Get_Group_Key.java @@ -1,9 +1,17 @@ package Client; import java.io.IOException; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.spec.EncodedKeySpec; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; import java.util.Base64; +import javax.crypto.BadPaddingException; import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; @@ -12,6 +20,7 @@ import Crypto.KeyReposit; import Function.Function; import Function.PacketRule; +import Server.Server_Code_Manager; public class Client_Get_Group_Key implements PacketRule { @@ -21,31 +30,66 @@ public Client_Get_Group_Key() } - public SecretKey running(String groupCode) + public SecretKey running(String groupCode,int keynum) //keynum 0 = fileupload : 그냥 그룹aeskey 리턴 //non-zero = filedownload : 해당 keynum의 key { Client_Server_Connector csc = Client_Server_Connector.getInstance(); - KeyReposit reposit = KeyReposit.getInstance(); - System.out.println(Base64.getEncoder().encodeToString(reposit.get_aesKey().getEncoded())); + //KeyReposit reposit = KeyReposit.getInstance(); + //System.out.println(Base64.getEncoder().encodeToString(reposit.get_aesKey().getEncoded())); //Crypto crypto = new Crypto(Crypto_Factory.create("AES256", Cipher.DECRYPT_MODE, reposit.get_aesKey())); byte[] event = new byte[1024]; event[0] = GET_GROUP_KEY; - Function.frontInsertByte(1, groupCode.getBytes(), event); + event[1] = (byte) keynum; + + Function.frontInsertByte(2, groupCode.getBytes(), event); System.out.println("get Key"); - SecretKey GpKey = null; + byte[] GpKey = null; + int sklen = 0; + byte[] sk = null; + SecretKey ret = null; try { csc.send.setPacket(event).write(); - GpKey = new SecretKeySpec(csc.receive.setAllocate(32).read().getByte(), "AES"); + if (keynum !=0) //download + { + GpKey = csc.receive.setAllocate(128).read().getByte(); + + sklen = Function.byteArrayToInt(csc.receive.setAllocate(4).read().getByte()); + sk = csc.receive.setAllocate(sklen).read().getByte(); + + EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(sk); + KeyFactory generator = KeyFactory.getInstance("RSA"); + PrivateKey privateKey = generator.generatePrivate(privateKeySpec); + + byte[] t = Crypto_Factory.create("RSA1024", Cipher.DECRYPT_MODE, privateKey).doFinal(GpKey); + + ret = new SecretKeySpec(t,"AES"); + } + else //upload + { + ret = new SecretKeySpec(csc.receive.setAllocate(32).read().getByte(),"AES"); + } } catch (IOException e) { e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvalidKeySpecException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (BadPaddingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - return GpKey; + return ret; } } diff --git a/Cryptonite/src/Client/Client_Group_Main.java b/Cryptonite/src/Client/Client_Group_Main.java index 8efc9d26..d1e14928 100644 --- a/Cryptonite/src/Client/Client_Group_Main.java +++ b/Cryptonite/src/Client/Client_Group_Main.java @@ -112,17 +112,18 @@ public void click() } public String noExtensionName() { if(!fileName.endsWith(".cnmc")){ - System.out.println("fileName : "+fileName+"/n"); +// System.out.println("fileName : "+fileName+"/n"); StringTokenizer st2 = new StringTokenizer(fileName, "#"); String filename = ""; - st2.nextToken(); + keynum = Integer.parseInt(st2.nextToken()); while(st2.hasMoreTokens()) { - filename +=st2.nextToken(); + filename = filename + st2.nextToken() + "#"; } + filename.substring(0, filename.length()-1); //마지막 # 떼어냄 fileName=filename; } - System.out.println("filename2222222 : "+fileName+"/n"); +// System.out.println("filename2222222 : "+fileName+"/n"); return fileName.substring(0, fileName.length() - 5); } @@ -132,6 +133,7 @@ public String noExtensionName() { public JButton button; public String fullPath; public String fileName =""; + public int keynum = 0; } private ArrayList _btnList; @@ -527,7 +529,7 @@ public void actionPerformed(ActionEvent e) } else { - key = new Client_Get_Group_Key().running(_gpCode); + key = new Client_Get_Group_Key().running(_gpCode,_btnList.get(i).keynum); } new Client_File_Download().requestFile(_btnList.get(i).fullPath, _downloadPath + "\\" + _btnList.get(i).fileName, key); diff --git a/Cryptonite/src/Client/test.java b/Cryptonite/src/Client/test.java index 0bb2651f..49c26e33 100644 --- a/Cryptonite/src/Client/test.java +++ b/Cryptonite/src/Client/test.java @@ -17,12 +17,18 @@ public static void main(String[] argv) { try{ - String q = "C5lUH+jiwyiyUhzRxwdcelmQI6r3eCp6d9356qvpySVXO8Od74Qndc/GnJ6uiSX4Gn6Y/Smv8w1yLak8u4H5Mg9T6LvEH+QwVx8DFfTIyx/4iI04oqv8GSpQm3jY/zFhC6g+p+dnqRu00RIYJ0EDtBeoBsJus95x0O27C1OTLSI="; + String q = "cDpm3T1BdccR9YII14iOW/Gp25qDlCtHCziV3zzGb8yISGrxNSa/+a9nnNb3EuDRr4Oewwlk5WQ2zVRGV2dm04AiNzCwAL0uAa8DGXJnMGI2F+6zpFcjcNIUZP/xXzY7kneRBArC786y/SKpelGOQ8wgxCEsvRsDuMJJ2VWqSHo="; byte[] w = Base64.getDecoder().decode(q); - String sk = "MIICdAIBADANBgkqhkiG9w0BAQEFAASCAl4wggJaAgEAAoGBAKbGXfgDm4stDOz6KBP9ul8ZeFiD/Kwc4aUqz438IImcjLy/afTQE8lA+tXnN97dWAJw3JYF3l+cPOvobc+UFGPB9skJqsdnUAV9vw+nTLbYRHEW7Ld1ChyWfmQd5eJnMAZkzB4kaMabLkUQ7BrlJVxlrBjFIbpuzEL7Izmug4o7AgMBAAECgYBEsA6jx1iYycU5FQ7MrQPFh0f5rOK0QHDvBeLWJ7F+++s7Edsh6VE84nZtAv4P3DoTR0iSwXgFCOROhTw08lgy07Iow4bxnOLHfnvb4GsPzM/vbBkD9QcXX2e67CbQ/IsYWZp6OwVbpbDpwp9AXyO9FwC/wZQsgmA1DOVKu6Q8AQJBANXcU2xEm5m1zZONP6sFQffZaMlLO1veTLKPge7HvPe7L/cKIU/XNeYYu/kgil1PfYjnJ4IX+HCDJdjpdP4F+aECQQDHoupBcTMz1uBYB2BVcQia63KQn6nLEUpKOGI5Hd6wSk00EQMm0f7FqEgcafY3rFPCO9lqTcJi7RVsB3eXnQ5bAj8P30pRvsXNorCfQtx21O0QeBJO3kaJivYmSoBaOHjN6halPxs50b8uVKee1ctvIXcvsfg8r4rAzvsAHlOQhuECQBCv13Dc63C710jEZRL/Pb7lS1A3aFPnABSwHdW0X3bQ3x8pBOBr0SXoaQ6m9MO2jdHAeu3dzg/CzXWYMjDkGd0CQDoWQf3JTFGu8/OiPWubITsYn7+cnlulYi4o6MMq2nDBzA1mjjqW4KEZEUahpc3PsyOqNqs+l9B/sgxSCEki0Nw="; + System.out.println(q.length()); + System.out.println(w.length); + + String sk = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAI0cjCg1heBvtTLVfw1Y4qaPsW4YEkCJES8QJD7WvRymWW5a/tOYqhMBm8q04yuj+DePgYCBjuPwRqRtwHE4EEiHGSecUHAMUI5WBaasppIGpIpob1lw1ddD1rosMbVLaDdqjW69K5Al8Z2latAzfrLLwD7UxsKDF7Ya4UeqWHqxAgMBAAECgYAMGle3FygdYNdkvcUA6w+9g3OPYscl+9uacsL5FMfxjh77hRh4I47qxGeNUkhttMmUUl2kHPiZekFE1xj7XPigbmf3xX1FJQayarS2mC+S9QkgJuyEOq5JdXybSCCxJQ5qveUQuJeQe55Eg7Bv0iDcYzDBDovPOWTENnu/J9y4MQJBAO7uiPUQJ6I+FXsBgHK+zsmuSkYeKifYNrPzCRdiQq8/EUK9aseqS/ec34ENadt1TjoaHiincnmjjf9l/gq4GtUCQQCXMR/W40ERf00GOyb1oMD6jkQjvntsw7CDObA6Yopw0sMhN7ChSDJpI5hjcd8CJ/l+bFJYvP6kNgH531FK69ZtAkEA2oA/tUTRyfhWsoecDNNbzmpaOCdLy+ZZmFTwgnb0nsjhIxSP+wpMsKPAbYdzwCNVp6LM48bF1GFy8RY3rVvSBQJAdDO7ZutvHUWcK4fXH74X0/r4AAjsz+FvjswN2DHYeXJjquokhTD6HbjP7M6eOggDR9l1SOKpTAh+aE/tKQot2QJACtyDt/DPfdGUd2+giHym4kZtiL83xHyTOWfvNFwEiUztfqMGc96xSwsWwNoVLSgbEd1Q7/in17BqVM6tBA8OlA=="; byte[] e = Base64.getDecoder().decode(sk); + System.out.println(sk.length()); + System.out.println(e.length); + EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(e); KeyFactory generator = KeyFactory.getInstance("RSA"); PrivateKey privateKey = generator.generatePrivate(privateKeySpec); @@ -31,6 +37,8 @@ public static void main(String[] argv) String y = Base64.getEncoder().encodeToString(t); System.out.println(y); + System.out.println(y.length()); + System.out.println(t.length); } catch (Exception e) { diff --git a/Cryptonite/src/Server/Server_Delete_Group.java b/Cryptonite/src/Server/Server_Delete_Group.java index 46fdf343..9d8b9533 100644 --- a/Cryptonite/src/Server/Server_Delete_Group.java +++ b/Cryptonite/src/Server/Server_Delete_Group.java @@ -74,6 +74,7 @@ public void running(int count) throws IOException db.Update("update test set mygrouplist = '" + save + "' where uscode = " + uscode + ";"); } db.Update("delete from grouplist where gpcode = "+Server_Code_Manager.codeCutter(gpCode)+";"); + db.Update("delete from groupkey where gpcode = "+Server_Code_Manager.codeCutter(gpCode)+";"); File forDelete = new File("Server_Folder/Backup/" + gpCode); String[] arrayTemp = forDelete.list(); diff --git a/Cryptonite/src/Server/Server_Get_GroupKey.java b/Cryptonite/src/Server/Server_Get_GroupKey.java index 6fa2e4eb..086a1b50 100644 --- a/Cryptonite/src/Server/Server_Get_GroupKey.java +++ b/Cryptonite/src/Server/Server_Get_GroupKey.java @@ -13,38 +13,63 @@ import Crypto.Crypto_Factory; import Function.Function; -public class Server_Get_GroupKey extends Server_Funtion -{ +public class Server_Get_GroupKey extends Server_Funtion { int gpcode; - + int keynum = 0; + public Server_Get_GroupKey(Server_Client_Activity activity) { super(activity); } @Override - public void Checker(byte[] packet) - { + public void Checker(byte[] packet) { _packetMaxCount = 1; - gpcode = Server_Code_Manager.codeCutter(new String(Function.cuttingByte(1, packet)).trim()); + keynum = packet[1]; + gpcode = Server_Code_Manager.codeCutter(new String(Function.cuttingByte(2, packet)).trim()); } @Override - public void running(int count) throws IOException - { + public void running(int count) throws IOException { Checker(_activity.getReceiveEvent()); Server_DataBase db = Server_DataBase.getInstance(); - - - try { - - ResultSet rs = db.Query("Select *from grouplist where gpcode = "+ gpcode+";"); - rs.next(); - String groupKey = rs.getString(4); - - byte[] groupkey = Base64.getDecoder().decode(groupKey); - - _activity.send.setPacket(groupkey, 32).write(); - + + try { + if (keynum == 0) { + ResultSet rs = db.Query("Select *from grouplist where gpcode = " + gpcode + ";"); + rs.next(); + String groupKey = rs.getString(4); + + byte[] groupkey = Base64.getDecoder().decode(groupKey); + + _activity.send.setPacket(groupkey, 32).write(); + + } else { + System.out.println("Select groupkey from groupkey where gpcode = '" + gpcode + "' and uscode = '" + + Server_Code_Manager.codeCutter(_activity.getClientCode()) + "' and groupkeynum = '" + keynum + + "';"); + ResultSet rs = db.Query("Select groupkey from groupkey where gpcode = '" + gpcode + "' and uscode = '" + + Server_Code_Manager.codeCutter(_activity.getClientCode()) + "' and groupkeynum = '" + keynum + + "';"); + rs.next(); + + String groupKey = rs.getString(1); + + byte[] groupkey = Base64.getDecoder().decode(groupKey); + + ResultSet rs1 = db.Query("Select secretkey from test where uscode = " + Server_Code_Manager.codeCutter(_activity.getClientCode()) +";"); + rs1.next(); + String Sk = rs1.getString(1); + + byte[] sk = Base64.getDecoder().decode(Sk); + int len = sk.length; + + _activity.send.setPacket(groupkey,128).write(); + _activity.send.setPacket(Function.intToByteArray(len),4).write(); + _activity.send.setPacket(sk,len).write(); + + + } + } catch (SQLException e) { // TODO �ڵ� ������ catch ��� e.printStackTrace(); diff --git a/Cryptonite/src/Server/Server_Group_Invite.java b/Cryptonite/src/Server/Server_Group_Invite.java index 992f3f05..be63fb46 100644 --- a/Cryptonite/src/Server/Server_Group_Invite.java +++ b/Cryptonite/src/Server/Server_Group_Invite.java @@ -3,126 +3,163 @@ import java.io.IOException; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; import java.security.PublicKey; +import java.security.spec.EncodedKeySpec; import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Base64; import java.util.StringTokenizer; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; +import javax.crypto.spec.SecretKeySpec; import Crypto.Crypto_Factory; -public class Server_Group_Invite extends Server_Funtion -{ +public class Server_Group_Invite extends Server_Funtion { private String _id; private String _gpCode; private boolean _passCheck; - - public Server_Group_Invite(Server_Client_Activity activity) - { + + public Server_Group_Invite(Server_Client_Activity activity) { super(activity); } - private void setInformation(byte[] packet) - { + private void setInformation(byte[] packet) { int end = 0; byte[] idTemp = new byte[packet[1]]; - for(int i = 0; i < idTemp.length; i++) - { + for (int i = 0; i < idTemp.length; i++) { idTemp[i] = packet[i + 3]; end = i + 3; } _id = new String(idTemp).trim(); - + byte[] gpCodeTemp = new byte[packet[2]]; - for(int i = 0; i < gpCodeTemp.length; i++) - { + for (int i = 0; i < gpCodeTemp.length; i++) { gpCodeTemp[i] = packet[i + end + 1]; } _gpCode = new String(gpCodeTemp).trim(); } - + @Override - public void Checker(byte[] packet) - { + public void Checker(byte[] packet) { setInformation(packet); _packetMaxCount = 1; _cutSize = 1; } @Override - public void running(int count) throws IOException - { - if(count == 1) - { - Checker(_activity.getReceiveEvent()); - try - { + public void running(int count) throws IOException { + if (count == 1) { + Checker(_activity.getReceiveEvent()); + try { _passCheck = true; Server_DataBase db = Server_DataBase.getInstance(); ResultSet rs = db.Query("select * from test where id = '" + _id + "';"); rs.next(); String uscode = "@" + rs.getString(6); - - ResultSet rs2 = db.Query("select * from grouplist where gpcode = " + Server_Code_Manager.codeCutter(_gpCode) + ";"); + + ResultSet rs2 = db.Query( + "select * from grouplist where gpcode = " + Server_Code_Manager.codeCutter(_gpCode) + ";"); rs2.next(); String gplist = rs2.getString(2); StringTokenizer st = new StringTokenizer(gplist, ":"); - while(st.hasMoreTokens()) - { - if(st.nextToken().equals(uscode)) - { + while (st.hasMoreTokens()) { + if (st.nextToken().equals(uscode)) { _passCheck = false; } } - if(_passCheck) - { + if (_passCheck) { gplist += ":" + uscode; - db.Update("update grouplist set gplist = '" + gplist + "' where gpcode = " + Server_Code_Manager.codeCutter(_gpCode) + ";"); - + db.Update("update grouplist set gplist = '" + gplist + "' where gpcode = " + + Server_Code_Manager.codeCutter(_gpCode) + ";"); + ResultSet rs3 = db.Query("select * from test where id = '" + _id + "';"); rs3.next(); String mygrouplist = rs3.getString(10); - if(mygrouplist.equals("NULL")) - { + String pk = rs3.getString(14); + byte[] Pk = Base64.getDecoder().decode(pk); + + if (mygrouplist.equals("NULL")) { mygrouplist = _gpCode; - } - else - { + } else { mygrouplist += ":" + _gpCode; } - db.Update("update test set mygrouplist = '" + mygrouplist + "' where uscode = " + Server_Code_Manager.codeCutter(uscode) + ";"); - + + db.Update("update test set mygrouplist = '" + mygrouplist + "' where uscode = " + + Server_Code_Manager.codeCutter(uscode) + ";"); + ResultSet rs4 = db.Query("select * from test where id = '" + _id + "';"); rs4.next(); String pubkey = rs4.getString(14); byte[] pubKey = Base64.getDecoder().decode(pubkey); - PublicKey pk = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubKey)); - - ResultSet rs5 = db.Query("select * from grouplist where gpcode = " + Server_Code_Manager.codeCutter(_gpCode) + ";"); + ResultSet rs5 = db.Query("Select secretkey from test where uscode = '" + Server_Code_Manager.codeCutter(_activity.getClientCode()) + "';"); rs5.next(); + String sk = rs5.getString(1); + byte[] Sk = Base64.getDecoder().decode(sk); + + EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Sk); + KeyFactory generator = KeyFactory.getInstance("RSA"); + + PublicKey PK = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubKey)); //초대받는사람 공개키 + PrivateKey privateKey = generator.generatePrivate(privateKeySpec); //초대하는사람 비밀키 + + ResultSet rs6 = db.Query("Select groupkeynum, groupkey from groupkey where uscode = '" + + Server_Code_Manager.codeCutter(_activity.getClientCode()) + "' and gpcode = '" + + Server_Code_Manager.codeCutter(_gpCode) + "';"); - byte[] groupkey = Crypto_Factory.create("RSA1024", Cipher.ENCRYPT_MODE, pk).doFinal(Base64.getDecoder().decode(rs5.getString(4))); + ArrayList gknumList = new ArrayList<>(); + ArrayList GroupkeyList = new ArrayList<>(); - //System.out.println(); - db.Update("INSERT INTO groupkey values('"+ Server_Code_Manager.codeCutter(_gpCode) +"'," + rs5.getInt(11) + ",'" + Server_Code_Manager.codeCutter(uscode) + "','" + Base64.getEncoder().encodeToString(groupkey) + "')"); + while(rs6.next()) + { + int gknum = rs6.getInt(1); + String gk = rs6.getString(2); + byte[] Gk = Base64.getDecoder().decode(gk); + + byte[] groupkey = Crypto_Factory.create("RSA1024", Cipher.DECRYPT_MODE, privateKey).doFinal(Gk); + groupkey = Crypto_Factory.create("RSA1024", Cipher.ENCRYPT_MODE, PK).doFinal(groupkey); + String Groupkey = Base64.getEncoder().encodeToString(groupkey); + + gknumList.add(gknum); + GroupkeyList.add(Groupkey); + + } + for (int i=0; i