Re: Beginner's Algorithm

From: Michael Amling (nospam_at_nospam.com)
Date: 10/01/03


Date: Wed, 01 Oct 2003 13:15:36 GMT

Crown&7 wrote:
> 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?

   If you want to encrypt in Java, implement any of the five AES finalists.

> 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`~");

   There's no point in constructing a "new String" of a String literal.
Just use the String literal itself.

[...]
>
> 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);

   I haven't actually looked into its algorithm, but it's unlikely that
java.util.Random is cryptographically suited to your purpose.

> 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()))));

   There's really no need to use floating point here. This would be
somewhat simpler and faster if you replaced
"(int)Math.floor(charMap.length()*rand2.nextDouble())" with
"uniform(rand2, charMap.length())", where uniform is

int uniform(Random rnd, int limit) {
    int mini=Integer.MAX_VALUE%limit, candidate;
    while ((candidate=rnd.nextInt() & Integer.MAX_VALUE)<=mini) {
    }
    return candidate%limit;
}

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

   It would be simpler and faster if you used two char variables to hold
the two previous characters, eliminating the need for the StringBuffer
variable named "data".

[...]

--Mike Amling



Relevant Pages

  • Re: Encryption ??
    ... algorithm itself, at least not in this area. ... I stated, not very clearly, that the algorithm as implemented had a potential buffer overflow problem when it was used correctly i.e. when the text was an exact multiple of 8. ... Notice now that not only don't we have buffer overflow, but it still seems like we encrypt and decrypt the entire string even though I removed the memcpy function that was supposed to copy these extra characters. ...
    (comp.lang.clipper)
  • Re: DCL encryption technique
    ... }encrypting a character string (1-30 characters) to ... }disguise string content. ... If you pass two parameters it will encrypt the first using the second ... $ EndIf ...
    (comp.os.vms)
  • Re: Generate Unique Identifier
    ... There is no "standard algorithm" to do what you want, ... string fields. ... "Jan Hyde " wrote: ... Alphanumeric Code like I mentioned which is 6 Characters wide and on ...
    (microsoft.public.vb.general.discussion)
  • Beginners Algorithm
    ... For fun I decided to whip up an encryption algorithm using Java. ... character map technique so I could choose the valid input/output characters. ... characters in the encrypted string ...
    (sci.crypt)
  • Re: Generating strings based on pattern
    ... That's an algorithm question, not a C question. ... construct a string which lists each of the ... allowed characters at that position. ... allow the next iteration of the inner loop ...
    (comp.lang.c)

Loading