Created
January 19, 2012 15:03
-
-
Save KarlHerler/1640461 to your computer and use it in GitHub Desktop.
Files used to attack assignment 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wget http://users.abo.fi/ipetre/crypto/encrypt3.dat | |
scala GenCaesar encrypt3.dat | |
file decrypted/* >fileoutput.txt | |
scala parseFile fileoutput.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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