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

Smootk
  • 7
  • 5
  • 1
    What are you actually trying to do? Encrypt a string using a key? Hash a password..? – Brendan Long May 21 '12 at 15:26
  • Do you have some code written ? – ilanco May 21 '12 at 15:27
  • 2
    Umm. Yes, more or less. That's what [mcrypt](http://www.php.net/manual/en/intro.mcrypt.php) is for. – Quentin May 21 '12 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. – Ian Atkin May 21 '12 at 15:30
  • Did you try using [base64_encode](http://tr.php.net/base64_encode) and [base64_decode](http://tr.php.net/manual/en/function.base64-decode.php)? 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) – jadkik94 May 21 '12 at 15:31
  • possible duplicate of [Best way to use PHP to encrypt and decrypt?](http://stackoverflow.com/questions/1289061/best-way-to-use-php-to-encrypt-and-decrypt) – John Conde May 21 '12 at 15:32
  • By the way - **do not attempt to do encryption yourself, you have 99.9% chances of doing it wrong**. There are plenty of libraries, well known, written by people who know their stuff... Use these (pretty much what @Quentin mentions :D). – Romain May 21 '12 at 15:34
  • @brendan , I'm trying to encypt js , css files names , and send those new strings to html, so they can be used by ajax to retrieve those files when needed. – Smootk May 21 '12 at 15:41
  • @ilanco , Actually I have a code, but it's a mere essay using base_convert ... but I deleted it. – Smootk May 21 '12 at 15:43
  • @Ian, I want to encrypt a file name, so that I can get it using my own reverse-operation. I want to make the encryption function only known by myself. I don't want to use an already existing method. – Smootk May 21 '12 at 15:44
  • @Jadkik, I used base_convert, But the fact that it encrypts the string to a hexadecemal... the encryption of that hexadecemal returns a number instead of the first string. This is my question of course – Smootk May 21 '12 at 15:46
  • @John , thanks , I will try figure that out :))) – Smootk May 21 '12 at 15:47
  • @Romain, Actually, it's not a problem if a IT Geek has overcome the code and decrypted my stuff, it's not really a serious problem..because all I want is to send decrypted filenames to the browser and decrypt them using ajax, instead of displaying them in the source code – Smootk May 21 '12 at 15:49
  • @Smootk I guess you got your answer, but you should know of course, that this is useless: anyone who really cares about what the urls are should know how to find them :D – jadkik94 May 21 '12 at 15:54
  • @Smootk There is an OpenSSL port in JavaScript that you could use to do proper encryption. However I fail to see what protection against whatever this provides you with, i.e. I agree with jadkik94 – Romain May 22 '12 at 07:38
  • @jadkik94, But this is still a good solution against websites crowling, I want to encrypt user infos, and also files names, and reload them using ajax... – Smootk May 23 '12 at 12:47
  • @Smootk True. It would complicate things, but again, not impossible ;) – jadkik94 May 23 '12 at 14:15

1 Answers1

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]);
}