Crypt

From: Stefan Kaczmarek <stefan_at_thezonie.org>
Date: Thu, 7 Jun 2007 08:45:31 -0700

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:55:51 BST

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