How to encrypt images with algorithm RC5 in Java

4

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
    
asked by anonymous 24.09.2015 / 15:34

1 answer

2

According to this answer in SOen JCE provides support for RC5, but no concrete implementation of it. I can not confirm the veracity of this information, but by its exception this seems to be the case. An external provider may be necessary, and the suggestion for this same response is Bouncy Castle .

After install it you need configure it in this 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 to a static static installation:

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

Changing N to the next number in the list (there should be several entries of type security.provider.x , where x is a number).

After that, your code as it should should work normally, or at least it will no longer have that specific error. I'm not sure if the way you're creating Cipher is correct or not, I think it's also necessary to specify the # / em> (ex. Cipher.getInstance("RC5/CBC/PKCS5Padding") ).

    
25.09.2015 / 14:30