I’m having a challenge converting the PHP openssl_* functions to GO language. I want to be able to encrypt a text using PHP and decrypt it using GO. This is the PHP functions encryption and decryption:
<?php
function Encrypt($ClearTextData)
{
$secretKey = 'RJBwYSb5dFcyqUEHD300jY9nhxmi6Lnx';
try {
$EncryptionKey = base64_decode($secretKey);
$InitializationVector = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-256-CBC'));
$EncryptedText = openssl_encrypt($ClearTextData, 'AES-256-CBC', $EncryptionKey, 0, $InitializationVector);
$data = base64_encode($EncryptedText . '::' . $InitializationVector);
return $data;
} catch (Exception $ex) {
throw $ex;
}
}
function Decrypt($CipherData)
{
$secretKey = 'RJBwYSb5dFcyqUEHD300jY9nhxmi6Lnx';
try {
if (!$CipherData) {
throw new ErrorException('Empty cipher string', 4004);
}
$EncryptionKey = base64_decode($secretKey);
list($Encrypted_Data, $InitializationVector) = array_pad(explode('::', base64_decode($CipherData), 2), 2, null);
return openssl_decrypt($Encrypted_Data, 'AES-256-CBC', $EncryptionKey, 0, $InitializationVector);
} catch (Exception $ex) {
throw $ex;
}
}
I want to have the same functions in GO. The decrypt function in GO should be able to decrypt what has been encrypted in PHP