Decrypt contents of encrypted file?

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]; //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 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?

+6
source share
1 answer

Your file is almost certainly not RSA encrypted. It is probably encrypted by AES under a random symmetric key, and then the key is encrypted using RSA.

You assume that someone has really encrypted the entire file using only RSA. Assuming that the implementation even allows you to do this (I saw those that throw exceptions when you try it), it would be a too slow way to do something useful.

+2
source

Source: https://habr.com/ru/post/918238/


All Articles