Issue encrypting with DES in PHP

42 views Asked by At

Im trying to encrypt some information about a formulary i created in HTML and managed with a PHP Script, heres the formulary. Formulary

In php i created this

<?php
date_default_timezone_set('America/Bogota');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "TallerFB";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Error en la conexión a la base de datos: " . $conn->connect_error);
}

$fecha_hora = date('Y-m-d H:i:s');
$banco_origen = $_POST['banco_origen'];
$cuenta_origen = $_POST['cuenta_origen'];
$tipo_cuenta_origen = $_POST['tipo_cuenta_origen'];
$banco_destino = $_POST['banco_destino'];
$cuenta_destino = $_POST['cuenta_destino'];
$tipo_cuenta_destino = $_POST['tipo_cuenta_destino'];
$numero_identificacion = $_POST['numero_identificacion'];
$valor_transaccion = $_POST['valor_transaccion'];
$cus = $_POST['cus'];
$descripcion = $_POST['descripcion'];

$clave = "BlOcKcH4!n$";

$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('des-cbc'));
$cifrado_cbc = openssl_encrypt($fecha_hora . $banco_origen . $cuenta_origen . $tipo_cuenta_origen . $banco_destino . $cuenta_destino . $tipo_cuenta_destino . $numero_identificacion . $valor_transaccion . $cus . $descripcion, 'des-cbc', $clave, OPENSSL_RAW_DATA, $iv);

$cifrado_ecb = openssl_encrypt($fecha_hora . $banco_origen . $cuenta_origen . $tipo_cuenta_origen . $banco_destino . $cuenta_destino . $tipo_cuenta_destino . $numero_identificacion . $valor_transaccion . $cus . $descripcion, 'des-ecb', $clave, OPENSSL_RAW_DATA);

$iv_cfb = openssl_random_pseudo_bytes(openssl_cipher_iv_length('des-cfb'));
$cifrado_cfb = openssl_encrypt($fecha_hora . $banco_origen . $cuenta_origen . $tipo_cuenta_origen . $banco_destino . $cuenta_destino . $tipo_cuenta_destino . $numero_identificacion . $valor_transaccion . $cus . $descripcion, 'des-cfb', $clave, OPENSSL_RAW_DATA, $iv_cfb);

$iv_ofb = openssl_random_pseudo_bytes(openssl_cipher_iv_length('des-ofb'));
$cifrado_ofb = openssl_encrypt($fecha_hora . $banco_origen . $cuenta_origen . $tipo_cuenta_origen . $banco_destino . $cuenta_destino . $tipo_cuenta_destino . $numero_identificacion . $valor_transaccion . $cus . $descripcion, 'des-ofb', $clave, OPENSSL_RAW_DATA, $iv_ofb);

echo "Texto cifrado CBC: " . $cifrado_cbc . "<br>";
echo "Texto cifrado ECB: " . $cifrado_ecb . "<br>";
echo "Texto cifrado CFB: " . $cifrado_cfb . "<br>";
echo "Texto cifrado OFB: " . $cifrado_ofb . "<br><br>";

$clave = "BlOcKcH4!n$";

// Descifrado CBC
$iv_cbc = openssl_random_pseudo_bytes(openssl_cipher_iv_length('des-cbc'));
$datos_descifrados_cbc = openssl_decrypt($cifrado_cbc, 'des-cbc', $clave, OPENSSL_RAW_DATA, $iv_cbc);

// Descifrado ECB
$datos_descifrados_ecb = openssl_decrypt($cifrado_ecb, 'des-ecb', $clave, OPENSSL_RAW_DATA);

// Descifrado CFB
$iv_cfb = openssl_random_pseudo_bytes(openssl_cipher_iv_length('des-cfb'));
$datos_descifrados_cfb = openssl_decrypt($cifrado_cfb, 'des-cfb', $clave, OPENSSL_RAW_DATA, $iv_cfb);

// Descifrado OFB
$iv_ofb = openssl_random_pseudo_bytes(openssl_cipher_iv_length('des-ofb'));
$datos_descifrados_ofb = openssl_decrypt($cifrado_ofb, 'des-ofb', $clave, OPENSSL_RAW_DATA, $iv_ofb);

// Mostrar los datos descifrados
echo "Datos descifrados CBC: " . $datos_descifrados_cbc . "<br>";
echo "Datos descifrados ECB: " . $datos_descifrados_ecb . "<br>";
echo "Datos descifrados CFB: " . $datos_descifrados_cfb . "<br>";
echo "Datos descifrados OFB: " . $datos_descifrados_ofb . "<br><br>";

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "TallerFB";

$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "INSERT INTO transacciones (cifrado_cbc, cifrado_ecb, cifrado_cfb, cifrado_ofb) VALUES ('$cifrado_cbc', '$cifrado_ecb', '$cifrado_cfb', '$cifrado_ofb')";

$sql = "INSERT INTO transacciones (fecha_hora, banco_origen, cuenta_origen, tipo_cuenta_origen, banco_destino, cuenta_destino, tipo_cuenta_destino, numero_identificacion, valor_transaccion, cus, descripcion)
        VALUES ('$fecha_hora', '$banco_origen', '$cuenta_origen', '$tipo_cuenta_origen', '$banco_destino', '$cuenta_destino', '$tipo_cuenta_destino', '$numero_identificacion', '$valor_transaccion', '$cus', '$descripcion')";

if ($conn->query($sql) === TRUE) {
    echo "Transacción registrada correctamente.";
    unset($_POST);
} else {
    echo "Error al registrar la transacción: " . $conn->error;
}

$conn->close();
?>
<br>
<a href="inicio.php">Click aquí para realizar otra transacción</a>

This is supposed to encrypt and decrypt all of the information the user put in the formulary, it does, when i click "Enviar" i put this code to see if this works, apparently it works but with OFB cypher the code is just unreadable, let me show you here, it does the encryption process but idk what happen with OFB.

As you can see all of this is being saved in a database, all of the columns in the database appears to be NULL, i was guessing that it could due to the symbols used in the encryption, but once i do

SELECT * FROM transacciones it just appears to be empty, not even in PhpMyAdmin im able to see anything from cypher neither decypher.

Here the database.

I just eliminated that part of the code where it prints the cypher and decypher in the script theres nothing i can do to get the decypher from the database since it appears as NULL everytime i put anything in the formulary. Here ima show proof of what does appears with different inputs.

0

There are 0 answers