Skip to content

Instantly share code, notes, and snippets.

@kyawhtut-cu
Forked from KushtrimPacaj/AesEncryptDecrypt.java
Created August 27, 2021 09:28
Show Gist options
  • Save kyawhtut-cu/17b4afc47c48e3fc99009f4427ec325a to your computer and use it in GitHub Desktop.
Save kyawhtut-cu/17b4afc47c48e3fc99009f4427ec325a to your computer and use it in GitHub Desktop.
AES encryption in Java, equivalent of Crypt ( Encrypter.php class ) on Laravel (PHP)
import android.util.Base64;
import android.util.Log;
import com.google.gson.Gson;
import org.apache.commons.codec.binary.Hex;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AesEncryptDecrypt
{
public static String encrypt(byte[] keyValue, String plaintext) throws Exception {
Key key = new SecretKeySpec(keyValue, "AES");
//serialize
String serializedPlaintext = "s:" + plaintext.getBytes().length + ":\"" + plaintext + "\";";
byte[] plaintextBytes = serializedPlaintext.getBytes("UTF-8");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] iv = c.getIV();
byte[] encVal = c.doFinal(plaintextBytes);
String encryptedData = Base64.encodeToString(encVal, Base64.NO_WRAP);
SecretKeySpec macKey = new SecretKeySpec(keyValue, "HmacSHA256");
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
hmacSha256.init(macKey);
hmacSha256.update(Base64.encode(iv, Base64.NO_WRAP));
byte[] calcMac = hmacSha256.doFinal(encryptedData.getBytes("UTF-8"));
String mac = new String(Hex.encodeHex(calcMac));
//Log.d("MAC",mac);
AesEncryptionData aesData = new AesEncryptionData(
Base64.encodeToString(iv, Base64.NO_WRAP),
encryptedData,
mac);
String aesDataJson = new Gson().toJson(aesData);
return Base64.encodeToString(aesDataJson.getBytes("UTF-8"), Base64.DEFAULT);
}
public static String decrypt(byte[] keyValue, String ivValue, String encryptedData, String macValue) throws Exception {
Key key = new SecretKeySpec(keyValue, "AES");
byte[] iv = Base64.decode(ivValue.getBytes("UTF-8"), Base64.DEFAULT);
byte[] decodedValue = Base64.decode(encryptedData.getBytes("UTF-8"), Base64.DEFAULT);
SecretKeySpec macKey = new SecretKeySpec(keyValue, "HmacSHA256");
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
hmacSha256.init(macKey);
hmacSha256.update(ivValue.getBytes("UTF-8"));
byte[] calcMac = hmacSha256.doFinal(encryptedData.getBytes("UTF-8"));
byte[] mac = Hex.decodeHex(macValue.toCharArray());
if (!Arrays.equals(calcMac, mac))
return "MAC mismatch";
Cipher c = Cipher.getInstance("AES/CBC/PKCS7Padding"); // or PKCS5Padding
c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] decValue = c.doFinal(decodedValue);
int firstQuoteIndex = 0;
while (decValue[firstQuoteIndex] != (byte) '"') firstQuoteIndex++;
return new String(Arrays.copyOfRange(decValue, firstQuoteIndex + 1, decValue.length - 2));
}
}
/**
* Created by Kushtrim on 06.10.2015.
*/
public class AesEncryptionData {
public String iv;
public String value;
public String mac;
public AesEncryptionData(String iv, String value, String mac) {
this.iv = iv;
this.value = value;
this.mac = mac;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment