Skip to content

Instantly share code, notes, and snippets.

@sbelloz
Last active August 29, 2015 14:04
Show Gist options
  • Save sbelloz/16c34b4fd6e16e2d457c to your computer and use it in GitHub Desktop.
Save sbelloz/16c34b4fd6e16e2d457c to your computer and use it in GitHub Desktop.
Encryption and Decryption in Android
package it.bellotti.java.crypto;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
/**
* Created with IntelliJ IDEA.
* User: Simone Bellotti
* Date: 05/06/2014
* Time: 17.06
*/
public class CypherUtils {
// Algorithm used
private final static String ALGORITHM = "AES";
/**
* Encrypt data
*
* @param secretKey - a secret key used for encryption
* @param data - data to encrypt
* @return Encrypted data
* @throws Exception
*/
private static String cipher(String secretKey, String data) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), secretKey.getBytes(), 128, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return toHex(cipher.doFinal(data.getBytes()));
}
/**
* Decrypt data
*
* @param secretKey - a secret key used for decryption
* @param data - data to decrypt
* @return Decrypted data
* @throws Exception
*/
private static String decipher(String secretKey, String data) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), secretKey.getBytes(), 128, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(toByte(data)));
}
// Helper methods
private static byte[] toByte(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;
}
private static String toHex(byte[] stringBytes) {
StringBuffer result = new StringBuffer(2 * stringBytes.length);
for (int i = 0; i < stringBytes.length; i++) {
result.append(HEX.charAt((stringBytes[i] >> 4) & 0x0f)).append(HEX.charAt(stringBytes[i] & 0x0f));
}
return result.toString();
}
public static String encrypt(String SEED, String password) {
try {
return cipher(SEED, password);
} catch (Exception e) {
Log.w("CypherUtils.encrypt", "Errore durante il cipher.", e);
return null;
}
}
public static String decrypt(String SEED, String encryptedPassword) {
try {
return decipher(SEED, encryptedPassword);
} catch (Exception e) {
Log.w("CypherUtils.decrypt", "Errore durante il decipher.", e);
return null;
}
}
private CypherUtils(){}
private final static String HEX = "0123456789ABCDEF";
}
protected String encrypt(String SEED, String password) {
return CypherUtils.encrypt(SEED, password);
}
protected String decrypt(String SEED, String encryptedPassword) {
return CypherUtils.decrypt(SEED, encryptedPassword);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment