Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could pass your file writing stream thru a <code>CipherOutputStream</code></p> <p>Generate a random string, or number or anything. get its byte array, produce a key, and use it to encrypt your file.</p> <pre><code> byte password[] = (WHAT YOUR WANT. STRING, NUMBER, etc.).getBytes(); DESKeySpec desKeySpec; try { desKeySpec = new DESKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(desKeySpec); Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); desCipher.init(Cipher.ENCRYPT_MODE, key); // Create stream FileOutputStream fos = new FileOutputStream("Your file here"); BufferedOutputStream bos = new BufferedOutputStream(fos); CipherOutputStream cos = new CipherOutputStream(bos, desCipher); } </code></pre> <p>Now you can write to the file using <code>cos</code></p> <p>Reading the file is done the same way using the <code>SecretKey</code> object</p> <pre><code>SecretKey key = loadKey(); // Deserialize your SecretKey object try { Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); FileInputStream fis = new FileInputStream("Your file here"); BufferedInputStream bis = new BufferedInputStream(fis); CipherInputStream cis = new CipherInputStream(bis, cipher); </code></pre> <p>now you can read using <code>cis</code></p> <p>The downside is you need to keep the <code>SecretKey</code> object (Serialize it or something) it wouldn't be a problem for any low level hacker to get the data (since the key is stored on the device) but it wouldn't allow just changing your data using a text editor.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload