AES KeyPairGenerator Not Recognised

前端 未结 3 781
天涯浪人
天涯浪人 2021-01-07 02:33

I have an issue with my java code. I\'m trying to encrypt a file. However, when I run my java code I get \"java.security.InvalidKeyException: Invalid AES key length: 162 byt

3条回答
  •  庸人自扰
    2021-01-07 03:19

    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.

提交回复
热议问题