[Michael T. Babcock] Android stuff

Random tips

I'm working on a program that requires decrypting a file on an Android tablet. I'm generating the file on the Linux side by sending it through gzip and then "openssl enc -aes-256-ecb ...", and I wanted to read it one line at a time easily in my Android java code. Here's the function I call:

private BufferedReader cryptoStream(File cryptedfile) throws IOException
	final byte[] keyspec = ...;
    	SecretKeySpec skeySpec = new SecretKeySpec(keyspec, "AES");
        /* First we decrypt, then decompress the data stream */
        try {
		Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
		cipher.init(Cipher.DECRYPT_MODE, skeySpec, 
				new IvParameterSpec(keyspec)));
		return new BufferedReader(new 
				InputStreamReader(new GZIPInputStream(new 
				CipherInputStream(new FileInputStream(cryptedfile), cipher)), "US-ASCII")
	} catch (NoSuchAlgorithmException e) {
		Log.e(TAG, "Cipher.getInstance failed - no such cipher");
		return null;
	} catch (NoSuchPaddingException e) {
		Log.e(TAG, "Cipher.getInstance failed - no such padding");
		return null;
	} catch (InvalidKeyException e) {
		Log.e(TAG, "Cipher.init failed - bad key");
		return null;
	} catch (InvalidAlgorithmParameterException e) {
		Log.e(TAG, "Cipher.init failed - invalid algorithm");
		return null;

I'm then calling this quite simply with:

final BufferedReader datareader = cryptoStream(file);
while ((line = datareader.readline()) != null) { ... }

I'm purposely throwing the IO exception back up the chain so it can be handled at the level where the data's being parsed and dealt with which is probably closer to the UI code. At any rate, if you wanted to decrypt a compressed stream, this works great for me and is easy to read.

Stumble it! XFN Friendly Powered by DJBDNS Powered by Zope Valid CSS! Website Security Test

Served by:  Zope 2.7.6

Page Copyright © 2014, Michael T. Babcock. All Rights Reserved.

To contact me, send an E-mail to sawyoursite at this domain.

If you'd really like your mail server reported for spam, send me some junk mail to junk-yum@mikebabcock.me or devnull@mikebabcock.me. This site powered by djbdns