I am trying to load a private key from a file and then use this private key to get the modulus and exponent. But when I create the private key I get the following error:
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : DerInputStream.getLength(): lengthTag=109, too big. at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:250) at java.base/java.security.KeyFactory.generatePrivate(KeyFactory.java:390) at Client.main(Client.java:58) Caused by: java.security.InvalidKeyException: IOException : DerInputStream.getLength(): lengthTag=109, too big. at java.base/sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:133) at java.base/sun.security.pkcs.PKCS8Key.(PKCS8Key.java:94) at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.(RSAPrivateCrtKeyImpl.java:130) at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:85) at java.base/sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:355) at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:246) ... 2 more
Here is my code where I get the file name and try to create the private key.
byte[] keyBytes = Files.readAllBytes(Paths.get(output+".prv"));
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory rsaFact = KeyFactory.getInstance("RSA");
RSAPrivateKey key = (RSAPrivateKey) rsaFact.generatePrivate(spec);
The output.prv, the output is the username and .prv is the file type that is created in a different method, which automatically generates the key. I just want to get the private key from the file.
Here is the key that is generated from the other file
SunRsaSign RSA private CRT key, 2048 bits
params: null
modulus: 18084100169983149427066482527604115746190219078189234105203625326412760751210703298115063502298186511066239351114969324553879550391327154377587706062971324999459293277427867723772543991811325841765280665439097010290220945299964041074424240275807775652203588398158621625667076399388107776579829026844381635271039680795179509425842635409982862232922158074095170776431141412596599683031473368650600492082930092439720107233068168026832652353420501942453862260408347057156619733497039885468915234561425093650221644249373913108527900535389930734873554925693784673505692243026885455606070479910344434901259082575943448959779
private exponent: 12841292241184458014364296464395879190082765845883888300560921491909465600791804620052500270174873821894343358451520934079434405550470601084422525856424562483785298857922405362790528705111977519542103946282851787049697302766748959816294320303267870905390823401820464387958996206697352207130254717522269682173363746267525476965078058033366333877304865058976851114190836350093363759569164389007627774273603200327643820232553479155125421781107349834029404115646012972640582500098078887754339093154204134258421766428473216528531352682353113256728328028902309568767490774074390452849891054091134627576057810041899256811593
Thanks everyone