0

I want to encrypt and decrypt a string this way

Encryption

string ----> convert to hexadecimal ----> some maths operations on the hex to get a new hex

the decryption:

hex ----> reverse the maths operations to get the first hex -----> convert to string

is it possible to do this type of encryption decription... and How to convert the hex (octal or even base32 or base64 to the original string)?

Thank you

17
  • 1
    What are you actually trying to do? Encrypt a string using a key? Hash a password..? Commented May 21, 2012 at 15:26
  • Do you have some code written ? Commented May 21, 2012 at 15:27
  • 2
    Umm. Yes, more or less. That's what mcrypt is for. Commented May 21, 2012 at 15:28
  • Most of the time you don't need to decrypt a string, you can get away with one way encryption. In the case of a password, you encrypt it and store it. When it's entered again you encrypt the entered version and compare it with the stored version. You can do this with md5, sha1, etc. Commented May 21, 2012 at 15:30
  • Did you try using base64_encode and base64_decode? It might do what you want (I'm not sure if you want it to be safe, if you do, then don't use this alone) Commented May 21, 2012 at 15:31

1 Answer 1

1

Just spitting code

To byte array;

$text = 'blub';
$out = new Array();

for($i = 0; i < strlen($text); i++)
{
    $out[$i] = ord(substr($text, $i, 1));
}

// Do what you want to your bytes here :D

And for reversing

// Do stuff reversed to your encoding

$out = new Array();
$text = '';

for($i = 0; i < count($out); i++)
{
    $text = $text . chr($out[$i]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you NoHDD, as this solution is gonna be useful, Best Answer
BTW, for future reference, this is more akin to obfuscation than it is encryption.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.