I'm trying to use the RC5 algorithm with the Java cipher class, but it's returning an error, can anyone 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 that 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