File f = new File(filename); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int) f.length()]; dis.readFully(keyBytes); dis.close(); RSAPrivateKeySpec spec = new RSAPrivateKeySpec(new BigInteger(keyBytes),new BigInteger(keyBytes)); KeyFactory kf = KeyFactory.getInstance("RSA"); kf.generatePrivate(spec);
The file opens, but the question arose how to find out the modulus and the private exponent with a sequence of keyBytes bytes. RSAPrivateKeySpec should receive modulus and private exponent as input parameters, I don’t have these values and I inserted new BigInteger (keyByte) everywhere, but this is not correct and, accordingly, can not be decrypted, encrypted with such a key.