iv = “0000000000000000”
key = “WwXyBHp3itt+edYciiSTcg==”
php code :
<?php
function encrypt(mixed $value): string
{
$iv = "0000000000000000";
// Encrypt the value using AES-128-CBC and a secret key
$value = openssl_encrypt($value, strtolower(CIPHER),
secret, 0, $iv);
// Check if encryption was successful
if ($value === false) {
throw new EncryptException('Could not encrypt the data.');
}
// Encode IV to Base64 for easier transmission
$iv = base64_encode($iv);
// Create a JSON object containing IV and the encrypted value
$json = json_encode(compact('iv', 'value'), JSON_UNESCAPED_SLASHES);
// Check if JSON encoding was successful
if (json_last_error() !== JSON_ERROR_NONE) {
throw new EncryptException('Could not encrypt the data.');
}
// Return the JSON encoded and Base64 encoded result
return $value; //base64_encode($json);
}
const secret='WwXyBHp3itt+edYciiSTcg==';
const CIPHER = 'AES-128-CBC';
$plain='pay/v1/inquiry#1735222907#POST#WwXyBHp3itt+edYciiSTcg==';
echo encrypt($plain);
result :
1O4fDiwtwOqLHc0eOXm9wFGT3TjWpIKLrVGuq0JI67a4pVTaT1F7ZNnJB8fJNdRl1ZG6xNb9obnFQUeUfSahTQ==
currect result is this.
i coverted code to c#:
private const string Secret = "WwXyBHp3itt+edYciiSTcg==";
public static string Encrypt(string value)
{
using (Aes aes = Aes.Create())
{
aes.Key = Convert.FromBase64String(Secret);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// Generate a random initialization vector (IV)
//aes.GenerateIV();
byte[] iv = Encoding.ASCII.GetBytes("0000000000000000");
using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, iv))
{
using (var ms = new System.IO.MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
byte[] inputBuffer = Encoding.UTF8.GetBytes(value);
cs.Write(inputBuffer, 0, inputBuffer.Length);
cs.FlushFinalBlock();
byte[] encrypted = ms.ToArray();
// Encode IV to Base64 for easier transmission
string ivBase64 = Convert.ToBase64String(iv);
string valueBase64 = Convert.ToBase64String(encrypted);
// Create a JSON object containing IV and the encrypted value
var jsonObject = new { iv = ivBase64, value = valueBase64 };
string json = JsonConvert.SerializeObject(jsonObject);
// Return the Base64 encoded result
return valueBase64; //Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
}
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text = Encrypt("pay/v1/inquiry#1735222907#POST#WwXyBHp3itt+edYciiSTcg==");
}
but result :
/p9KiDT+1wBJzJYFqejXKIsAFD0GxqKUhbjW2Pjl4JVvN13VMoLw5SC1idO9JGYn09RywdHXXEE5FCAwfKxsOA==
this result is incorrect and doesn’t work for base server.
please help me to resolve c# code issue.
New contributor
vahid wss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2