AES KeyPairGenerator Not Recognised

孤街醉人 提交于 2019-12-01 01:03:59

AES is a symmetric algorithm, hence they use of KeyPairGenerator is not supported. To generate a key with AES you call KeyGenerator

KeyGenerator kgen = KeyGenerator.getInstance("AES");
       kgen.init(128);  //set keysize, can be 128, 192, and 256

By looking at the rest of your code, it looks like you are trying to achive asymmetric encryption (since you call getPublic() and getPrivate() etc), so I advice you to switch to using RSA or any other asymmetric algorithm that java supports. You will most likley only need to replace AES with RSA in your getInstance(); calls, and pherhaps some fine-tuning. Good luck

Yuriy Nakonechnyy

As far as I know, AES is symmetric encryption algorithm i.e. it needs only one key for encryption/decryption.

From the JavaDoc of java.security.KeyPairGenerator:

The KeyPairGenerator class is used to generate pairs of public and private keys.

Meaning that it should be used for asymmetric encryption algorithms. For symmetric encryption algorithms one should use javax.crypto.KeyGenerator.

However, I advise simply mimicking some tutorial on how to encrypt / decrypt byte array in Java using AES like this one.

It uses sun.misc.Base64Encoder / Base64Decoder classes to encode / decode byte array to / from String, however you may skip this step.

Hope this helps

How can you use a keypair generator for AES? AES is a symmetric key algorithm. Refer this link. That means if you encrypt data using a key "k", then you will have to decrypt it also using the same key "k".

But when you generate key pair, as the name suggests, two keys are generated and if you encrypt using one of the keys, you can decrypt only using the other key. This is the base for PKI.

If you want to use keypair generator use an algorithm like "rsa" or "dsa" in the getInstance() method like this :

KeyPairGenerator keygen=KeyPairGenerator.getInstance("rsa");

I think your code should now work fine after making the above change.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!