我正在为我的程序使用免费的IAIK-JCE。在这里:
http://javadoc.iaik.tugraz.at/iaik_jce/current/iaik/security/cipher/MARS.html
它说MARS可以使用128 do 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);
有人可以说我做错了吗?
答案 0 :(得分:0)
我找到了解决方案。也许有人将来会有同样的问题,所以我发布解决方案:它应该适用于任何算法。您必须从以下网址下载JCE:
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
并覆盖
中的文件\Java\jdk1.8.0_45\jre\lib\security