Monday 29 March 2010

Symmetric-key algorithm

This is a more robust version of my character substitution(with random generation) that was used for my finshare application in the chat component. 


 !! warning character substitution is a weak security approach and should not be used in a production environment !!

Here is an example of the output:

( k <-   -> ` ) ( I <- ! -> j ) ( C <- " -> 6 ) ( l <- # -> ^ ) ( { <- $ -> O ) ( < <- % -> & ) ( % <- & -> * ) ( z <- ' -> H ) ( p <- ( -> y ) ( r <- ) -> C ) ( & <- * -> Y ) ( X <- + -> A ) ( M <- , -> / ) ( ~ <- - -> I ) ( G <- . -> V ) ( , <- / -> M ) ( y <- 0 -> 8 ) ( ` <- 1 -> } ) ( : <- 2 -> ~ ) ( n <- 3 -> D ) ( s <- 4 -> F ) ( F <- 5 -> _ ) ( " <- 6 -> U ) ( @ <- 7 -> @ ) ( 0 <- 8 -> = ) ( S <- 9 -> \ ) ( D <- : -> 2 ) ( H <- ; -> t ) ( R <- < -> % ) ( 8 <- = -> r ) ( ? <- > -> q ) ( Q <- ? -> > ) ( 7 <- @ -> 7 ) ( + <- A -> h ) ( O <- B -> R ) ( ) <- C -> " ) ( 3 <- D -> : ) ( x <- E -> m ) ( 4 <- F -> 5 ) ( ^ <- G -> . ) ( ' <- H -> ; ) ( - <- I -> ! ) ( j <- J -> d ) ( o <- K -> L ) ( K <- L -> { ) ( / <- M -> , ) ( a <- N -> g ) ( $ <- O -> B ) ( u <- P -> u ) ( t <- Q -> ? ) ( B <- R -> < ) ( b <- S -> 9 ) ( h <- T -> i ) ( 6 <- U -> | ) ( . <- V -> p ) ( _ <- W -> x ) ( [ <- X -> + ) ( * <- Y -> Z ) ( Y <- Z -> o ) ( \ <- [ -> X ) ( 9 <- \ -> [ ) ( v <- ] -> a ) ( # <- ^ -> G ) ( 5 <- _ -> W ) (   <- ` -> 1 ) ( ] <- a -> N ) ( m <- b -> S ) ( d <- c -> z ) ( J <- d -> c ) ( i <- e -> f ) ( e <- f -> w ) ( N <- g -> k ) ( A <- h -> T ) ( T <- i -> e ) ( ! <- j -> J ) ( g <- k ->   ) ( | <- l -> # ) ( E <- m -> b ) ( q <- n -> 3 ) ( Z <- o -> K ) ( V <- p -> ( ) ( > <- q -> n ) ( = <- r -> ) ) ( w <- s -> 4 ) ( ; <- t -> Q ) ( P <- u -> P ) ( } <- v -> ] ) ( f <- w -> s ) ( W <- x -> E ) ( ( <- y -> 0 ) ( c <- z -> ' ) ( L <- { -> $ ) ( U <- | -> l ) ( 1 <- } -> v ) ( 2 <- ~ -> - )


Hello World
;wG.Oa\<^8n
Hello World

So where's this magic that makes it happen?

Here we set the character range to cover 0-9,a-z,A-z and all manner of punctuation symbols


//Symmetric-key algorithm

final byte minCharVal = 32;
final byte maxCharVal = 127;
char[][] letters = new char[maxCharVal-minCharVal][2];
int[] shiftVal = new int[3];

This will randomly generate the encryption sequence to be used.


void start()
{
  size(20, 20);
  char aVal;

  for(int count = 0; count < shiftVal.length; count++)
  {//Key management
    shiftVal[count] = (int)random(10)+1;
  }
  int myCount;
  for(int count = 0; count < letters.length; count++)
  {myCount = 0;
    do
    {
      aVal = (char)(random(letters.length)+minCharVal);
      myCount++;
    }while(inArray(aVal));
    
    letters[count][0] = aVal;
  }System.out.println();
  
  for(byte countOutter = 0; countOutter < letters.length; countOutter++)
  {      
    for(byte countInner = 0; countInner < letters.length; countInner++)
    {
        if((char)(countOutter+minCharVal) == letters[countInner][0])
        {
          letters[countOutter][1] = (char)(countInner+minCharVal);
        }
    }  
  }
  
  for(int count = 0; count < letters.length; count++)
  {
    System.out.print("( "+(char)(letters[count][0])+" <- data-blogger-escaped--="" data-blogger-escaped-char="" data-blogger-escaped-count="" data-blogger-escaped-mincharval=""> "+(char)(letters[count][1])+" )\t");
  }
  System.out.println();
  
  String hi = "Hello World";
  System.out.println(hi);
  hi = cryptString(hi,true);
  System.out.println(hi);
  hi = cryptString(hi,false);
  System.out.println(hi);
}


This function maps the ascii value to the value in the range as set of at the top



char crypt(char input_char, boolean encrypt)
{
  return crypt(input_char-minCharVal, encrypt);
}


Move character forward or backwards through the sequence


char crypt(int index, boolean encrypt)
{
  if(encrypt)
  {  return (char)(letters[index][1]);  }
  else
  {  return (char)(letters[index][0]);  }
}


Find the character to substitute


boolean inArray(char input_char)
{
  for(byte count = 0; count < letters.length; count++)
  {
    if(letters[count][0] == input_char)
    { return true; }
  }
  return false;
}


The utility function to encode or decode a string


String cryptString(String inputText, boolean encrypt)
{
  char[] inputArray = inputText.toCharArray();
  char[] returnArray = new char[inputArray.length];
  
  //add(in random place) the next three shiftVals
  
  for(int count = 0; count < inputArray.length; count++)
  {
      char tempChar = inputArray[count];
      for(int countShift = 0; countShift <= count; countShift++)
      { tempChar = crypt(tempChar,encrypt); }
      returnArray[count] = tempChar;
  }
  return new String(returnArray);
}
... continue reading!