0

I am getting the above error in this particular line of code:

KeyFactory keyFactory=keyFactory.getInstance(keyAlgorithm);

        EncodedKeySpec privateKeySpec=new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey1=keyFactory.generatePrivate(privateKeySpec); 

To be precise this is the error I get:

GeneratePublicPrivateKeys.java:35: error: non-static method generatePublic(KeySp
ec) cannot be referenced from a static context
PublicKey publicKey1=KeyFactory.generatePublic(publicKey
Spec);

Note: I have gone through these posts
“Non-static method cannot be referenced from a static context” error
non static method cannot be referenced from a static context
and some more and I have tried the solutions provided there,but no they do not solve my problem

What should I do?
Thankyou in advance:)

Community
  • 1
  • 1

2 Answers2

0

Try to change this line:

PublicKey publicKey1=KeyFactory.generatePublic(publicKey
Spec);

to

PublicKey publicKey1=keyFactory.generatePublic(publicKey
Spec);
Iswanto San
  • 18,263
  • 13
  • 58
  • 79
0

You need to change

KeyFactory.generatePublic(...)

to be:

keyFactory.generatePublic(...)

At the moment your code is trying to call the generatePublic method as if it were static (ie associated with the KeyFactory class, rather than with an object of that type). You need to call this method on a specific instance of the class - presumably the keyFactory object refered to in your first code snippet.

codebox
  • 19,927
  • 9
  • 63
  • 81