/** * @param s * @return * @throws GeneralSecurityException * @throws UnsupportedEncodingException */ public static String encrypt(final String s) throws GeneralSecurityException, UnsupportedEncodingException { String encrypted = null; try { if (s != null) { final SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); final SecretKey secretKey = secretKeyFactory.generateSecret(new PBEKeySpec(secret.toCharArray())); final Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new PBEParameterSpec(SALT, 20)); final byte[] stringBytes = s.getBytes("UTF-8"); final byte[] encryptedBytes = cipher.doFinal(stringBytes); final byte[] encodedBytes = Base64.encode(encryptedBytes, Base64.DEFAULT); encrypted = new String(encodedBytes, "UTF-8"); } } catch (GeneralSecurityException x) { throw x; } catch (UnsupportedEncodingException x) { throw x; } catch (Exception x) { DBG.m(x); } return encrypted; }
// encrypts string public String encrypt(String toEncrypt) { try { // encrypt byte[] bytes = toEncrypt.getBytes("ASCII"); byte[] encoded = encryptionCipher.doFinal(bytes); return Base64.encode(encoded); } catch (Exception e) { System.out.println("Encryption Error: " + e); } return null; }