Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to encrypt a text file in java

For my School project I had to show that I can utilize file handling within a program. For this I made a very simple login process that you can create an account on that writes a username and password to a text file located in the resource folder. Obviously this has no security at all as it wasn't designed to be secure just to showcase file handling however my teacher has said that I should attempt to add some encryption to the file as well to get a better grade.

I have done some research and many people are recommending DES.

The problem I'm having is I don't have much time left for my project and need to finish it asap. Using DES seems like it would take a while to implement all the extra code.

In my program I am using a simple lineNumberReader to read the files line by line. To write to the files I am using a BufferedWriter.

Is there anyway to encrypt this data very simply? It doesn't have to be very secure but I need to show that I have atleast attempted to encrypt the data. The encryption and decryption would all be completed on the same application as data isn't being transferred.

Potentially a way I can create a very simple encryption and decryption algorithm myself?

like image 861
Patch Avatar asked Jan 15 '15 11:01

Patch


4 Answers

An easy and fun scrambling algorithm would be the Burrows-Wheeler transform. Not really a secure encryption, but seriously, it's a school work and this is awesome.

like image 107
Olavi Mustanoja Avatar answered Nov 09 '22 18:11

Olavi Mustanoja


Try this,... Its pretty simple

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class HelloWorld{
    public static void main(String[] args) {

        try{
            KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
            SecretKey myDesKey = keygenerator.generateKey();

            Cipher desCipher;
            desCipher = Cipher.getInstance("DES");


            byte[] text = "No body can see me.".getBytes("UTF8");


            desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
            byte[] textEncrypted = desCipher.doFinal(text);

            String s = new String(textEncrypted);
            System.out.println(s);

            desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
            byte[] textDecrypted = desCipher.doFinal(textEncrypted);

            s = new String(textDecrypted);
            System.out.println(s);
        }catch(Exception e)
        {
            System.out.println("Exception");
        }
    }
}

So basically before writing to file you will encrypt and after reading you will need to decrypt it.

like image 33
j4rey Avatar answered Nov 09 '22 18:11

j4rey


use simple subtitute encryption algorythm, change every character into number or other character.

  1. get every character of your string.
  2. get the ascii value of the string.
  3. add the ascii value with specific integer (this will be your encryption key)
  4. display the result
like image 3
pupil Avatar answered Nov 09 '22 17:11

pupil


A very basic method would be to xor the data with a key. This method is symmetrical, i.e you can use the same key to decode as encode.

If we choose a 1 byte key it's nice and simple, enough to make it unreadable (but not at all secure!):

private void encodeDecode(byte[] bytes, byte key) {
    for(int i=0; i<bytes.length; i++)
        bytes[i] = (byte) (bytes[i]^key);
}
like image 3
weston Avatar answered Nov 09 '22 18:11

weston