Skip to content

Instantly share code, notes, and snippets.

@aogilvie
Created August 19, 2013 08:52
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aogilvie/6267013 to your computer and use it in GitHub Desktop.
Save aogilvie/6267013 to your computer and use it in GitHub Desktop.
Quick and sexy String encrypt / decrypt for Android

#Quick and sexy String encrypt / decrypt for Android

Sometimes you find yourself wanted to use SharedPrefs to hold something precious? Want to prevent those pesky rooted hackz0rs from looking in your SharedPreds? Here is a quick and sexy no-external-libs-required solution.

Notes;

  • I have declared everything static so you can create your own utility class and throw the following straight in.

  • 3rd party imports are not required. Use import android.util.Base64; for Base64, and import javax.crypto.*; for Cipher.

  • You can change .getInstance("DES") to MD5 or whatever you prefer.

Encrypt It

private static String cryptoPass = "sup3rS3xy";
    
public static String encryptIt(String value) {
	try {
		DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(keySpec);

		byte[] clearText = value.getBytes("UTF8");
		// Cipher is not thread safe
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, key);

		String encrypedValue = Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT);
		Log.d(TAG, "Encrypted: " + value + " -> " + encrypedValue);
		return encrypedValue;

	} catch (InvalidKeyException e) {
		e.printStackTrace();
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} catch (InvalidKeySpecException e) {
		e.printStackTrace();
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	} catch (BadPaddingException e) {
		e.printStackTrace();
	} catch (NoSuchPaddingException e) {
		e.printStackTrace();
	} catch (IllegalBlockSizeException e) {
		e.printStackTrace();
	}
	return value;
}; 

Decrypt It

private static String cryptoPass = "sup3rS3xy";

public static String decryptIt(String value) {
	try {
		DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(keySpec);

		byte[] encrypedPwdBytes = Base64.decode(value, Base64.DEFAULT);
		// cipher is not thread safe
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));

		String decrypedValue = new String(decrypedValueBytes);
		Log.d(TAG, "Decrypted: " + value + " -> " + decrypedValue);
		return decrypedValue;

	} catch (InvalidKeyException e) {
		e.printStackTrace();
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} catch (InvalidKeySpecException e) {
		e.printStackTrace();
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	} catch (BadPaddingException e) {
		e.printStackTrace();
	} catch (NoSuchPaddingException e) {
		e.printStackTrace();
	} catch (IllegalBlockSizeException e) {
		e.printStackTrace();
	}
	return value;
} 
@aogilvie
Copy link
Author

You can have a blanket exception here, I just show the possible exceptions should you care about one.

@nizarmostefa
Copy link

yyyyyyyyyis

@sudarshan103
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment