0

I have generated a public and private code with puttygen, the private key is exported as openssl, the name of the peys are public_key.der , private_key.pem but when i try to use java to encrypt it i get this error:

java.io.FileNotFoundException: public_key.der 

The codode is :

 public static String RSAPublicEncryptuion(String text){ DataInputStream dis = null; try { File pubKeyFile = new File("public_key.der"); dis = new DataInputStream(new FileInputStream(pubKeyFile)); byte[] keyBytes = new byte[(int) pubKeyFile.length()]; dis.readFully(keyBytes); dis.close(); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); String textoEncryptado = new String(cipher.doFinal(text.getBytes()), "UTF-8"); return textoEncryptado; } catch (FileNotFoundException ex) { Logger.getLogger(RSAEncrypt.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException ex) { Logger.getLogger(RSAEncrypt.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalBlockSizeException | BadPaddingException ex) { Logger.getLogger(RSAEncrypt.class.getName()).log(Level.SEVERE, null, ex); } return "Error"; } 

The public_key are in the same package than this class (testras.ras), what i'm doing wrong ? Thanks for all! Sorry for my bad English

2
  • Where's the public_key.der file? Commented Feb 3, 2015 at 21:12
  • In the same package as the .java, when i compile it, it would be in the same place where the .class Commented Feb 3, 2015 at 21:13

1 Answer 1

2

Your current approach (using a relative filepath) depends on the location of the key file relative to the working directory at runtime, which can be non-obvious.

However, you mention that the public key file is "in the same place where the .class" file is -- you can leverage this fact to gain a more flexible solution. Try using Class.getResourceAsStream, as illustrated below:

InputStream is = RSAEncrypt.class.getResourceAsStream("public_key.der"); 
Sign up to request clarification or add additional context in comments.

4 Comments

now i have this error: java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format the key is correct wo what is wrong ?
debugging this would require more info about your execution environment (e.g. I've seen Maven corrupt a PKCS 12 key, presumably trying to minify it or something). if you haven't already, though, maybe check out the answer to this question, which links to another answer on this topic...
Now its work, thx for all it was the kay it was in incorrect format, again thanks for all!
Yes forgot that sorry, Thx For all!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.