Skip to content

Instantly share code, notes, and snippets.

@Swisyn
Forked from aogilvie/string_encrypt_decrypt.md
Created April 23, 2016 20:10
Show Gist options
  • Save Swisyn/4c9d76e05ae5d219a6fe2dc28c1d8d11 to your computer and use it in GitHub Desktop.
Save Swisyn/4c9d76e05ae5d219a6fe2dc28c1d8d11 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;
} 
@ypatilbalbatti
Copy link

hi

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