codeFreak
codeFreak

Reputation: 353

Reading from an encrypted file into a declared string variable

I have an encrypted file that was done using reference from this question .I got the file encrypted.Now my issue is when trying to read the contents out, am getting an empty strings from the returned read(). Below is my call method and the method to decrypt the encrypted text to a string variable.

Calling Method:

File encryptedCFG = new File(homeDir + "/" + folder_name + "/twCGF.txt");
dc.ReadEncryptedFile(encryptedCFG); 

Method:

public void ReadEncryptedFile(File deInFile) {
        try {
            FileInputStream fis = new FileInputStream(deInFile);
            int length = (int) deInFile.length();
            byte[] filebyte = new byte[length]
            // Decrypt the byte contents from the file using the cipher setup
            byte[] tmpTxT = mDecipher.doFinal(filebyte);
            fis.read(tmpTxT);
            fis.close();

         // Read into a string since we got the contents
         String plaintxt = new String(tmpTxt, "UTF-8");

         } catch (Exception e) {
            e.printStackTrace();
        }
    }

Any pointers why am not getting the contents of the encrypted file correctly?

Upvotes: 1

Views: 172

Answers (1)

Artjom B.
Artjom B.

Reputation: 61952

At the line where you're decrypting the byte array, it's still empty. You haven't read the file in, yet. You have to switch the operations.

byte[] filebyte = new byte[length]
fis.read(filebyte);
byte[] tmpTxt = mDecipher.doFinal(filebyte);
fis.close();

String plaintxt = new String(tmpTxt, "UTF-8");

Upvotes: 2

Related Questions