Results 1 to 5 of 5
  1. #1
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Encrypting data and saving to a file?

    Hi,

    I am working on a project that is almost complete and all that is left to do is encryption. I have looked around on google and don't really know where to look or start. What I am trying to do is save data to file, for instance a user enters a username and password which is then stored to a file. This however needs to be encrypted somehow and stored to a file. Any ideas?

    Thanks,

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Encrypting data and saving to a file?

    Look at the java.security and javax.crypto packages. Do a Search for code samples that use these packages.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Encrypting data and saving to a file?

    cheers will do.

  4. #4
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Encrypting data and saving to a file?

    Okay I have figured out how to make my system more secure by using md5 hashing for the login details and the DES encryption and decryption for the csv file. The next step is adding to the file , I have thought about it and the only way I can think of is decrypting the file and adding it to the decrypted file, then to encrypt it again and delete the decrypted file. The problem now is that it will show a decrypted file when the user session is running and if the program errors in anyway it will leave the decrypted file there. There is one other way I was thinking was to store it as a string or 2d array? This is my code at the moment.
    Java Code:
    
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    
    public class CipherExample {
    
    	public static void main(String[] args) {
    		try {
    			String key = "squirrel123"; // needs to be at least 8 characters for DES
    
    			FileInputStream fis = new FileInputStream("transactioncsv.csv");
    			FileOutputStream fos = new FileOutputStream("transactioncsve.csv");
    			encrypt(key, fis, fos);
    
    			FileInputStream fis2 = new FileInputStream("transactioncsve.csv");
    			FileOutputStream fos2 = new FileOutputStream("decrypted.csv");
    			decrypt(key, fis2, fos2);
    		} catch (Throwable e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
    		encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
    	}
    
    	public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
    		encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
    	}
    
    	public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {
    
    		DESKeySpec dks = new DESKeySpec(key.getBytes());
    		SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    		SecretKey desKey = skf.generateSecret(dks);
    		Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE
    
    		if (mode == Cipher.ENCRYPT_MODE) {
    			cipher.init(Cipher.ENCRYPT_MODE, desKey);
    			CipherInputStream cis = new CipherInputStream(is, cipher);
    			doCopy(cis, os);
    		} else if (mode == Cipher.DECRYPT_MODE) {
    			cipher.init(Cipher.DECRYPT_MODE, desKey);
    			CipherOutputStream cos = new CipherOutputStream(os, cipher);
    			doCopy(is, cos);
    		}
    	}
    
    	public static void doCopy(InputStream is, OutputStream os) throws IOException {
    		byte[] bytes = new byte[64];
    		int numBytes;
    		while ((numBytes = is.read(bytes)) != -1) {
    			os.write(bytes, 0, numBytes);
    		}
    		os.flush();
    		os.close();
    		is.close();
    	}
    
    }
    Transactionollyhal.csv file:


    12 Apr 2012,Start,200.0,Bills,Deposit,200.0,sds,
    13 Apr 2012,Start,200.0,Bills,Withdraw,200.0,sadad,
    14 Apr 2012,Start,100.0,Bills,Deposit,300.0,dsad,
    15 Apr 2012,Start,100.0,Bills,Deposit,100.0,dasda,
    16 Apr 2012,Start,300.0,Bills,Deposit,200.0,dsadas,

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Encrypting data and saving to a file?

    IF the file is not huge, keep it in memory while adding to it. Read and decrypt into memory, add on in memory, write out encrypted.

    Use ByteArray streams vs File Streams
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Encrypting a big file
    By ebing in forum Advanced Java
    Replies: 1
    Last Post: 08-10-2011, 07:55 AM
  2. how to extract specific data from image file and saving in to database
    By Balasubrahmanyam in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-27-2011, 02:19 PM
  3. Saving data from a socket to a file in real time
    By colossusdub in forum New To Java
    Replies: 3
    Last Post: 03-03-2010, 07:32 PM
  4. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 09:24 PM
  5. Encrypting File in Java
    By sandeepkk2005 in forum Advanced Java
    Replies: 0
    Last Post: 11-27-2007, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •