0

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

  • 1
    Did you create a new account because your old one is question banned? That is discouraged! Context: https://stackoverflow.com/questions/66261958 That being said: how did you prv file get created, what is its content? Don't share a confidential key but generate a dummy one that can be used to properly test / experiment with the code. – luk2302 Feb 18 '21 at 16:32
  • the .prv is created by some code my lecturer has given me to use, the question then says to use the key inside this file to encrypt a message. This is what I am trying to do. –  Feb 18 '21 at 16:40
  • The code itself works. Either the key is corrupt or the format/encoding of the key does not match the code, which expects a DER encoded PKCS#8 key. Check if your key meets this requirement (or share a **test** key). – Topaco Feb 18 '21 at 17:20
  • I have just edited the post, with my updated key generation and an example key that is generated from the other class –  Feb 18 '21 at 17:25
  • Is this the key file content? Anyway, obviously modulus and private exponent are given. Then you could use [`RSAPrivateKeySpec`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/security/spec/RSAPrivateKeySpec.html#%3Cinit%3E(java.math.BigInteger,java.math.BigInteger)) instead of `PKCS8EncodedKeySpec`, and pass modulus and private exponent as `BigInteger`. – Topaco Feb 18 '21 at 17:57
  • Cheers mate I got that working –  Feb 18 '21 at 18:28

1 Answers1

0

I would just comment this but as I am not allowed too I'll instead propose an answer and say: Did you have a look at this thread?

Edit: Typo

vinerich
  • 11
  • 1
  • 2