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

peti446
  • 330
  • 1
  • 4
  • 12

1 Answers1

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");
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
wdf
  • 171
  • 5
  • now i have this error: `java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format` the key is correct wo what is wrong ? – peti446 Feb 03 '15 at 22:03
  • 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](http://stackoverflow.com/questions/8451131/read-private-key-in-der-format-java), which links to another answer on this topic... – wdf Feb 04 '15 at 00:42
  • Now its work, thx for all it was the kay it was in incorrect format, again thanks for all! – peti446 Feb 04 '15 at 12:40
  • Yes forgot that sorry, Thx For all! – peti446 Feb 05 '15 at 17:02