Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>If you take user input for the password make sure to read <a href="https://stackoverflow.com/a/3452620/1188357">this answer</a>.</strong></p> <p>You should take a look at: <a href="http://developer.android.com/reference/javax/crypto/CipherInputStream.html" rel="noreferrer">CipherInputStream</a> and <a href="http://developer.android.com/reference/javax/crypto/CipherOutputStream.html" rel="noreferrer">CipherOutputStream</a>. They are used to encrypt and decrypt byte streams.</p> <p>I have a file named <code>cleartext</code>. The file contains:</p> <blockquote> <pre><code>Hi, I'm a clear text. How are you? That's awesome! </code></pre> </blockquote> <p>Now, you have an <code>encrypt()</code> function:</p> <pre><code>static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { // Here you read the cleartext. FileInputStream fis = new FileInputStream("data/cleartext"); // This stream write the encrypted text. This stream will be wrapped by another stream. FileOutputStream fos = new FileOutputStream("data/encrypted"); // Length is 16 byte // Careful when taking user input!!! https://stackoverflow.com/a/3452620/1188357 SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); // Create cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, sks); // Wrap the output stream CipherOutputStream cos = new CipherOutputStream(fos, cipher); // Write bytes int b; byte[] d = new byte[8]; while((b = fis.read(d)) != -1) { cos.write(d, 0, b); } // Flush and close streams. cos.flush(); cos.close(); fis.close(); } </code></pre> <p>After you execute this function, there should be a file names <code>encrypted</code>. The file contains the encrypted characters.</p> <p>For decryption you have the <code>decrypt</code> function:</p> <pre><code>static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { FileInputStream fis = new FileInputStream("data/encrypted"); FileOutputStream fos = new FileOutputStream("data/decrypted"); SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, sks); CipherInputStream cis = new CipherInputStream(fis, cipher); int b; byte[] d = new byte[8]; while((b = cis.read(d)) != -1) { fos.write(d, 0, b); } fos.flush(); fos.close(); cis.close(); } </code></pre> <p>After the execution of decrypt, there should be a file named <code>decrypted</code>. This file contains the free text.</p> <p>You write you're a "noob" but depending on the use-case of encryption you could do a lot of harm if you're not doing it the right way. Know your tools!</p> <p>Usage of CipherOutputStream <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/security/CryptoSpec.html#CipherOutput" rel="noreferrer">Oracle documentation</a>:</p> <pre><code>SecretKeySpec skeySpec = new SecretKeySpec(y.getBytes(), "AES"); FileInputStream fis; FileOutputStream fos; CipherOutputStream cos; // File you are reading from fis = new FileInputStream("/tmp/a.txt"); // File output fos = new FileOutputStream("/tmp/b.txt"); // Here the file is encrypted. The cipher1 has to be created. // Key Length should be 128, 192 or 256 bit =&gt; i.e. 16 byte SecretKeySpec skeySpec = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); Cipher cipher1 = Cipher.getInstance("AES"); cipher1.init(Cipher.ENCRYPT_MODE, skeySpec); cos = new CipherOutputStream(fos, cipher1); // Here you read from the file in fis and write to cos. byte[] b = new byte[8]; int i = fis.read(b); while (i != -1) { cos.write(b, 0, i); i = fis.read(b); } cos.flush(); </code></pre> <p>Thus, the encryption should work. When you reverse the process, you should be able to read the decrypted bytes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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