Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. else if(msg.type.equals("pubKey")){
  2. pubKey = msg.content; //get public key
  3. String key = Arrays.toString(crypt.geteKey());
  4. clients[findClient(ID)].send(new Message("symmKey", "SERVER", key, msg.sender));//! //send symmetric key encrypted with public key
  5. }
  6.  
  7. public void keyEncryption() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
  8. Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  9. cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  10. eKey = cipher.doFinal(key.getBlowfishKeyBytes()); //symmetric key encrypted with public key
  11. //System.out.println("2. cipherText= " + bytesToHex(symmKey));
  12. }
  13.  
  14. else if(msg.type.equals("symmKey")){
  15. symmKey = (String) msg.content; //get encrypted symmetric key (must unlock with private key)
  16. }
  17.  
  18. package com.socket;
  19.  
  20. import java.io.Serializable;
  21.  
  22. public class Message implements Serializable{
  23.  
  24. private static final long serialVersionUID = 1L;
  25. public String type, sender,content, recipient;
  26.  
  27. public Message(String type, String sender, String content, String recipient){
  28. this.type = type; this.sender = sender; this.content = content; this.recipient = recipient;
  29. }
  30.  
  31. @Override
  32. public String toString(){
  33. return "{type='"+type+"', sender='"+sender+"', content='"+content+"', recipient='"+recipient+"'}";
  34. }
  35. }
  36.  
  37. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  38. //KeyPair
  39. try {
  40. keyPair = new Keypair();
  41. } catch (NoSuchAlgorithmException ex) {
  42. jTextArea1.append("Security Error! You are not safe!");
  43. }
  44. Object pubKey = keyPair.getKeyPair().getPublic();
  45. username = jTextField3.getText();
  46. password = jPasswordField1.getText();
  47.  
  48. if (!username.isEmpty() && !password.isEmpty()) {
  49. client.send(new Message("login", username, password, "SERVER"));
  50. client.send(new Message("pubKey",username, pubKey, "SERVER")); //send Public key to Server
  51. }
  52. }
  53.  
  54. Database exception : userExists()
  55. 53846 ERROR reading: cannot assign instance of sun.security.rsa.RSAPublicKeyImpl to field com.socket.Message.content of type java.lang.String in instance of com.socket.Message
  56.  
  57. pubKey = msg.content;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement