0

I have code:

public PublicKey read_public_key(String path)
                throws IOException, NoSuchAlgorithmException,
                InvalidKeySpecException {

         KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
            FileInputStream pubKeyStream = new FileInputStream(path);  
            byte[] pubKeyBytes = new byte[(int)path.length()];
            pubKeyStream.read(pubKeyBytes);   
            pubKeyStream.close();  
            X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes);   
            PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);  

            return pubKey;
        }

But I get invalid key format How do I get public key from .pub file? Later I need to:

private static byte[] encrypt(String text, PublicKey key) {
        byte[] cipherText = null;
        try {
          // get an RSA cipher object and print the provider
          final Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
          // encrypt the plain text using the public key
          cipher.init(Cipher.ENCRYPT_MODE, key);
          cipherText = cipher.doFinal(text.getBytes());
        } catch (Exception e) {
          e.printStackTrace();
        }
        return cipherText;
      }
babboon
  • 683
  • 3
  • 20
  • 45
  • 1
    It might be 1024-bit key but the file is likely larger than 128 bytes. Better to use a buffer and read the whole file in. It might be in Base-64 ASCII. Also take a look at http://stackoverflow.com/questions/3243018/how-to-load-rsa-private-key-from-file – Apprentice Queue Nov 15 '13 at 13:38
  • Edited code to byte[] pubKeyBytes = new byte[(int)path.length()]; but get the same erro. – babboon Nov 15 '13 at 13:39
  • 1
    `path.length()` gives you the length of the file name NOT the length of the file content. – Apprentice Queue Nov 15 '13 at 13:40
  • Yes, true. But this is not the case – babboon Nov 15 '13 at 13:42
  • My public key is in .pub file. Not in .pem – babboon Nov 15 '13 at 13:48
  • File f = new File(path); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int)f.length()]; dis.readFully(keyBytes); dis.close(); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(spec); – babboon Nov 15 '13 at 13:51
  • Tried code above but same error – babboon Nov 15 '13 at 13:59
  • Have you looked at that .pub file at all? Is it ASCII or is it binary? And how do you know you are supposed to use X509 encoder instead of PKCS5 (for example) – Apprentice Queue Nov 15 '13 at 14:40
  • -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq9GIGKEI0A5vu8QKFbG8 ........... HwIDAQAB -----END PUBLIC KEY----- – babboon Nov 15 '13 at 14:41
  • Maybe try removing --BEGIN PUBLIC and --END PUBLIC strings. Have you looked for any tutorials on this such as http://docs.oracle.com/javase/tutorial/security/apisign/vstep2.html ? – Apprentice Queue Nov 15 '13 at 14:46
  • I seems I tried to read public key from certificate. But it is plain public key file, not a certificate. How do I read public key from file into PublicKey pubkey = ... – babboon Nov 15 '13 at 14:47
  • Correct answer: Security.addProvider(new BouncyCastleProvider()); FileReader fileReader = new FileReader(path); PEMReader r = new PEMReader(fileReader); return (PublicKey) r.readObject(); – babboon Nov 17 '13 at 10:43

0 Answers0