Skip to content

Instantly share code, notes, and snippets.

@KarlHerler
Created January 19, 2012 15:03
Show Gist options
  • Save KarlHerler/1640461 to your computer and use it in GitHub Desktop.
Save KarlHerler/1640461 to your computer and use it in GitHub Desktop.
Files used to attack assignment 3
import java.lang.RuntimeException
import java.io._
import scala.io.Source
class GenCaesar(cipher: Array[Byte]) {
def brute(b: Array[Byte]): IndexedSeq[(Int, Array[Byte])] =
(0 to 255).map {
i => (i, (b.map(decrypt(_, i))))
}
//data decrypt
def decrypt = brute(cipher)
//atomary decrypt
def decrypt(b: Byte, k: Int): Byte = ((b-k.toByte)).toByte
override def toString = decrypt mkString "\n"
def writeToFiles(dir: String): Unit = {
//writeToFile("./decrypted/0.jpg", cipher)
val decrypted = decrypt
decrypted.foreach {
(tup) =>
val path = "./"+dir+"/"+tup._1+".dat"
writeToFile(path, tup._2)
}
}
def writeToFile(path: String, data: Array[Byte]): Unit = {
val file = new File(path)
file.createNewFile()
val fos = new BufferedOutputStream(new FileOutputStream(file))
fos.write(data)
fos.close()
}
}
object GenCaesar {
def main(args: Array[String]): Unit = {
for (arg <- args) {
val file = new File(arg)
val fin = new FileInputStream(file)
val bytes = new Array[Byte](file.length.toInt)
fin.read(bytes)
val c = new GenCaesar(bytes)
c.writeToFiles("decrypted")
}
}
}
wget http://users.abo.fi/ipetre/crypto/encrypt3.dat
scala GenCaesar encrypt3.dat
file decrypted/* >fileoutput.txt
scala parseFile fileoutput.txt
import scala.io.Source
class parseFile(fo: String) {
val splitted = fo split "\n"
def removeDatas = splitted.filterNot(_.endsWith("data"))
override def toString = removeDatas mkString "\n"
}
object parseFile {
def main(args: Array[String]): Unit = {
for (arg <- args) {
val sauce = Source.fromFile(arg)
val lines = sauce mkString ""
sauce.close()
val q = new parseFile(lines)
println(q)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment