I have a problem decrypting a file using RSA public key decryption. My process is to get the xml file, encrypt the contents and write it to the same file. Another function decrypts the contents. My source code:
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];
I just pass the name of the encrypted file and the corresponding value of the private key to the function. However, cis.read(block)
returns -1 on the first try. Can anyone suggest how I can decrypt an encrypted file?
source share