4
I am trying to use the RC5 algorithm with the Cipher class of Java, but it is returning an error, someone can help me?
import java.io.*;
import java.security.*;
import javax.crypto.*;
public class EncriptaDecriptaRC5 {
KeyGenerator keyGenerator = null;
SecretKey secretKey = null;
Cipher cipher = null;
public EncriptaDecriptaRC5() throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException {
keyGenerator = KeyGenerator.getInstance("RC5"); //Cria a chave
keyGenerator.init(128); // 128 - 192 - 256
secretKey = keyGenerator.generateKey();
System.out.println(secretKey.getEncoded().length);
cipher = Cipher.getInstance("RC5"); //Cria uma instância da cifra mencionando o nome do algoritmo de criptografia
}
void encrypt(String srcPath, String destPath) throws InvalidKeyException, FileNotFoundException, IOException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
File rawFile = new File(srcPath);
File imagemEncriptada = new File(destPath);
InputStream inStream = null;
OutputStream outStream = null;
cipher.init(Cipher.ENCRYPT_MODE, secretKey); //Inicializa o cipher para encriptar
inStream = new FileInputStream(rawFile); //Inicializa o input e o output streams
outStream = new FileOutputStream(imagemEncriptada);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len)); //Para criptografar/descriptografar vários blocos usa-se o método update().
outStream.flush();
}
outStream.write(cipher.doFinal()); //Depois de tudo feito chamamos o método doFinal().
inStream.close();
outStream.close();
}
void decrypt(String srcPath, String destPath) throws InvalidKeyException, FileNotFoundException, IOException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
File encryptedFile = new File(srcPath);
File decryptedFile = new File(destPath);
InputStream inStream = null;
OutputStream outStream = null;
cipher.init(Cipher.DECRYPT_MODE, secretKey); //Inicializa o cipher para decriptografar
inStream = new FileInputStream(encryptedFile); //Inicializa o input e o output streams
outStream = new FileOutputStream(decryptedFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
outStream.write(cipher.doFinal());
inStream.close();
outStream.close();
}
}
The error the program returns is this:
Exception in thread "main" java.security.NoSuchAlgorithmException: RC5 KeyGenerator not available
at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:169)
at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
at Simetrico.EncriptaDecriptaRC5.<init>(EncriptaDecriptaRC5.java:23)
at Simetrico.TesteRC5.main(TesteRC5.java:27)
Java Result: 1
Apparently your JVM doesn’t support this algorithm. Does it have to be RC5? I found little information about him, probably because he is relatively old and apparently little used. P.S. You have Java Cryptographic Extensions (JCE) installed?
– mgibsonbr
Yes, it needs to be the RC5, the other algorithms implemented by Cipher I’ve already achieved, just this one. I also found little information about his implementation. I need to do a comparison of the algorithms, so even if it’s old, I need his results. I already installed the JCE version for Java security policy.
– Dainara Voitechen
The installed JCE was: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
– Dainara Voitechen