nicholas
nicholas

Reputation: 127

Errors returned when trying to decrypt data using Secretkey

I'm currently using DES as a practice method for encrypting/decrypting data (I know it isn't industry practice!) and I'm running into an error when decrypting(here is the output):

java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:469)
    at com.sun.crypto.provider.DESCipher.engineInit(DESCipher.java:186)
    at javax.crypto.Cipher.implInit(Cipher.java:802)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
    at javax.crypto.Cipher.init(Cipher.java:1249)
    at javax.crypto.Cipher.init(Cipher.java:1186)
    at CryptoPrac.Encrypt_Decrypt.Decrypt(Encrypt_Decrypt.java:68)
    at CryptoPrac.Crypto_Main.main(Crypto_Main.java:35)

This is my code:

public byte[] Decrypt(byte[] encrypted)
{
    try
    {
        KeyStore keyStore = KeyStore.getInstance("JCEKS");
        keyStore.load(new FileInputStream("output.jceks"), "password".toCharArray());
        SecretKey key = (SecretKey) keyStore.getKey("key", "password".toCharArray());
        System.out.println(key.toString());
        Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        deCipher.init(Cipher.DECRYPT_MODE, key);

        return deCipher.doFinal(encrypted);
    }
    catch (Exception e)
    {
        System.out.println(e.getMessage());
    }
    return null;
}

I think it might be a problem with me trying to cast getKey as a secret key but I'm not sure how to do it otherwise (the key is stored as a secretkey in the keystore but it returns an error when i don't have it there).

edit: I have the policy .jars in my referenced libraries because I think it may be a problem with them not being recognized.

Upvotes: 1

Views: 376

Answers (2)

Bryan
Bryan

Reputation: 186

When you are initializing deCipher with the init() method, the IV is a necessary third parameter. Depending on how the original string is being encrypted will determine how you go about obtaining this value.

Upvotes: 2

nityanarayan
nityanarayan

Reputation: 378

Mate you are missing this " just after "key

See in your code SecretKey key = (SecretKey) keyStore.getKey("key, "password".toCharArray());

Upvotes: 1

Related Questions