Skip to content

Commit

Permalink
Adding a Demo class
Browse files Browse the repository at this point in the history
  • Loading branch information
jho committed May 3, 2012
1 parent b3a57eb commit 6c3a99f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
4 changes: 4 additions & 0 deletions demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

args="-Ddemo.log4j.propertie $@"
mvn -e -q -o compile exec:java -Dexec.mainClass="org.jho.ace.Demo" -Dexec.args="$args" -Dexec.classpathScope=runtime
10 changes: 10 additions & 0 deletions src/main/resources/demo.log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %X{url} %p %c - %m%n

log4j.category.org.jho.ace=DEBUG
log4j.category.org.jho.ace.cryptanalyzer=DEBUG

log4j.category.org.jho.ace.genetic=INFO
3 changes: 2 additions & 1 deletion src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %X{url} %p %c - %m%n
log4j.appender.stdout.layout.ConversionPattern=%m%n

log4j.category.org.jho.ace=DEBUG
log4j.category.org.jho.ace.cryptanalyzer=DEBUG

log4j.category.org.jho.ace.genetic=INFO
49 changes: 49 additions & 0 deletions src/main/scala/org/jho/ace/Demo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2012 Photobucket
*/

package org.jho.ace

import org.jho.ace.ciphers.Cipher
import org.jho.ace.cryptanalyzer.AStarCryptanalyzer
import org.jho.ace.cryptanalyzer.BFSCryptanalyzer
import org.jho.ace.cryptanalyzer.GeneticCryptanalyzer
import org.jho.ace.cryptanalyzer.SACryptanalyzer
import org.jho.ace.util.Configureable
import org.jho.ace.util._

class Demo extends Configureable {
def run(args:Map[String, String]) {
val plaintext = language.sample(200)
val keyword = args.getOrElse("keyword", "KEYWORD")

val cipher:Cipher = Class.forName("org.jho.ace.ciphers."+args.getOrElse("cipher", "Vigenere"))
.newInstance
.asInstanceOf[Cipher]

//TODO: using reflection with scala default args is not working well
//this will have to do for now
val cryptanalyzer = args.getOrElse("cryptanalyzer", "BFS") match {
case "AStar" => new AStarCryptanalyzer
case "BFS" => new BFSCryptanalyzer
case "SA" => new SACryptanalyzer
case "Genetic" => new GeneticCryptanalyzer
}

val startTime = System.currentTimeMillis
val ciphertext = cipher.encrypt(keyword, plaintext)
println("Cryptanalyzing using "+cryptanalyzer.getClass.getSimpleName+"...")
val result = cryptanalyzer.decrypt(ciphertext, cipher)

println("----------------")
println("Result: " + result.keyword)
println("Accuracy: " + (1-plaintext.distance(result.plainText)))
println("Took: " + (System.currentTimeMillis - startTime)/1000 + " seconds")
}
}

object Demo extends CommandLine {
def main(args:Array[String]) = {
new Demo().run(parseArgs(args))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import scala.math._
* A Cryptanalyzer algorithm based on the A* path finding algorithm (or Best First Search with a heuristic)
*/
class AStarCryptanalyzer(heuristic:Heuristic = Heuristic.default) extends Cryptanalyzer(heuristic) {
val classifier = new DictionarySvm
//val classifier = new DictionarySvm
//classifier.load

def decrypt(cipherText:String, cipher:Cipher):CryptanalysisResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import scala.math._
* A Cryptanalyzer algorithm based on the A* path finding algorithm (or Best First Search with a heuristic)
*/
class BFSCryptanalyzer(heuristic:Heuristic = Heuristic.default) extends Cryptanalyzer(heuristic) {
val classifier = new DictionarySvm
//val classifier = new DictionarySvm
//classifier.load

def decrypt(cipherText:String, cipher:Cipher):CryptanalysisResult = {
Expand Down Expand Up @@ -60,7 +60,7 @@ class BFSCryptanalyzer(heuristic:Heuristic = Heuristic.default) extends Cryptana
if(c < best._2) {
sinceBest = 0
best = (n, c)
logger.trace("new best:" + best)
logger.debug("new best:" + best)
logger.trace(count+" keys searched")
//logger.debug("Score: " + classifier.score(decryption))
//logger.debug("Classification: " + classifier.classify(decryption))
Expand Down
22 changes: 22 additions & 0 deletions src/main/scala/org/jho/ace/util/CommandLine.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2012 Photobucket
*/

package org.jho.ace.util

trait CommandLine {
def parseArgs(argv:Array[String]):Map[String, String] = {
//seperate options from argumetns
val (opts, args) = argv.partition{ _.startsWith("-") }

//turning options array into map
return Map() ++ opts.map{ x =>
val pair = x.split("-{1,2}")(1).split("=")
if(pair.length ==1) (pair(0), "true") else (pair(0), pair(1))
}

//use the option values
//val verbose = optsMap.getOrElse("v", "false").toBoolean
//val number = optsMap.getOrElse("n", "0").toInt
}
}

0 comments on commit 6c3a99f

Please sign in to comment.