我在我的程序中使用免费的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);有人能说我做错了什么吗?
发布于 2016-05-15 03:08:24
我找到了解决方案。也许将来有人会有同样的问题,所以我发布了解决方案:它应该适用于任何算法。您必须从以下位置下载JCE:
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html中的文件,并覆盖
\Java\jdk1.8.0_45\jre\lib\securityhttps://stackoverflow.com/questions/37230605
复制相似问题