PHP and PB AES cypher

Just starting out? Need help? Post your questions and find answers here.
Liqu
User
User
Posts: 77
Joined: Sun Apr 21, 2013 10:31 am

PHP and PB AES cypher

Post by Liqu »

hi, i want to ask something

how to use this with php,

i got strange result, when on pb i can use output from pb and php, but on php i can't use output from pb

i use cypher and decypher code from this thread : http://forums.purebasic.com/english/vie ... exible+aes

pb test :

Code: Select all

key$         = "54325879851235451522748462123584";
iv$            = "2165468412123574";
message$  = "Dog, Cat, Whale, Ox, Duck, Elephant";

; PB  OUTPUT : THwv9zcRxSUzLLusMa7+Hd317JuloEGv+y8lSq3l2Uq6x30X
; PHP OUTPUT: THwv9zcRxSUzLLusMa7+HbrHfRcMM/V5whVdXrR3Sr1a+1a1UEz8INpZE4KsR5xf

Debug Cypher(message$,key$,iv$)
Debug Decypher("THwv9zcRxSUzLLusMa7+Hd317JuloEGv+y8lSq3l2Uq6x30X",key$,iv$)
Debug Decypher("THwv9zcRxSUzLLusMa7+HbrHfRcMM/V5whVdXrR3Sr1a+1a1UEz8INpZE4KsR5xf",key$,iv$)
php test :

Code: Select all

<?php

$key 	           = "54325879851235451522748462123584";
$iv               = "2165468412123574";
$message      = "Dog, Cat, Whale, Ox, Duck, Elephant";
$pboutput     = "THwv9zcRxSUzLLusMa7+Hd317JuloEGv+y8lSq3l2Uq6x30X";

$encrypted = openssl_encrypt ( $message , "aes-256-cbc" , $key, 0, $iv );
$decrypted = openssl_decrypt ( $encrypted , "aes-256-cbc" , $key, 0, $iv );
$test = openssl_decrypt ( $pboutput , "aes-256-cbc" , $key, 0, $iv );
echo($encrypted);
echo ("<br />");
echo($decrypted);
echo ("<br /> OUTPUT : ");
echo($test);
?>
and another problem is it can't work when unicode is on ( works fine on pb but not on php ).

how to make the cypher and decypher function output same with php output even the unicode is on?
please help , thank you very much :)
atomo
User
User
Posts: 65
Joined: Thu May 22, 2008 10:32 pm

Re: PHP and PB AES cypher

Post by atomo »

You can't read PB AES from PHP because PHP expect a padding, try this code :

Code: Select all

Procedure$ Cypher(string$, *key, *iv, bits)
  input_size = StringByteLength(string$)
  padding = 16 - Mod(input_size, 16)
  input_size + padding
  *input = AllocateMemory(input_size + SizeOf(Character)) : PokeS(*input, string$)
  *aes_encoded = AllocateMemory(input_size)
  If AESEncoder(*input, *aes_encoded, input_size, *key, bits, *iv)
    *base64_encoded = AllocateMemory(input_size * 2)
    If Base64Encoder(*aes_encoded, input_size, *base64_encoded, MemorySize(*base64_encoded))
      result$ = PeekS(*base64_encoded)
    EndIf
    FreeMemory(*base64_encoded)
  EndIf
  FreeMemory(*input)
  FreeMemory(*aes_encoded)
  
  ProcedureReturn result$
EndProcedure

Procedure$ Decypher(string$, *key, *iv, bits)
  input_size = StringByteLength(string$)
  *base64_decoded = AllocateMemory(input_size)
  base64_size = Base64Decoder(@string$, input_size, *base64_decoded, input_size)
  If base64_size
    *aes_decoded = AllocateMemory(base64_size + SizeOf(Character))
    If AESDecoder(*base64_decoded, *aes_decoded, base64_size, *key, bits, *iv)
      result$ = PeekS(*aes_decoded)
    EndIf
    FreeMemory(*aes_decoded)
  EndIf
  FreeMemory(*base64_decoded)
  
  ProcedureReturn result$
EndProcedure

key$ = "54325879851235451522748462123584"
iv$ = "2165468412123574"
message$ = "Dog, Cat, Whale, Ox, Duck, Elephant";

Debug Cypher(message$, @key$, @iv$, 256)
Liqu
User
User
Posts: 77
Joined: Sun Apr 21, 2013 10:31 am

Re: PHP and PB AES cypher

Post by Liqu »

with the new pboutput

Code: Select all

<?php

$key 	          = "54325879851235451522748462123584";
$iv               = "2165468412123574";
$message          = "Dog, Cat, Whale, Ox, Duck, Elephant";
$pboutput		  = "THwv9zcRxSUzLLusMa7+HbrHfRcMM/V5whVdXrR3Sr3d9eybpaBBr/svJUqt5dlK";  

$encrypted = openssl_encrypt ( $message , "aes-256-cbc" , $key, 0, $iv );
$decrypted = openssl_decrypt ( $encrypted , "aes-256-cbc" , $key, 0, $iv );
$test = openssl_decrypt ( $pboutput , "aes-256-cbc" , $key, 0, $iv );
echo($encrypted);
echo ("<br />");
echo($decrypted);
echo ("<br /> OUTPUT : ");
echo($test);
?>
i tried this, but php still can't decypher it.

i think we should make pboutput exactly like php output :

Code: Select all

THwv9zcRxSUzLLusMa7+HbrHfRcMM/V5whVdXrR3Sr1a+1a1UEz8INpZE4KsR5xf

but i have no idea haha, the weird thing is pb can decypher output from
php : THwv9zcRxSUzLLusMa7+HbrHfRcMM/V5whVdXrR3Sr1a+1a1UEz8INpZE4KsR5xf
and pb : THwv9zcRxSUzLLusMa7+Hd317JuloEGv+y8lSq3l2Uq6x30X

to : Dog, Cat, Whale, Ox, Duck, Elephant
Liqu
User
User
Posts: 77
Joined: Sun Apr 21, 2013 10:31 am

Re: PHP and PB AES cypher

Post by Liqu »

anybody know the solution?
Liqu
User
User
Posts: 77
Joined: Sun Apr 21, 2013 10:31 am

Re: PHP and PB AES cypher

Post by Liqu »

after several testing, the ouput can match if the exe is not in unicode, and using 128 bit encyption with ECB

Code: Select all

Procedure$ Cypher(string$, *key, *iv, bits)
  input_size = StringByteLength(string$)
  padding = 16 - Mod(input_size, 16)
  input_size + padding
  *input = AllocateMemory(input_size + SizeOf(Character)) : PokeS(*input, string$)
  *aes_encoded = AllocateMemory(input_size)
  If AESEncoder(*input, *aes_encoded, input_size, *key, bits, *iv,#PB_Cipher_ECB)
    *base64_encoded = AllocateMemory(input_size * 2)
    If Base64Encoder(*aes_encoded, input_size, *base64_encoded, MemorySize(*base64_encoded))
      result$ = PeekS(*base64_encoded)
      
    EndIf
    FreeMemory(*base64_encoded)
  EndIf
  FreeMemory(*input)
  FreeMemory(*aes_encoded)
 
  ProcedureReturn result$
EndProcedure

Procedure$ Decypher(string$, *key, *iv, bits)
  input_size = StringByteLength(string$)
  *base64_decoded = AllocateMemory(input_size)
  base64_size = Base64Decoder(@string$, input_size, *base64_decoded, input_size)
  If base64_size
    *aes_decoded = AllocateMemory(base64_size + SizeOf(Character))
    If AESDecoder(*base64_decoded, *aes_decoded, base64_size, *key, bits, *iv,#PB_Cipher_ECB)
      result$ = PeekS(*aes_decoded)
    EndIf
    FreeMemory(*aes_decoded)
  EndIf
  FreeMemory(*base64_decoded)
 
  ProcedureReturn result$
EndProcedure


key$ = "1234567890123456"
rstring$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."


*input = AllocateMemory(Len(rstring$)+1)
PokeS(*input,rstring$,-1,#PB_Ascii)

Cypher$ = Cypher(PeekS(*input,-1,#PB_Ascii),@key$,0,128)

;// GET BUFFER FROM PHP

;Base64E$  = "E13GvgFht8WI/eKFmcbVgtGtbRJ+gR6yJ28mHU8pNSg="
Base64E$  = Cypher$
Debug "CYPHER : "+ Cypher$
Debug "DECYPHER : "+Decypher(Base64E$, @key$, 0, 128)

PB Output :

Code: Select all

CYPHER : E13GvgFht8WI/eKFmcbVgtGtbRJ+gR6yJ28mHU8pNSg=
DECYPHER : ABCDEFGHIJKLMNOPQRSTUVWXYZ.

PHP :

Code: Select all

<?php
	header('Content-type: text/html; charset=UTF-8');
	
    $key = "1234567890123456";
	$plaintext ="ABCDEFGHIJKLMNOPQRSTUVWXYZ.";
	
	$raw = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB);
	#echo $raw . "<br>";
	$raw64 = base64_encode($raw);
	echo "CYPHER : ".$raw64 . "<br>";
	$pro64 = base64_decode($raw64);
	#echo $pro64 . "<br>";
	echo "DECYPHER : ". mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $pro64, MCRYPT_MODE_ECB). "<br>";

?>
PHP Output

Code: Select all

CYPHER : E13GvgFht8WI/eKFmcbVgtGtbRJ+gR6yJ28mHU8pNSg=
DECYPHER : ABCDEFGHIJKLMNOPQRSTUVWXYZ.
Thank you very much atomo :D

It works great on ascii, but doesn't work when on unicode, somebody know the workaround so ascii and unicode exe generate the same output or the output can still be decypher by php?
Post Reply