0

I'm trying to make a program which uses RSA encryption to encrypt and decrypt data sent to and from multiple computers. I have a folder which stores the public keys of other computers, on my computer. I can't however, seem to load this key file and create a publicKey object which points to it. My current attempt is shown below but I was wondering what methods other people use to do this?

      //Returns the key of a friend (given as input)

  public PublicKey getFriendsKey(String friend){
    try{ObjectInputStream friendsKeyInput = new ObjectInputStream(new fileInputStream(programLocation + "/Text Files/encryptionKeys/" + friend + "PublicKey.key"));
    PublicKey friendsKey = friendsKeyInput.readObject();
    }
    catch(Exception e){e.printStackTrace();}
    return friendsKey;
  }
slasham
  • 13
  • 2
  • Do you understand that the data length is limited to the key length and 100 to 1000 times slower than a symmetric cipher? That data is generally encrypted with a symmetric algorithm such as AES? – zaph Jul 05 '16 at 18:22
  • @zaph: actually key length minus at least 11 octets for padding/randomization, although now that keys are mostly (required to be) at least 2048-bit = 256-octet that's relatively less important. OP: tell us how the files were generated, and/or post a sample file (or several if they aren't all similar); if not text, use hex dump(s). – dave_thompson_085 Jul 05 '16 at 21:28
  • @dave_thompson_085 I know but in order to avoid arguments (I failed here) I carefully stated "limited to the key length" which is true. If I take into account padding I have gotten slammed because of raw data capabilities. In the context of my comment I want to indicate that there is a limit on data size for asymmetric encryption and this seemed relevant since the OP states files which may well be greater than key size - padding - whatever, I did not want to get into the padding issue. – zaph Jul 05 '16 at 21:40

1 Answers1

0

Why are you using ObjectInputStream? Most probably your key is not a serializable java object. See this answer. Load RSA public key from file

Community
  • 1
  • 1
kamalkishor1991
  • 876
  • 9
  • 14
  • #11410070 covers SPKI (what Java calls X509EncodedKey) and maybe _some_ other PEM using BC, but not a full X509 cert (or even PKCS7 containing cert), or OpenSSH format publickey which is very different, or (Open)PGP format publickey which is very different, or various of ad-hoc two-number formats. – dave_thompson_085 Jul 05 '16 at 21:31