Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Last active June 18, 2022 03:59
Show Gist options
  • Save thomasdarimont/fae409eaae2abcf83bd6633b961e7f00 to your computer and use it in GitHub Desktop.
Save thomasdarimont/fae409eaae2abcf83bd6633b961e7f00 to your computer and use it in GitHub Desktop.
Example for 128bit AES with Java and PHP
<?php
class AesCipher {
private const OPENSSL_CIPHER_NAME = "aes-128-cbc";
private const CIPHER_KEY_LEN = 16; //128 bits
private static function fixKey($key) {
if (strlen($key) < AesCipher::CIPHER_KEY_LEN) {
//0 pad to len 16
return str_pad("$key", AesCipher::CIPHER_KEY_LEN, "0");
}
if (strlen($key) > AesCipher::CIPHER_KEY_LEN) {
//truncate to 16 bytes
return substr($key, 0, AesCipher::CIPHER_KEY_LEN);
}
return $key;
}
/**
* Encrypt data using AES Cipher (CBC) with 128 bit key
*
* @param type $key - key to use should be 16 bytes long (128 bits)
* @param type $iv - initialization vector
* @param type $data - data to encrypt
* @return encrypted data in base64 encoding with iv attached at end after a :
*/
static function encrypt($key, $iv, $data) {
$encodedEncryptedData = base64_encode(openssl_encrypt($data, AesCipher::OPENSSL_CIPHER_NAME, AesCipher::fixKey($key), OPENSSL_RAW_DATA, $iv));
$encodedIV = base64_encode($iv);
$encryptedPayload = $encodedEncryptedData.":".$encodedIV;
return $encryptedPayload;
}
/**
* Decrypt data using AES Cipher (CBC) with 128 bit key
*
* @param type $key - key to use should be 16 bytes long (128 bits)
* @param type $data - data to be decrypted in base64 encoding with iv attached at the end after a :
* @return decrypted data
*/
static function decrypt($key, $data) {
$parts = explode(':', $data); //Separate Encrypted data from iv.
$encrypted = $parts[0];
$iv = $parts[1];
$decryptedData = openssl_decrypt(base64_decode($encrypted), AesCipher::OPENSSL_CIPHER_NAME, AesCipher::fixKey($key), OPENSSL_RAW_DATA, base64_decode($iv));
return $decryptedData;
}
};
$decrypted = AesCipher::decrypt('0123456789abcdef', 'G0a6cK+sxBkSwyCjcG4efA==:YWJjZGVmOTg3NjU0MzIxMA==');
var_dump($decrypted);
?>
package de.tdlabs.training.crypto;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AesCrypto {
private static String CIPHER_NAME = "AES/CBC/PKCS5PADDING";
private static int CIPHER_KEY_LEN = 16; //128 bits
/**
* Encrypt data using AES Cipher (CBC) with 128 bit key
*
* @param key - key to use should be 16 bytes long (128 bits)
* @param iv - initialization vector
* @param data - data to encrypt
* @return encryptedData data in base64 encoding with iv attached at end after a :
*/
public static String encrypt(String key, String iv, String data) {
try {
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
SecretKeySpec secretKey = new SecretKeySpec(fixKey(key).getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance(CIPHER_NAME);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
byte[] encryptedData = cipher.doFinal((data.getBytes()));
String encryptedDataInBase64 = Base64.getEncoder().encodeToString(encryptedData);
String ivInBase64 = Base64.getEncoder().encodeToString(iv.getBytes(StandardCharsets.UTF_8));
return encryptedDataInBase64 + ":" + ivInBase64;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
private static String fixKey(String key) {
if (key.length() < CIPHER_KEY_LEN) {
int numPad = CIPHER_KEY_LEN - key.length();
for (int i = 0; i < numPad; i++) {
key += "0"; //0 pad to len 16 bytes
}
return key;
}
if (key.length() > CIPHER_KEY_LEN) {
return key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
}
return key;
}
/**
* Decrypt data using AES Cipher (CBC) with 128 bit key
*
* @param key - key to use should be 16 bytes long (128 bits)
* @param data - encrypted data with iv at the end separate by :
* @return decrypted data string
*/
public static String decrypt(String key, String data) {
try {
String[] parts = data.split(":");
IvParameterSpec iv = new IvParameterSpec(Base64.getDecoder().decode(parts[1]));
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance(CIPHER_NAME);
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] decodedEncryptedData = Base64.getDecoder().decode(parts[0]);
byte[] original = cipher.doFinal(decodedEncryptedData);
return new String(original);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static void main(String[] args) {
String key = "0123456789abcdef"; // 128 bit key
String initVector = "abcdef9876543210"; // 16 bytes IV, it is recommended to use a different random IV for every message!
String plain_text = "plain text";
String encrypted = encrypt(key, initVector, plain_text);
System.out.println(encrypted);
String decrypt = decrypt(key, encrypted);
System.out.println(decrypt);
}
}
@sandip-maji
Copy link

sandip-maji commented Oct 19, 2020

PHP Code :

$iv = "8147829559990027";
$key = "SANDIP0000000000";
$data = "sandip";
$encodedEncryptedData = base64_encode(openssl_encrypt($data, "aes-256-cbc",$key, OPENSSL_RAW_DATA, $iv));
echo $encodedEncryptedData;

Output :

wbkrmlU2fFKhvSxd0f5wsA==

Java Code :

public class Posttouch {

private static String CIPHER_NAME = "AES/CBC/PKCS5PADDING";

public static void main(String args[]) {
	System.out.println("working");
	String key = "SANDIP0000000000";
	String iv = "8147829559990027";
	String data = "sandip";
	String encrypt = null;
	try {
		encrypt = encrypt(key, iv, data);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	System.out.println(encrypt);
}

public static String encrypt(String key, String iv, String data) {

	try {
		IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
		SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");

		Cipher cipher = Cipher.getInstance(CIPHER_NAME);
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);

		byte[] encryptedData = cipher.doFinal((data.getBytes()));

		String encryptedDataInBase64 = Base64.getEncoder().encodeToString(encryptedData);
		String ivInBase64 = Base64.getEncoder().encodeToString(iv.getBytes(StandardCharsets.UTF_8));

		return encryptedDataInBase64;

	} catch (Exception ex) {
		throw new RuntimeException(ex);
	}
}

}

Output :
afYoGd4LZfWBrQ3cgf61vQ==

What is the issue in java code? generating different encrypting string, please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment