decrypt.go 996 Bytes
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2010 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"crypto/aes"
9
	"crypto/cipher"
10 11 12 13 14 15
	"compress/gzip"
	"io"
	"os"
)

func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) {
16
	r, _ := os.Open(srcfile)
17
	var w io.Writer
18
	w, _ = os.Create(dstfile)
19
	c, _ := aes.NewCipher(key)
20 21
	w = cipher.StreamWriter{S: cipher.NewOFB(c, iv), W: w}
	w2, _ := gzip.NewWriter(w)
22 23 24 25 26
	io.Copy(w2, r)
	w2.Close()
}

func DecryptAndGunzip(dstfile, srcfile string, key, iv []byte) {
27
	f, _ := os.Open(srcfile)
28 29
	defer f.Close()
	c, _ := aes.NewCipher(key)
30 31 32
	r := cipher.StreamReader{S: cipher.NewOFB(c, iv), R: f}
	r2, _ := gzip.NewReader(r)
	w, _ := os.Create(dstfile)
33
	defer w.Close()
34
	io.Copy(w, r2)
35 36 37 38 39 40
}

func main() {
	EncryptAndGzip("/tmp/passwd.gz", "/etc/passwd", make([]byte, 16), make([]byte, 16))
	DecryptAndGunzip("/dev/stdout", "/tmp/passwd.gz", make([]byte, 16), make([]byte, 16))
}