Monday, July 31, 2017

Starting with Basic File Cryptography (XOR Cipher)

  In this blog post, I'll discuss one interesting basic cipher technique to garble a file: The "XOR Cipher". What it does is obviously apply XOR (outputs true if both inputs are different) function for every bytes.

  It's not rocket-science like other more sophisticated algorithms like AES, DES, or Blowfish; but adequate for blazingly-fast-low-level-security encryption.

  The snippets I will show you adds a little twist. We will only encrypt the first 512 bytes of the file so that our code is scalable for larger files.


The code:

import java.io.*;
public class ImageCrypto {
private static final int XOR_KEY = 345;
private static final int BYTE_LENGTH = 512;
public static void applyXor( File file ) throws IOException {
RandomAccessFile rac = new RandomAccessFile(file, "rw");
byte[] data = new byte[BYTE_LENGTH];
rac.read( data );
byte[] newData = xor( data );
rac.seek(0);
rac.write(newData);
rac.close();
}
public static byte[] xor( byte[] data ) {
byte[] newData = new byte[data.length];
for (int i = 0; i < data.length; i++) {
newData[i] = xor(data[i]);
}
return newData;
}
public static byte xor(byte b) {
return (byte) (0xFF & ((int) b ^ XOR_KEY));
}
}

Some test:
public static void main(String[] args) throws IOException {
File file = new File( "D:\\test images\\8.png" );
long start = System.nanoTime();
ImageCrypto.applyXor(file);
long end = System.nanoTime() - start;
System.out.printf("Took: %d nanoseconds or %d milliseconds\n", end, (end/1000000));
}
Output:
Took: 1085333 nanoseconds or 1 milliseconds
view raw Main.java hosted with ❤ by GitHub

Friday, July 28, 2017

Reading File MIME Type

  What I'm about to share with you covers basic MIME type reading. In some cases, you don't want to use additional external libraries for your project. For this simple task, using readily available classes on Java is always a joy.

  Take extra care because not all commonly known MIME types are supported. I will show you that on the last part. Let's start.

The code:

package blog.boydeploy.mimetype;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Objects;
public class MimeUtility {
public static String getMimeType(File file) throws FileNotFoundException {
Objects.requireNonNull(file);
if (!file.exists()) throw new FileNotFoundException("File is missing: " + file.getAbsolutePath());
FileNameMap fmap = URLConnection.getFileNameMap();
return fmap.getContentTypeFor(file.toURI().getPath());
}
}

Some Test:

package blog.boydeploy.mimetype;
import java.io.File;
import java.io.FileNotFoundException;
public class Main {
public static void main(String... args) throws Exception {
printMIME(new File("D:\\CS-KEYBOARD SHORTCUTS.pdf"));
printMIME(new File("D:\\1.jpg"));
}
private static void printMIME(File f) throws FileNotFoundException {
System.out.println("file = " + f.getAbsolutePath());
System.out.println("mime = " + MimeUtility.getMimeType(f));
}
}
Output:
file = D:\CS-KEYBOARD SHORTCUTS.pdf
mime = application/pdf
file = D:\1.jpg
mime = image/jpeg
view raw Main.java hosted with ❤ by GitHub

What else you need to know?

The list of supported MIME types are located on your JRE. Locate the file named content-types.properties at <JRE>/lib directory.  This file is being read by sun.net.www.MimeTable class which happens to be an implementation of java.net.FileNameMap. And, the default instance returned by URLConnection.getFileNameMap() function.

Starting with Basic File Cryptography (XOR Cipher)

  In this blog post, I'll discuss one interesting basic cipher technique to garble a file: The "XOR Cipher". What it does is o...