Beginner's Algorithm

From: Crown&7 (CN7_at_hotmail.com)
Date: 10/01/03


Date: Wed, 01 Oct 2003 05:09:39 GMT

For fun I decided to whip up an encryption algorithm using Java. I know
nothing about cryptogrophy and I'm sure it shows, so please go easy on me.
I'm looking for feedback on the originality and strength of the algorithm
(considering if it had a 'real' random number generator) and, if anyone has
time, suggestions on how to improve it. The algorithm is restricted to
outputting only plain text characters that's why I decided to use a
character map technique so I could choose the valid input/output characters.

The algorithm:
    Randomly generate two characters and place them at the front of the
string to be encrypted
    Use a seed value to start a random number generator
    Do a typical character offset mapping using random numbers and character
map,
    but with a little twist. Use the numeric values of previous two
characters in the encrypted string
    to add to the random number used to do the mapping of the current
character.

Adding the previous two characters into the mapping algorithm definitely
helped it generate a more random sequence of characters when given the same
input, but does it really add to the strength?

Here is the code:

import java.util.Random;

public class myEncrypt
{

   private static final String myCharMap = new String(
      "P>H2Fe8X@KlfZ&RiWkUmp%qrou.x,zCwnD)EGyacIL{" +
      "M6dvO_Qg?hSA;bT+V[jY134B5t7<0!#$^*(s-J}9]:N`~");

public static void main(String[] args)
{
   String data = null;
   String dataEncrypted = null;
   long dataSeed = 0x312695168964L;

   if (args.length != 1)
      usageExit();

   data = args[0];

   try
   {
      dataEncrypted = encrypt(myCharMap, data, dataSeed);

      System.out.println("Encrypted string: "+dataEncrypted);
   }
   catch(Exception e)
   {
      System.err.println(e.getMessage());
   }
}

public static void usageExit()
{
   System.out.println("");
   System.out.println("Usage: java myEncrypt <string>");
   System.out.println("");
   System.out.println(" <string> plain text string to encrypt");

   System.exit(1);
}

public static String encrypt (String charMap,
                              String dataIn,
                              long seed) throws Exception
{
   StringBuffer result = new StringBuffer(dataIn.length()+2);
   StringBuffer data = new StringBuffer(dataIn.length()+2);
   Random rand = new Random(seed);
   Random rand2 = new
Random(Math.abs(System.currentTimeMillis()-seed));
   int offset = 0;
   int location;
   int lenMap;

data.append(charMap.charAt((int)Math.floor((charMap.length()*rand2.nextDoubl
e()))));

data.append(charMap.charAt((int)Math.floor((charMap.length()*rand2.nextDoubl
e()))));
   data.append(dataIn);

   lenMap = charMap.length();

   for (int index = 0; index < data.length(); index++)
   {
      location = charMap.indexOf(data.charAt(index));

      if (location < 0)
         throw new Exception("Encryption error: '" +
                             data.charAt(index)+"' character is not
allowed");

      offset = (int) (rand.nextDouble() * lenMap);

      if (index > 0)
         offset += (int) result.charAt(index-1);

      if (index > 1)
         offset += (int) result.charAt(index-2);

      location = (offset + location) % lenMap;

      try
      {
         result.append(charMap.charAt(location));
      }
      catch(Exception e)
      {
         // The should only occur if there is a programming error above
         throw new Exception("Encryption error.");
      }
   }

   return result.toString();
}

public static String decrypt (String charMap,
                              String data,
                              long seed) throws Exception
{
   StringBuffer result = new StringBuffer(data.length());
   Random rand = new Random(seed);
   int offset = 0;
   int location;
   int lenMap;

   lenMap = charMap.length();

   for (int index = 0; index < data.length(); index++)
   {
      location = charMap.indexOf(data.charAt(index));

      if (location < 0)
      {
         // Means the encrytped char is missing in the character map
         // which can be caused if the character map for encrypting was
         // different than the one for decrypting
         throw new Exception("Illegal character in ecrypted string.");
      }

      offset = (int) (rand.nextDouble() * lenMap);

      if (index > 0)

         offset += (int) data.charAt(index-1);

      if (index > 1)
         offset += (int) data.charAt(index-2);

      location = (location - offset) % lenMap;

      if (location < 0)
         location = lenMap + (location % lenMap);

      try
      {
         result.append(charMap.charAt(location));
      }
      catch(Exception e)
      {
         // Means the location of the unencrypted character is out of bounds
of
         // the character map which can be caused if the character map for
         // encrypting was different than the one for decrypting
         throw new Exception("Invalid encrypted string.");
      }
   }

   return result.substring(2);
}
}


Quantcast