FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour encrypt/decrypt in Harbour and php
Posts: 838
Joined: Wed Aug 22, 2007 10:09 AM
encrypt/decrypt in Harbour and php
Posted: Fri Dec 20, 2019 06:21 PM

Hi,

Are there a set of functions that, for example, I encrypt the string in the php web page, and I read and decrypt in Habour -prg code?

Thank you

Saludos / Regards,



FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
Posts: 1816
Joined: Wed Oct 26, 2005 02:49 PM
Re: encrypt/decrypt in Harbour and php
Posted: Sat Dec 21, 2019 12:56 PM
Pues no es exactamente encriptar... pero para ocultar un poco el contenido puedes usar la codificaci贸n en base64.

Encode y decode en xharbour
Code (fw): Select all Collapse
cBas64 := hb_base64encode(::user+":"+::pass,len(::user+":"+::pass))聽
cString := hb_base64decode(::respuesta["qrdata"])


en PHP
Code (fw): Select all Collapse
$str = 'This is an encoded string';
echo base64_encode($str);

<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>


En este momento me encuentro investigando y avanzando de a poco sobre el tema de encriptar con los comandos openssl apenas tenga resultados comento.
Saludos
LEANDRO AREVALO
Bogot谩 (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 25.01 ] [ xHarbour 64 bits) ]
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: encrypt/decrypt in Harbour and php
Posted: Sat Dec 21, 2019 05:11 PM
leandro wrote:
En este momento me encuentro investigando y avanzando de a poco sobre el tema de encriptar con los comandos openssl apenas tenga resultados comento.


Leandro, What openssl functions are you using?

Moises, use functions hb_base64Encode and hb_base64Decode
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 1816
Joined: Wed Oct 26, 2005 02:49 PM
Re: encrypt/decrypt in Harbour and php
Posted: Sat Dec 21, 2019 05:28 PM
Cristobal como siempre gracias por responder, en este momento me encuentro leyendo sobre el tema de openssl, es nuevo para mi. Pero a lo que quiero llegar, es a traducir este codigo de php a xharbour "si se puede".

Code (fw): Select all Collapse
//Configuraci贸n del algoritmo de encriptaci贸n
//Debes cambiar esta cadena, debe ser larga y unica
//nadie mas debe conocerla
$clave  = 'Una cadena, muy, muy larga para mejorar la encriptacion';
//Metodo de encriptaci贸n
$method = 'aes-256-cbc';
// Puedes generar una diferente usando la funcion $getIV()
$iv = base64_decode("C9fBxl1EWtYTL1/M8jfstw==");
聽/*
聽Encripta el contenido de la variable, enviada como par谩metro.
聽 */
聽$encriptar = function ($valor) use ($method, $clave, $iv) {
聽 聽 聽return openssl_encrypt ($valor, $method, $clave, false, $iv);
聽};
聽/*
聽Desencripta el texto recibido
聽*/
聽$desencriptar = function ($valor) use ($method, $clave, $iv) {
聽 聽 聽$encrypted_data = base64_decode($valor);
聽 聽 聽return openssl_decrypt($valor, $method, $clave, false, $iv);
聽};
聽/*
聽Genera un valor para IV
聽*/
聽$getIV = function () use ($method) {
聽 聽 聽return base64_encode(openssl_random_pseudo_bytes(openssl_cipher_iv_length($method)));
聽};
聽?>
聽
聽
聽<?php
聽
/**
聽* function to encrypt
聽* @param string $data
聽* @param string $key
聽*/
function encrypt($data,$key)
{
聽 聽 $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
聽 聽 $encrypted=openssl_encrypt($data, "aes-256-cbc", $key, 0, $iv);
聽 聽 // return the encrypted string with $iv joined 
聽 聽 return base64_encode($encrypted."::".$iv);
}
聽
/**
聽* function to decrypt
聽* @param string $data
聽* @param string $key
聽*/
function decrypt($data,$key)
{
聽 聽 list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
聽 聽 return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
}
聽
聽
$key="1235@";
$string="la casa azul";
聽
$encryptado=encrypt($string,$key);
echo $encryptado;
echo "<hr>";
echo decrypt($encryptado,$key);
Saludos
LEANDRO AREVALO
Bogot谩 (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 25.01 ] [ xHarbour 64 bits) ]
Posts: 838
Joined: Wed Aug 22, 2007 10:09 AM
Re: encrypt/decrypt in Harbour and php
Posted: Sat Dec 21, 2019 07:57 PM

Muchas gracias.

驴No existe un m茅todo de encriptacion m谩s fuerte?

Saludos / Regards,



FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: encrypt/decrypt in Harbour and php
Posted: Sat Dec 21, 2019 09:48 PM
MOISES wrote:Muchas gracias.

驴No existe un m茅todo de encriptacion m谩s fuerte?


Look for the hb_Crypt and hb_Decrypt functions, and you can even support yourself for using these functions in the cMimeEnc and cMimeDec functions

Pd: in this forum, please use the English language. Thank you


Busca las funciones hb_Crypt y hb_Decrypt, e incluso puedes apoyarte para el uso de estas funciones en las funciones cMimeEnc y cMimeDec

Pd: en este forum, por favor, usad el idioma ingl茅s. Gracias
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 838
Joined: Wed Aug 22, 2007 10:09 AM
Re: encrypt/decrypt in Harbour and php
Posted: Sun Dec 22, 2019 09:47 AM

Thank you Crist贸bal.

Do you refer to this?

https://github.com/vszakats/hb/blob/mas ... /hbcrypt.c

Saludos / Regards,



FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: encrypt/decrypt in Harbour and php
Posted: Sun Dec 22, 2019 10:01 AM

Yes, but in version 3.2 there are also
I use this functions with cMimeEnc and cMimeDec functions

Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces

Continue the discussion