Posts

Showing posts with the label AES

How to encrypt and decrypt strings in android using AES?

Simple helper class to encrypt and decrypt strings using AES128. The result is Ascii-encoded (actually hex, no base64), so no byte[] has to be stored. A SEED value is used as a shared secret ("Master-Password"). Only with the same SEED the stored values can be decrypted.  import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /**  * Usage:  * <pre>  * String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)  * ...  * String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)  * </pre>  * @author ferenc.hechler  */ public class SimpleCrypto {  public static String encrypt(String seed, String cleartext) throws Exception {   byte[] rawKey = getRawKey(seed.getBytes());   byte[] result = encrypt(rawKey, cleartext.getBytes());   return toHex(result);  }  pu...