Payloads use AES-256-CBC. The AES key is the first 32 characters of the SHA-256 hex digest of your encryption key, used as bytes. The IV is the fixed 16-byte string 1234567890123456, padding is PKCS#7, and ciphertext is Base64-encoded.
import hashlib, base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
IV = b"1234567890123456"
def derive_key(key: str) -> bytes:
return hashlib.sha256(key.encode()).hexdigest()[:32].encode()
def system_encryption(plain_text: str, key: str) -> str:
cipher = AES.new(derive_key(key), AES.MODE_CBC, IV)
encrypted = cipher.encrypt(pad(plain_text.encode("utf-8"), AES.block_size))
return base64.b64encode(encrypted).decode()
def system_decryption(cipher_text: str, key: str) -> str:
cipher = AES.new(derive_key(key), AES.MODE_CBC, IV)
decrypted = unpad(cipher.decrypt(base64.b64decode(cipher_text)), AES.block_size)
return decrypted.decode("utf-8")
import java.security.MessageDigest;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class SystemEncryption {
private static final String IV = "1234567890123456";
private static byte[] deriveKey(String key) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(key.getBytes("UTF-8"));
StringBuilder hex = new StringBuilder();
for (byte b : hash) hex.append(String.format("%02x", b));
return hex.substring(0, 32).getBytes("UTF-8");
}
public static String encrypt(String plainText, String key) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(deriveKey(key), "AES"),
new IvParameterSpec(IV.getBytes("UTF-8")));
return Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes("UTF-8")));
}
public static String decrypt(String cipherText, String key) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(deriveKey(key), "AES"),
new IvParameterSpec(IV.getBytes("UTF-8")));
return new String(cipher.doFinal(Base64.getDecoder().decode(cipherText)), "UTF-8");
}
}
const crypto = require("crypto");
const IV = Buffer.from("1234567890123456", "utf8");
function deriveKey(key) {
return Buffer.from(crypto.createHash("sha256").update(key).digest("hex").slice(0, 32), "utf8");
}
function systemEncryption(plainText, key) {
const c = crypto.createCipheriv("aes-256-cbc", deriveKey(key), IV);
return Buffer.concat([c.update(plainText, "utf8"), c.final()]).toString("base64");
}
function systemDecryption(cipherText, key) {
const d = crypto.createDecipheriv("aes-256-cbc", deriveKey(key), IV);
return Buffer.concat([d.update(Buffer.from(cipherText, "base64")), d.final()]).toString("utf8");
}
const IV = "1234567890123456";
function deriveKey(string $key): string {
return substr(hash("sha256", $key), 0, 32);
}
function systemEncryption(string $plain, string $key): string {
return base64_encode(openssl_encrypt($plain, "aes-256-cbc", deriveKey($key), OPENSSL_RAW_DATA, IV));
}
function systemDecryption(string $cipher, string $key): string {
return openssl_decrypt(base64_decode($cipher), "aes-256-cbc", deriveKey($key), OPENSSL_RAW_DATA, IV);
}