kunt

golang IRC bot
git clone git://git.2f30.org/kunt
Log | Files | Refs | LICENSE

bdec.go (1081B)


      1 package main
      2 
      3 import (
      4 	"bufio"
      5 	"code.google.com/p/go.crypto/blowfish"
      6 	"encoding/gob"
      7 	"flag"
      8 	"fmt"
      9 	"log"
     10 	"os"
     11 )
     12 
     13 func usage() {
     14 	fmt.Fprintf(os.Stderr, "usage: %s <pass> <encrypted-file>\n",
     15 		os.Args[0])
     16 	os.Exit(2)
     17 }
     18 
     19 func decryptBuf(c *blowfish.Cipher, a []byte) []byte {
     20 	pt := make([]byte, len(a))
     21 	for i := 0; i < len(a); i += blowfish.BlockSize {
     22 		c.Decrypt(pt[i:i+blowfish.BlockSize],
     23 			a[i:i+blowfish.BlockSize])
     24 	}
     25 	return pt
     26 }
     27 
     28 type hdr struct {
     29 	Magic string
     30 	Len   int
     31 	Data  []byte
     32 }
     33 
     34 func main() {
     35 	log.SetPrefix("bdec: ")
     36 	flag.Usage = usage
     37 	flag.Parse()
     38 
     39 	args := flag.Args()
     40 	if len(args) < 2 {
     41 		usage()
     42 	}
     43 
     44 	c, err := blowfish.NewCipher([]byte(args[0]))
     45 	if err != nil {
     46 		log.Fatal(err)
     47 	}
     48 
     49 	fi, err := os.Open(args[1])
     50 	if err != nil {
     51 		log.Fatal(err)
     52 	}
     53 	defer fi.Close()
     54 
     55 	r := bufio.NewReader(fi)
     56 	g := gob.NewDecoder(r)
     57 
     58 	h := new(hdr)
     59 	err = g.Decode(h)
     60 	if err != nil {
     61 		log.Fatal(err)
     62 	}
     63 
     64 	if h.Magic != "BENC" {
     65 		log.Fatal(fmt.Errorf("Invalid magic: %s", h.Magic).Error())
     66 	}
     67 
     68 	db := decryptBuf(c, h.Data)
     69 	fmt.Printf("%s", db[0:h.Len])
     70 }