mcrypt something like –s"fjf

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

mcrypt something like –s"fjf

Post by tecktalkcm0391 »

Can anyone help me in encrypting a string that is like this: ¡“„Œ
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A key for every character? Image

What have you tried?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Code: Select all

FUNCTION ENCRYPT_DECRYPT($Str_Message) { 
    $Len_Str_Message=STRLEN($Str_Message); 
    $Str_Encrypted_Message=""; 
    FOR ($Position = 0;$Position<$Len_Str_Message;$Position++){ 
        // long code of the function to explain the algoritm 
        //this function can be tailored to modifyng the formula 
        //to calculate the key to use for every character in the string. 
        $Key_To_Use = (($Len_Str_Message+$Position)+1); // (+5 or *3 or ^2)  EDIT THIS LINE TO CHANGE THE KEY TO CHANGE WHOLE ENCRYPTION
        //after that we need a module division because can´t be greater than 255 
        $Key_To_Use = (255+$Key_To_Use) % 255; 
        $Byte_To_Be_Encrypted = SUBSTR($Str_Message, $Position, 1); 
        $Ascii_Num_Byte_To_Encrypt = ORD($Byte_To_Be_Encrypted); 
        $Xored_Byte = $Ascii_Num_Byte_To_Encrypt ^ $Key_To_Use;  //xor operation 
        $Encrypted_Byte = CHR($Xored_Byte); 
        $Str_Encrypted_Message .= $Encrypted_Byte; 
        
        //short code of  the function once explained 
        //$str_encrypted_message .= chr((ord(substr($str_message, $position, 1))) ^ ((255+(($len_str_message+$position)+1)) % 255)); 
    } 
    RETURN $Str_Encrypted_Message; 
} //end function
Also, do you know how to get the MCRYPT to work, anyone? Please help!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Sorry to say it, but a xor based encryption is hardly that.

What have you attempted to get mcrypt working so far?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

To tell you the truth, nothing. I can't seem to find any functions to do anything to help. Pack() and Unpack() are confusing me, and I don't think that bin2hex() will work either.

But this is what I am using:

Code: Select all

DEFINE ("Keysize","23r!j09$%^@#30"); // This is made by something else

function encode($data) 
{ 
        $td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, ""); 
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); 
        $encdata = mcrypt_ecb (MCRYPT_TripleDES,(Keysize), $data, MCRYPT_ENCRYPT, $iv); 
        $hextext=bin2hex($encdata); 
        return $hextext; 
} 

function decode($data) 
{ 
        $td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, ""); 
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); 
        $dectext = rtrim(mcrypt_ecb (MCRYPT_TripleDES,(Keysize), pack("H" . strlen($data), $data), MCRYPT_DECRYPT,$iv), "\0"); 
        return $dectext; 
}
feyd wrote:Sorry to say it, but a xor based encryption is hardly that.
Why?
EDIT: Read: http://forums.gamemaker.nl/lofiversion/ ... 63790.html and it says its good. Not the best, but good.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Considering the value of $Key_To_Use is a linear equation, the encryption is extremely trivial. It's not a good one at any level.

As for your attempts at mcrypt, what's not working? They look fine from a quick glance.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

tecktalkcm0391 wrote:EDIT: Read: http://forums.gamemaker.nl/lofiversion/ ... 63790.html and it says its good. Not the best, but good.
Lol, you picked the defining authority on the subject, didn't you ;)
You haven't yet learned the first lesson of the young cryptographer: "Don't make your own homebrew encryption methods, they WILL suck".

OTP is the best encryption method, provided it is implemented correctly, and both parties have other means of transporting the pads. While XOR can be used in an OTP encryption, its key feature is the one-time pad, not the XOR. What you did has nothing to do with OTP.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

feyd wrote:Considering the value of $Key_To_Use is a linear equation, the encryption is extremely trivial. It's not a good one at any level.

As for your attempts at mcrypt, what's not working? They look fine from a quick glance.
I try doing encode("adadf832!@#%@adfkasdhkdgf"); and I get the error. I am trying to get that to work.

Mordred wrote:
tecktalkcm0391 wrote:EDIT: Read: http://forums.gamemaker.nl/lofiversion/ ... 63790.html and it says its good. Not the best, but good.
Lol, you picked the defining authority on the subject, didn't you ;)
You haven't yet learned the first lesson of the young cryptographer: "Don't make your own homebrew encryption methods, they WILL suck".

OTP is the best encryption method, provided it is implemented correctly, and both parties have other means of transporting the pads. While XOR can be used in an OTP encryption, its key feature is the one-time pad, not the XOR. What you did has nothing to do with OTP.
lol. I didn't relize I read it wrong I'll have to do the OTP stuff then... :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

'll have to do the OTP stuff then...
Are you sure? OTP means your key is (at least) as long as the plaintext, sufficiently random, exchanged through a secure channel before the encrypted message is sent and only used once.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

For right now, I am going to forget about all this other stuff. Can someone just help me get this to work:

I have this:

Code: Select all

DEFINE ("Keysize","23r!j09$%^@#30"); // This is made by something else 

function encode($data) 
{ 
        $td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, ""); 
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); 
        $encdata = mcrypt_ecb (MCRYPT_TripleDES,(Keysize), $data, MCRYPT_ENCRYPT, $iv); 
        $hextext=bin2hex($encdata); 
        return $hextext; 
} 

function decode($data) 
{ 
        $td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, ""); 
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); 
        $dectext = rtrim(mcrypt_ecb (MCRYPT_TripleDES,(Keysize), pack("H" . strlen($data), $data), MCRYPT_DECRYPT,$iv), "\0"); 
        return $dectext; 
}
And I want to run this, but I get errors [Pack of % is not valid (or something like that):

Code: Select all

$input = ': ¡“„Œ
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
DEFINE ("Keysize","23r!j09$%^@#30"); // This is made by something else
srand();

function code($data, $bEncode=true) {
	$td = mcrypt_module_open('tripledes', '', 'ecb', '') or die('mcrypt_module_open failed');
	
	$key = (mcrypt_enc_get_key_size($td)<strlen(Keysize)) ? substr(Keysize, 0, mcrypt_enc_get_key_size($td)) : Keysize;
		
	$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND) or die('mcrypt_create_iv failed');
	if ( 0>mcrypt_generic_init($td, $key, $iv) ) {
		die('mcrypt_generic_init failed');
	}
		
	$retval = $bEncode ? mcrypt_generic($td, $data) : mdecrypt_generic($td, $data);
	
	mcrypt_generic_deinit($td);
	mcrypt_module_close($td);
	
	return $retval;
}

$input = '¡“„Œ
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

[quote="volka"]try

Code: Select all

<?php
DEFINE ("Keysize","23r!j09$%^@#30"); // This is made by something else
srand();

function code($data, $bEncode=true) {
	$td = mcrypt_module_open('tripledes', '', 'ecb', '') or die('mcrypt_module_open failed');
	
	$key = (mcrypt_enc_get_key_size($td)<strlen(Keysize)) ? substr(Keysize, 0, mcrypt_enc_get_key_size($td)) : Keysize;
		
	$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND) or die('mcrypt_create_iv failed');
	if ( 0>mcrypt_generic_init($td, $key, $iv) ) {
		die('mcrypt_generic_init failed');
	}
		
	$retval = $bEncode ? mcrypt_generic($td, $data) : mdecrypt_generic($td, $data);
	
	mcrypt_generic_deinit($td);
	mcrypt_module_close($td);
	
	return $retval;
}

$input = '¡“„Œ
Post Reply