ashmalvi
ashmalvi

Reputation: 33

How to encrypt a file with newline feed

I am encrypting a file, but the encrypted file comes with a continuous string. I want output the same way as my input file. see for example

input file:
===========
Language,English

System Name,TSLGN0

Number of board SPC,12
.
.
Output Encrypted file:
========================
ADCDE12345456

ABCDDDDDDDEDEDAAAADDDD12333

ABCDE123456789

.
.

What I am getting:

760bad166e25ea1e2f6a741363816a15703f2e20524503eee544f69909dd69af760bad166e25ea1e2f

Code below:

BufferedWriter bwr = new BufferedWriter(new FileWriter(new File("C:\\Crypto_Out.txt")));

mbr = new BufferedReader(new FileReader("C:\\Crypto_In.txt"));
while ((line = mbr.readLine()) != null) 
{
    enSecretText=encrypt(line);
    bwr.write(enSecretText.toString());
}

bwr.flush();
bwr.close();

Please suggest

Upvotes: 1

Views: 507

Answers (3)

rossum
rossum

Reputation: 15693

Encryption treats files as a stream of bytes. It is not interested in the meaning assigned to those bytes, just how to encrypt them. Your encrypted ciphertext will be a continuous stream of bytes. It is up to you how to handle that ciphertext.

If you want the ciphertext as letters, then encode it as Base64. If you want to add newlines to your Base64 then you can do so, but your must remove the newlines before removing the Base64 to get back to the original ciphertext bytes.

Decrypting the ciphertext bytes will get back to your original text.

Upvotes: 1

IQV
IQV

Reputation: 500

You have to add bwr.newLine(); after bwr.write.

Upvotes: 0

Imesha Sudasingha
Imesha Sudasingha

Reputation: 3570

Just add a newline at the end of every iteration.

while ((line = mbr.readLine()) != null) 
{
    enSecretText=encrypt(line);
    bwr.write(enSecretText.toString());
    bwr.newLine();
}

Upvotes: 0

Related Questions