Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.jens.myapplication.util; /*w w w. j av a2 s. c om*/ import android.util.Base64; import android.util.Log; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** Aes encryption */ public class AESEncryption { private static final String ENCRYPTION_KEY = "6fO35)_-851*AcbF"; private static SecretKeySpec secretKey; private static byte[] key; private static void initKey() { MessageDigest sha = null; try { key = ENCRYPTION_KEY.getBytes("UTF-8"); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); // use only first 128 bit secretKey = new SecretKeySpec(key, "AES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } public static String encrypt(String strToEncrypt) { if(key == null){ initKey(); } try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); // setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")))); return Base64.encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")), Base64.DEFAULT); } catch (Exception e) { Log.e("AESEncryption", "Error while encrypting: " + e.toString()); } return null; } public static String decrypt(String strToDecrypt) { if(key == null){ initKey(); } try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, secretKey); //setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)))); return new String(cipher.doFinal(Base64.decode(strToDecrypt, Base64.DEFAULT))); } catch (Exception e) { Log.e("AESEncryption", "Error while decrypting: " + e.toString()); } return null; } }