Here you go. This is some old code for a fairly light encryption job. You might want to adjust the algorithm a bit to use at least SHA (also maybe not use DES). I think I'd also make the salt mutable based on a second input, but it really depends on how much you care about protecting what your encrypting.
- Brill import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import android.util.Log; public class PBECypher { private static final String TAG = "PBECypher"; private final static String HEX = "0123456789ABCDEF"; private Cipher encryptCipher; private Cipher decryptCipher; public PBECypher(char[] password) { super(); initCiphers(password); } private void initCiphers(char password[]) { PBEKeySpec pbeKeySpec; PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; int count = 20; pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); try { keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); encryptCipher = Cipher.getInstance("PBEWithMD5AndDES"); decryptCipher = Cipher.getInstance("PBEWithMD5AndDES"); encryptCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); decryptCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); } catch (Exception e) { Log.v(TAG, e.toString()); } } public byte[] encrypt(byte[] decrypted) throws Exception { byte[] output = encryptCipher.doFinal(decrypted); return output; } public byte[] decrypt(byte[] encrypted) throws Exception { byte[] decrypted = decryptCipher.doFinal(encrypted); return decrypted; } public String encryptString(String decrypted) throws Exception { byte[] input = decrypted.getBytes("UTF-8"); return toHexFromByte(encrypt(input)); } public String decryptString(String encrypted) throws Exception { byte[] input = toByteFromHex(encrypted); return new String(decrypt(input), "UTF-8"); } public static String toHex(String txt) { return toHexFromByte(txt.getBytes()); } public static String fromHex(String hex) { return new String(toByteFromHex(hex)); } public static byte[] toByteFromHex(String hexString) { int len = hexString.length() / 2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) { result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue(); } return result; } public static String toHexFromByte(byte[] buf) { if (buf == null) { return ""; } StringBuffer result = new StringBuffer(2 * buf.length); for (int i = 0; i < buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } private static void appendHex(StringBuffer sb, byte b) { sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f)); } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en