mcrypt something like –s"fjf
Posted: Mon Feb 19, 2007 9:15 pm
Can anyone help me in encrypting a string that is like this: ¡“„Œ
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/

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 functionCode: 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;
}Why?feyd wrote:Sorry to say it, but a xor based encryption is hardly that.
Lol, you picked the defining authority on the subject, didn't youtecktalkcm0391 wrote:EDIT: Read: http://forums.gamemaker.nl/lofiversion/ ... 63790.html and it says its good. Not the best, but good.
I try doing encode("adadf832!@#%@adfkasdhkdgf"); and I get the error. I am trying to get that to work.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.
lol. I didn't relize I read it wrong I'll have to do the OTP stuff then...Mordred wrote:Lol, you picked the defining authority on the subject, didn't youtecktalkcm0391 wrote:EDIT: Read: http://forums.gamemaker.nl/lofiversion/ ... 63790.html and it says its good. Not the best, but good.
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.
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.'ll have to do the OTP stuff then...
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;
}Code: Select all
$input = ': ¡“„Œ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 = '¡“„Œ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 = '¡“„Œ