Re: Crypt

From: Stefan Kaczmarek <stefan_at_thezonie.org>
Date: Thu, 7 Jun 2007 21:04:29 -0700

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:

> Z-
>
> 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.
>
> -D
>
> From: Stefan Kaczmarek [mailto:stefan_at_thezonie.org]
> 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
>
> D,
>
> 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];
> System.arraycopy(data_bytes,0,to_crypt,4,data_bytes.length);
> byte cipher[]=Crypt.Encrpyt(to_crypt);
> String hex=Crypt.bytesToHex(cipher);
> data=hex;
>
> // Decrypt the data
> data_bytes=Crypt.hexToBytes(data);
> byte plain[]=Crypt.Decrypt(data_bytes);
> byte decrypt[]=new byte[plain.length-4];
> System.arraycopy(plain,4,decrypt,0,decrypt.length);
> 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];
> System.arraycopy(data,0,ret,0,data.length);
>
> // Get a random int to encrypt with
> int key=new Random().nextInt();
>
> // Copy the key and data to the out array
> System.arraycopy(intToByteArray(key),0,ret,0,4);
>
> // Encrypt the data
> DoCrypt(key,ret,4);
>
> return ret;
> }
>
> // Will return the buffer with the key still at the first 4 bytes
> public static byte[] Decrypt(byte[] data)
> {
> // Create a byte buffer
> byte[] ret=new byte[data.length];
> System.arraycopy(data,0,ret,0,data.length);
>
> // Get the key from the first 4 bytes
> int key=byteArrayToInt(data,0);
>
> // Decrypt the data
> DoCrypt(key,ret,4);
>
> 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:04 BST

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