How to Encrypt Images with Java RC5 Algorithm

Asked

Viewed 386 times

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?

  • 1

    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.

  • The installed JCE was: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

1 answer

2


According to that answer in Soen the JCE provides support for RC5, but no concrete implementation of it. I cannot confirm the veracity of this information, but by your exception this seems to be the case. An external provider may be required, and the suggestion of that same response is Bouncy Castle.

After install it you need configure it that way before using it:

import org.bouncycastle.jce.provider.BouncyCastleProvider;
...
Security.addProvider(new BouncyCastleProvider());

Alternatively, add an entry in $JAVA_HOME/jre/lib/security/java.security for a global static installation:

security.provider.N=org.bouncycastle.jce.provider.BouncyCastleProvider

Exchanging N by the next number on the list (there must be several entries of the type security.provider.x, where x is a number).

After that, your code as it is should work normally, or at least you will no longer have that specific error. I’m not sure the way you’re creating the Cipher is correct or not, I think it is also necessary to specify the mode of operation and, where applicable, the padding (ex.: Cipher.getInstance("RC5/CBC/PKCS5Padding")).

  • 1

    Thanks @mgibsonbr was just what I needed. Now it’s working. : import org.bouncycastle.jce.provider.BouncyCastleProvider;&#xA;...&#xA;Security.addProvider(new BouncyCastleProvider()); it is not necessary to configure the java.security file (I couldn’t configure it directly in this file), I had to install the package referring to my java version (https://www.bouncycastle.org/latest_releases.html) - I downloaded and saved in $JAVA_HOME/jre/lib/ext

Browser other questions tagged

You are not signed in. Login or sign up in order to post.