-
-
Save Ikhiloya/d4405befb7750a75e8f45886c65dfdbc to your computer and use it in GitHub Desktop.
AES Encryption/Decryption on Android's Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.crypto.spec.IvParameterSpec; | |
import java.security.MessageDigest; | |
import java.security.spec.AlgorithmParameterSpec; | |
import android.util.Base64; | |
public class AESEnDecryption { | |
/* | |
//Sample Code | |
public static void main(String[] args) throws Exception{ | |
String msg = "123456"; | |
String keyStr = "abcdef"; | |
String ivStr = "ABCDEF"; | |
byte[] msg_byte = msg.getBytes("UTF-8"); | |
System.out.println("Before Encrypt: " + msg); | |
byte[] ans = AESEnDecryption.encrypt(ivStr, keyStr, msg.getBytes()); | |
System.out.println("After Encrypt: " + new String(ans, "UTF-8")); | |
String ansBase64 = AESEnDecryption.encryptStrAndToBase64(ivStr, keyStr, msg); | |
System.out.println("After Encrypt & To Base64: " + ansBase64); | |
byte[] deans = AESEnDecryption.decrypt(ivStr, keyStr, ans); | |
System.out.println("After Decrypt: " + new String(deans, "UTF-8")); | |
String deansBase64 = AESEnDecryption.decryptStrAndFromBase64(ivStr, keyStr, ansBase64); | |
System.out.println("After Decrypt & From Base64: " + deansBase64);} | |
*/ | |
public static byte[] encrypt(String ivStr, String keyStr, byte[] bytes) throws Exception{ | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
md.update(ivStr.getBytes()); | |
byte[] ivBytes = md.digest(); | |
MessageDigest sha = MessageDigest.getInstance("SHA-256"); | |
sha.update(keyStr.getBytes()); | |
byte[] keyBytes = sha.digest(); | |
return encrypt(ivBytes, keyBytes, bytes); | |
} | |
static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] bytes) throws Exception{ | |
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); | |
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec); | |
return cipher.doFinal(bytes); | |
} | |
public static byte[] decrypt(String ivStr, String keyStr, byte[] bytes) throws Exception{ | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
md.update(ivStr.getBytes()); | |
byte[] ivBytes = md.digest(); | |
MessageDigest sha = MessageDigest.getInstance("SHA-256"); | |
sha.update(keyStr.getBytes()); | |
byte[] keyBytes = sha.digest(); | |
return decrypt(ivBytes, keyBytes, bytes); | |
} | |
static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] bytes) throws Exception{ | |
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); | |
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); | |
return cipher.doFinal(bytes); | |
} | |
public static String encryptStrAndToBase64(String ivStr, String keyStr, String enStr) throws Exception{ | |
byte[] bytes = encrypt(keyStr, keyStr, enStr.getBytes("UTF-8")); | |
return new String(Base64.encode(bytes ,Base64.DEFAULT), "UTF-8"); | |
} | |
public static String decryptStrAndFromBase64(String ivStr, String keyStr, String deStr) throws Exception{ | |
byte[] bytes = decrypt(keyStr, keyStr, Base64.decode(deStr.getBytes("UTF-8"),Base64.DEFAULT)); | |
return new String(bytes, "UTF-8"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment