Skip to content

Instantly share code, notes, and snippets.

@dafahan
Last active March 29, 2024 12:34
Show Gist options
  • Save dafahan/955e8ef28a0653914b6f6adaadf00126 to your computer and use it in GitHub Desktop.
Save dafahan/955e8ef28a0653914b6f6adaadf00126 to your computer and use it in GitHub Desktop.
AES-256 encryption and decryption in PHP , Go, and Python
<?php
//the key & iv should be generated with php
// Generate a random key
$key_length = 32; // 256 bits for AES-256
$key = openssl_random_pseudo_bytes($key_length);
// Encode the key in base64 format for storage or transmission
$encoded_key = base64_encode($key);
echo "Generated Key: " . $encoded_key . "\n";
// Generate a random IV
$iv_length = 16; // 128 bits for AES
$iv = openssl_random_pseudo_bytes($iv_length);
// Convert the IV to hexadecimal format for storage or transmission
$hex_iv = bin2hex($iv);
echo "Generated IV (hex): " . $hex_iv . "\n";
// You can convert the IV back to binary format when needed
$binary_iv = hex2bin($hex_iv);
echo "Generated IV (binary): " . base64_encode($binary_iv) . "\n";
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/hex"
"fmt"
)
func main() {
key, err := base64.StdEncoding.DecodeString("hMoeP4BjNDpPMIxxSwtOfOUIfZVZflBlZV8T2oDRNCI=")
if err != nil {
panic(err)
}
iv, err := hex.DecodeString("a043d58b6a7865a823f0e795a7b6e7eb")
if err != nil {
panic(err)
}
plaintext := []byte("12345678912345678912345678900000")
// Encrypt plaintext
ciphertext := encrypt(plaintext, key, iv)
fmt.Printf("EncryptedText: %s\n", ciphertext)
// Decrypt ciphertext
decryptedPlaintext := decrypt(ciphertext, key, iv)
fmt.Printf("DecryptedText: %s\n", decryptedPlaintext)
}
func encrypt(plaintext, key, iv []byte) string {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
plaintext = pkcs7Pad(plaintext, aes.BlockSize)
ciphertext := make([]byte, len(plaintext))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, plaintext)
return base64.StdEncoding.EncodeToString(ciphertext)
}
func decrypt(ciphertext string, key, iv []byte) string {
data, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
panic(err)
}
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(data, data)
return string(pkcs7Unpad(data))
}
func pkcs7Pad(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padtext...)
}
func pkcs7Unpad(data []byte) []byte {
length := len(data)
unpadding := int(data[length-1])
return data[:(length - unpadding)]
}
<?php
function encrypt_data($plaintext, $key, $iv) {
// Encrypt using AES-256-CBC with PKCS7 padding
$ciphertext = openssl_encrypt($plaintext, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
return base64_encode($ciphertext);
}
function decrypt_data($ciphertext, $key, $iv) {
// Decrypt using AES-256-CBC with PKCS7 padding
$decrypted = openssl_decrypt(base64_decode($ciphertext), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
return $decrypted;
}
$plaintext = "12345678912345678912345678900000";
$key = base64_decode("hMoeP4BjNDpPMIxxSwtOfOUIfZVZflBlZV8T2oDRNCI=");
$iv = hex2bin("a043d58b6a7865a823f0e795a7b6e7eb");
$ciphertext = encrypt_data($plaintext, $key, $iv);
echo "Encrypted: " . $ciphertext . "\n";
$decrypted = decrypt_data($ciphertext, $key, $iv);
echo "Decrypted: " . $decrypted . "\n";
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
class Adauth:
def __init__(self):
self.key = base64.b64decode("hMoeP4BjNDpPMIxxSwtOfOUIfZVZflBlZV8T2oDRNCI=")
self.iv = bytes.fromhex("a043d58b6a7865a823f0e795a7b6e7eb")
def encrypt_data(self, data):
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
padded_data = pad(data.encode(), AES.block_size)
encrypted = cipher.encrypt(padded_data)
return base64.b64encode(encrypted).decode()
def decrypt_data(self, encrypted_data):
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
encrypted_bytes = base64.b64decode(encrypted_data)
decrypted = cipher.decrypt(encrypted_bytes)
unpadded_data = unpad(decrypted, AES.block_size)
return unpadded_data.decode()
ad = Adauth()
plaintext = "12345678912345678912345678900000"
print("Actual string:", plaintext)
encrypted_string = ad.encrypt_data(plaintext)
print("Encrypted string:", encrypted_string)
decrypted_string = ad.decrypt_data(encrypted_string)
print("Decrypted string:", decrypted_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment