RE: Crypt

From: Dylan Douglas <>
Date: Mon, 11 Jun 2007 18:14:10 -0700

Okay, so, got it working.
  srand( time() + 127 );
  $key = ( mt_rand( 0, 0x7fffffff ) );
  $encrypted_data = sprintf( '%08x', $key );
  $b = 0;
  $length = strlen( $data );
  for( $i = 0; $i < $length; $i++ )
    $key = ($key * 1103515245 + 12345) & 0x7fffffff;
    $b = ( $key / 65536 ) % 256;
    $value = ord( $data[$i] ) ^ $b;
    $encrypted_data .= sprintf( '%02x', $value );
The only real change to the original code is the addition of ANDing it
with 0x7fffffff, which keeps php from busting. I used it to encrypt
your string of "I really enjoy carpeting." and get:
string(58) "5c579169848331b8c228511ab4a54303cd76f31a45d30845b2fd38e3d4"
The starting bytes of: 5c579169 are is the key. It's value is
0x5c579169 (I tried to write the LSB first but php is a pita and that
made decoding the key a pain. So, I straight-out write it like it is a
hex string and if you need to shift things around, you can do that in
See if you can get it working on your side.
Ps. here are some others:
"Fresh taste... you can trust"

"We have your email address listed as If you do
not want to receive anymore emails on special savings and news, click



        From: Dylan Douglas
        Sent: Friday, June 08, 2007 5:22 PM
        To: Stefan Kaczmarek
        Cc: Ivan Kwok; Ben Ebert; Ty Heath; Jay Mairs; Nainesh Solanki;
Sergio Alvarez; Gerald Rode
        Subject: RE: Crypt
        I think I got it sorted out.
        I changed the line to: $key = ($key * 1103515245 + 12345) &
        I was going to keep working on it, but we are changing ips and
are now in ip hell.


                From: Stefan Kaczmarek []
                Sent: Thursday, June 07, 2007 9:04 PM
                To: Dylan Douglas
                Cc: Ivan Kwok; Ben Ebert; Ty Heath; Jay Mairs; Nainesh
Solanki; Sergio Alvarez
                Subject: Re: Crypt
                Well, if you need to tweak it to make it work easier in
php, let me know. It doesn't really matter what the algo is, as long as
it's implementable in java and php.

                - Z

                On Jun 7, 2007, at 6:22 PM, Dylan Douglas wrote:

                        Have I mentioned that php is dumber about ints
than Java? Well, I'm trying to get the:
                        $key = $key * 1103515245 + 12345;
                        code to work. I start off okay, then I seem to
zoom off to around 10^130 as I become a float (and that is just doing
your carpet string). So, cast to stay an int, right? Now I loop past
MAXINT become negative and get stuck somehow at MININT value. I tried
ANDing it with 0xffffffff, but that didn't help. I have to figure out
how to tell it to limit stuff to 4 bytes and to allow it to loop. Will
keep you updated.


                                From: Stefan Kaczmarek
                                Sent: Thursday, June 07, 2007 8:46 AM
                                To: Ivan Kwok; Ben Ebert; Ty Heath; Jay
Mairs; Nainesh Solanki; Sergio Alvarez; Dylan Douglas
                                Subject: Crypt

                                Here are the code snippets that I use to
encrypt / decrypt the json string. When I send you the sources, it'll be
"files=<hex string>". And when I get the sources back, I am assuming
it'll be a <hex string> that comes back.

                                To go to and from hex strings, this is
what I do:
                                // Encrypt the data and convert it ot a
hex string
                                String data="I really enjoy carpeting.";
                                byte data_bytes[]=data.getBytes();
                                byte to_crypt[]=new
                                byte cipher[]=Crypt.Encrpyt(to_crypt);
                                String hex=Crypt.bytesToHex(cipher);

                                // Decrypt the data
                                byte plain[]=Crypt.Decrypt(data_bytes);
                                byte decrypt[]=new byte[plain.length-4];
                                data=new String(decrypt);

                                I just take the string, convert it to a
byte array, add 4 bytes at the front for the key, and then encrypt the
byte array. I then convert the whole byte array into a hex string. Then,
when receiving a hex string, i convert it to a byte array, then decrypt
it, pull off the first 4 bytes, and covert the remaining bytes back into
a string.

                                Try decrypting
933d5162fadc8f46acc8e0fdd123205867743c03ce284794f989691a5a and you
should get the "I really enjoy carpeting."

                                For funsies, here are the decrpyt and
encrypt functions:

                                // The input buffer needs to have 4
bytes at the front to allow for the key to be placed there
                                public static byte[] Encrpyt(byte[]
                                // Create a byte buffer, which is the 4
byte key plus the data
                                byte[] ret=new byte[data.length];
                                // Get a random int to encrypt with
                                int key=new Random().nextInt();
                                // Copy the key and data to the out
                                // Encrypt the data
                                return ret;
                                // Will return the buffer with the key
still at the first 4 bytes
                                public static byte[] Decrypt(byte[]
                                // Create a byte buffer
                                byte[] ret=new byte[data.length];
                                // Get the key from the first 4 bytes
                                int key=byteArrayToInt(data,0);
                                // Decrypt the data
                                return ret;
                                private static void DoCrypt(int
key,byte[] data,int offset)
                                byte b=0;
                                for(int i=offset;i<data.length;i++)
                                key = key * 1103515245 + 12345;
                                b=(byte)((key/65536) % 256);
                                data[i] ^= b;

Received on Fri Sep 14 2007 - 10:55:53 BST

This archive was generated by hypermail 2.2.0 : Sun Sep 16 2007 - 22:19:46 BST