RC5实现中的java.security.InvalidAlgorithmParameterException

时间:2015-04-19 13:43:15

标签: java cryptography

我想在我的应用中使用RC5加密,其他字数和圆数比默认值。我使用了this implementation as a provider,但是当我打电话给它时,我得到了:

java.security.InvalidAlgorithmParameterException: Illegal parameters
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1037)
    at javax.crypto.Cipher.init(Cipher.java:1367)
    at javax.crypto.Cipher.init(Cipher.java:1301)
    at asymetric.cipher.RC5moje.encrypt(RC5moje.java:87)
    at asymetric.cipher.RC5DES.main(RC5DES.java:25)

这是我的代码:

import de.flexiprovider.api.keys.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import de.flexiprovider.core.FlexiCoreProvider;
import de.flexiprovider.core.rc5.RC5KeyGenerator;
import de.flexiprovider.core.rc5.RC5ParameterSpec;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Security;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;

public class RC5moje {

    private int roundNumer;
    private int wordSize;

    private SecretKey key;
    private RC5ParameterSpec RC5params;
    private Cipher rc5;

   public RC5moje(int roundNumer, int wordSize) {
        this.roundNumer = roundNumer;
        this.wordSize = wordSize;

        Security.addProvider(new FlexiCoreProvider());
        this.RC5params = new RC5ParameterSpec(roundNumer, wordSize);
        try {
            this.rc5 = Cipher.getInstance("RC5", "FlexiCore");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchProviderException ex) {
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        }
        RC5KeyGenerator rC5KeyGenerator = new RC5KeyGenerator();
        this.key = rC5KeyGenerator.generateKey();

    }

    public void encrypt(String inFile, String outFile) {
        try {

            rc5.init(Cipher.ENCRYPT_MODE, key, RC5params);

            FileInputStream fis = new FileInputStream(inFile);
            FileOutputStream fos = new FileOutputStream(outFile);
            CipherOutputStream cos = new CipherOutputStream(fos, rc5);

            byte[] block = new byte[8];
            int i;
            while ((i = fis.read(block)) != -1) {
                cos.write(block, 0, i);
            }
            cos.close();

        } catch (InvalidKeyException ex) {
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAlgorithmParameterException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void decrypt(String inFile, String outFile) {

        try {
            rc5.init(Cipher.DECRYPT_MODE, key,RC5params);

            FileInputStream fis;

            fis = new FileInputStream(inFile);

            FileOutputStream fos = new FileOutputStream(outFile);
            CipherInputStream cis = new CipherInputStream(fis, rc5);

            byte[] block = new byte[8];
            int i;

            while ((i = cis.read(block)) != -1) {
                fos.write(block, 0, i);
            }
            fos.close();
        } catch (InvalidKeyException ex) {
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(RC5moje.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

我像这样初始化这个类: RC5moje rc5 = new RC5moje(12, 32); 所以我认为这不是值的问题,因为那些是默认的,但也许我错误地创建了RC5Params?

编辑: 在de.flexiprovider.core.rc5.RC5ParameterSpec的源代码中,我发现您无法将它们设置为不正确的值:

public RC5ParameterSpec(int numRounds, int wordSize) {
    if ((numRounds < 8) || (numRounds > 127)) {
        this.numRounds = DEFAULT_NUM_ROUNDS;
    } else {
        this.numRounds = numRounds;
    }

    if ((wordSize != 16) && (wordSize != 32) && (wordSize != 64)) {
        this.wordSize = DEFAULT_WORD_SIZE;
    } else {
        this.wordSize = wordSize;
    }
    }

EDIT2: 因此,在与我的代码混合后,我决定尝试使用Bouncy Castle作为提供者,这解决了我的问题。

1 个答案:

答案 0 :(得分:0)

您正在使用特定于FlexiCore的RC5ParameterSpec,请尝试使用Java JCA one instead