【问题标题】:Mars algorithm, IAIK JCE, invalid key size [duplicate]Mars 算法,IAIK JCE,无效的密钥大小 [重复]
【发布时间】:2016-05-14 18:50:34
【问题描述】:

我正在为我的程序使用免费的 IAIK-JCE。这里:

http://javadoc.iaik.tugraz.at/iaik_jce/current/iaik/security/cipher/MARS.html

它说 MARS 可以使用 128 到 448 的密钥(增量为 32 位)。但是当我尝试使用与 128b 不同的密钥时:

Caused by: java.security.InvalidKeyException: Illegal key size or default parameters

我的代码:

 public void marsEncryption(Integer dlugoscKlucza, File file, List<User> listaOdbiorcow, ProgressBar pb,String mode) throws NoSuchAlgorithmException, NoSuchProviderException,
        InvalidKeyException, NoSuchPaddingException, IOException,
        IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {

    //Generowanie klucza
    SecretKey secretKey = generateMarsSymetricKey(dlugoscKlucza);

    //inicjalizacja
    Cipher cipher = Cipher.getInstance("MARS/"+mode+"/PKCS5Padding", "IAIK");

    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    //Pobranie wektora poczatkowego
    byte[] ivBytes = cipher.getIV();
    IvParameterSpec iv = new IvParameterSpec(ivBytes);

    //Zamiana klucza na tablice bajtow
    byte[] KeyBytes = secretKey.getEncoded();

    //Zaszyfrowanie klucza symetrycznego kluczem publicznym odbiorcow
    for (User odbiorca : listaOdbiorcow) {
        byte[] encryptedKey = rsaEncryption(KeyBytes, odbiorca.publicKey);
        odbiorca.symetricKey = Base64.encodeBase64String(encryptedKey);
    }

    //Zapisanie do pliku xmla
    XMLFactory.createXML(dlugoscKlucza, ivBytes, listaOdbiorcow, file.toString());
    //Szyforwanie pliku
    String newFile = file.toString() + "Crypt.xml";
    encryptTask = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            FileOutputStream fos = new FileOutputStream(newFile, true);
            try (FileInputStream fis = new FileInputStream(file.toString())) {
                byte[] block = new byte[(int) (128)];
                double postep = 0;
                double postepMax = file.length();
                updateProgress(0, postepMax);
                int i;
                while ((i = fis.read(block)) != -1) {
                    //Szyfrowanie
                    byte[] block2 = cipher.update(block, 0, i);
                    //Zapisanie do pliku
                    postep += i;
                    updateProgress(postep, postepMax);
                    if (isCancelled()) {
                        fis.close();
                        fos.close();
                        File f = new File(newFile);
                        f.delete();
                        updateProgress(0, postepMax);
                        break;
                    }
                    fos.write(block2);
                }
                byte[] outputFinalUpdate = cipher.doFinal();
                fos.write(outputFinalUpdate);
                return null;
            }
        }
    };
    pb.progressProperty().bind(encryptTask.progressProperty());
    new Thread(encryptTask).start();
}
 private SecretKey generateMarsSymetricKey(int dlugoscKlucza) throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyGenerator keyGen = KeyGenerator.getInstance("MARS", "IAIK");
    System.out.println("Wybrana długość klucza: " + dlugoscKlucza);
    keyGen.init(dlugoscKlucza);
    SecretKey secretKey = keyGen.generateKey();
    return secretKey;
}

失败了:

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

谁能说我做错了什么?

【问题讨论】:

    标签: java encryption


    【解决方案1】:

    我找到了解决方案。也许将来有人会遇到同样的问题,所以我发布解决方案:它应该适用于任何算法。您必须从以下位置下载 JCE:

    http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
    

    并覆盖

    中的文件
    \Java\jdk1.8.0_45\jre\lib\security
    

    【讨论】:

    • 用什么覆盖它们?
    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多