RE: Crypt

From: Dylan Douglas <>
Date: Thu, 7 Jun 2007 18:22:38 -0700

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


        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[data_bytes.length+4];
        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[] data)
        // 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 array


        // Encrypt the data


        return ret;


        // Will return the buffer with the key still at the first 4
        public static byte[] Decrypt(byte[] data)
        // 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:56:05 BST

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