Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrypt the encrypted file content?

I am having a problem decrypting a file using RSA public key decryption. My process is to receive the xml file, encrypt the content, and write it back to the same file. Another function decrypts the content. My source code is:

public void decryptFile(String fileName,PrivateKey privateKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    FileInputStream fis = new FileInputStream(fileName);
    File file=new File("decryptedfile.xml");
    if(file.exists()) {
        file.delete();
    }
            FileOutputStream fos = new FileOutputStream("decryptedfile.xml");
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int i;
    byte[] block = new byte[32];
    //System.out.println("Read : "+cis.read(block));
    while ((i = cis.read(block)) != -1) {
        System.out.println(String.valueOf(i));
        fos.write(block, 0, i);
    }
    fos.close();
}

I just pass in the name of the encrypted file, and the corresponding private key value, into the function. However the cis.read(block) returns -1 on the first attempt. Can anyone suggest how I can decrypt the encrypted file?

like image 246
Bathakarai Avatar asked Jun 16 '12 12:06

Bathakarai


1 Answers

Your file is almost certainly not RSA encrypted. Its probably encrypted with AES under a random symmetric key and the key is then encrypted with RSA.

You are assuming someone actually encrypted the entire file using just RSA. Assuming the implementation even lets you do this (i've seen ones that throw exceptions when one tries this), it would be way way way too slow to do anything useful.

like image 166
imichaelmiers Avatar answered Oct 04 '22 22:10

imichaelmiers