Quantcast
Channel: Decrypt the encrypted file content? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Decrypt the encrypted file content?

$
0
0

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?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images