Just a question. Do you think it would be wise using this code to change user-input afterward hashing it with either the MD5, SHA1, or SHA-2 hashing algorithms? Or would that compromise some of the data?

EDIT: Yes, I know that MD5 and I believe SHA1 and SHA-2 are one-way hashing/encryption algorithms.

    I don't think there's any point to using this code at all (see this thread; posts 18 and 23, and the latter parts of 12 and 14).

      a year later

      Could somebody please rewrite the code below, not using the single pipe operator? (taken from post #14):

      // {en|de}cryption algorithm
      $text = (($text ^ $k) & $lomask) | ($text & $himask);

      Thank you so much.

        Why do you want to avoid the use of bitwise or when it is intended?

          🙂

          if ( (condition 1) || (condition 2) || (condition 3) ) {
          do smt...
          }

          This is the common way I am familiar with. I have never ever known about the single pipe operator ; - especially, about that (advanced-perhaps) way of coding php. So I don't understand what $text = (($text ^ $k) & $lomask) | ($text & $himask); means.

            Thanks for the link.

            "especially, about that (advanced-perhaps) way of coding php" ; This is what I mean:

            --> Can $text = (($text ^ $k) & $lomask) | ($text & $himask); be written in another form, just like:

            $a ++ can be written in another way (suitable for newbie): $a = $a + 1

            (this is not about the single pipe anymore).

              Take ($text & $himask) for instance. The & is the bitwise "and", which sets a bit to 1 if the corresponding bits in both $text and $himask are 1. To write it in some other way would likely be some cumbersome loop, checking each bit against in one value against the corresponding bit in the other.

              <pre><?php
              $foo = 7;
              $bar = 14;
              
              // using the bitwise operator:
              $result = $foo & $bar;
              var_dump($result);
              
              // a much more cumbersome method:
              $fooBits = sprintf("%032b", $foo); // convert integer to binary string
              $barBits = sprintf("%032b", $bar); // ditto
              $result = '';
              for($i=0; $i<32; $i++)
              {
                 if (($fooBits[$i] == 1) && ($barBits[$i] == 1))
                 {
                    $result .= 1;
                 }
                 else
                 {
                    $result .= 0;
                 }
              }
              $result = bindec($result); // convert binary string to integer
              var_dump($result);
              ?></pre>
              
                a month later

                Hello ALL!
                I am a new in PHP and I read all these posts. They are really amazing for students like me.
                Don't mind but I have a question, I know its a silly one but I am sure my teacher is going to ask me the very first question that which encryption technique have you applied in your code?
                Please do tell !

                  Hello ALL!
                  I am a new in PHP and I read all these posts. They are really amazing for students like me.
                  Don't mind but I have a question, I know its a silly one but I am sure my teacher is going to ask me the very first question that which encryption technique have you applied in your code?
                  Please do tell !
                  sonam is online now Report Post Reply With Quote

                    6 months later

                    I liked this code for obscuring data until I managed to break it today

                    xx_1st4domains.co.uk_test
                    pq[4g|=wlbelfzf{&|x{av|
                    xx_1st4domains.co.uk_Sd~u
                    pq[4g|=wlbelfz
                    f{&|x\{av|
                    xx_1st4domains.co.uk_SXYshry
                    pq[4g|=wlbelfz*f{&|x\\{av|

                    It's that second underscore that is creating extra backslashes and throwing it out of line.
                    Changing characters before or after the underscore provides similar results as long as the length stays the same.

                    Any ideas?

                      ppcnseo wrote:

                      Any ideas?

                      Yes; don't use it.

                        10 months later

                        Simple String EnCrypt + DeCrypt function.

                        ...

                        <?php
                        // String EnCrypt + DeCrypt function
                        // Author: halojoy, July 2006
                        function convert($str,$ky=''){
                        if($ky=='')return $str;
                        $ky=str_replace(chr(32),'',$ky);
                        if(strlen($ky)<8)exit('key error');
                        $kl=strlen($ky)<32?strlen($ky):32;
                        $k=array();for($i=0;$i<$kl;$i++){
                        $k[$i]=ord($ky{$i})&0x1F;}
                        $j=0;for($i=0;$i<strlen($str);$i++){
                        $e=ord($str{$i});
                        $str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);
                        $j++;$j=$j==$kl?0:$j;}
                        return $str;
                        }
                        

                        [/QUOTE]

                        i remake this code for javascript client :

                        function convert(text,key){
                            if (key === '') {
                                return text; 
                            } 
                            key = key.replace(' ', ''); 
                            if (key.length < 8) { 
                                alert('key error'); 
                                return false;
                            } 
                            key_len = key.length; 
                            if (key_len > 32) { 
                                key_len = 32; 
                            } 
                            k = []; // key array 
                            for (i = 0; i < key_len; ++i) { 
                                k[i] = key.charCodeAt(i) & 0x1F;
                            } 
                            for (i = 0, j = 0; i < text.length; ++i) { 
                                e = text.charCodeAt(i); 
                                if (e + 0xE0) { 
                                    text = text.substr(0,i) + String.fromCharCode(e ^ k[j]) + text.substr(i+1);  
                        } j = (j + 1) % key_len; } return text; }

                        or

                        function convertshortverson(a,b){if(b===""){return a}b=b.replace(" ","");if(b.length<8){alert("key error");return false}key_len=b.length;if(key_len>32){key_len=32}k=[];for(i=0;i<key_len;++i){k[i]=b.charCodeAt(i)&31}for(i=0,j=0;i<a.length;++i){e=a.charCodeAt(i);if(e+224){a=a.substr(0,i)+String.fromCharCode(e^k[j])+a.substr(i+1)}j=(j+1)%key_len}return a}
                          9 years later

                          Hi Halojay,

                          I have been using your encryption for a few years and loved it - Brilliant. It is good in that most people, non-encryption geeks, don't know how to decrypt the data. All these years I had been using PHP 5 but as of yesterday after migrating my website to another host, PHP 7.4 gives me an error. ""Deprecated: Array and string offset access syntax with curly braces is deprecated in ...". My understanding was that PHP 7.4 was complaining about $ky{$i})&0x1F;, where the {} are used. I have never seen this before (except for function definitions) I tried to search for the meaning but could not find anything on the internet.

                          This was the line PHP 7.4 was complaining about :-
                          $k[$i]=ord($ky{$i})&0x1F;

                          Is it as simple as replacing the {} with [] ?
                          Is there any way to rewrite the code so that it is compatible with PHP 7.4?

                          Thanks for your help.

                            Write a Reply...