Exemple #1
0
func SetupEncrypt(w io.Writer, key []byte, iv []byte) (io.Writer, os.Error) {
	c, e := aes.NewCipher(key)
	if e != nil {
		return nil, e
	}
	return block.NewCBCEncrypter(c, iv, w), nil
}
Exemple #2
0
func EncryptAndGzip(dstfile, srcfile string, key, iv []byte) {
	r, _ := os.Open(srcfile, os.O_RDONLY, 0)
	var w io.WriteCloser
	w, _ = os.Open(dstfile, os.O_WRONLY|os.O_CREATE, 0666)
	defer w.Close()
	w, _ = gzip.NewDeflater(w)
	defer w.Close()
	c, _ := aes.NewCipher(key)
	io.Copy(block.NewCBCEncrypter(c, iv, w), r)
}
Exemple #3
0
func Encrypt(data []byte, key *rsa.PublicKey) (iv []byte, etext []byte, ckey []byte) {
	var IVBuf [32]byte // create random 32 byte buffer for IV
	var EncodedText []byte
	rand.Read(IVBuf[0:])
	var SessionKey [32]byte
	rand.Read(IVBuf[0:]) // random 32byte session key
	var CryptedText []byte
	WriteBuf := new(bytes.Buffer)
	cipher, _ := aes.NewCipher(SessionKey[0:])
	BlockWriter := block.NewCBCEncrypter(cipher, IVBuf[0:], WriteBuf)
	BlockWriter.Write(data)
	EncryptedKey, _ := rsa.EncryptPKCS1v15(rand.Reader, key, SessionKey[0:])
	BlockWriter.Write(data)    // Pass data through Encryptor
	WriteBuf.Read(CryptedText) // Read Encrypted data from buffer to byte slice
	//n := base64.URLEncoding.EncodedLen(len(CryptedText)))
	base64.URLEncoding.Encode(EncodedText, CryptedText)

	return IVBuf[0:], EncodedText, EncryptedKey
}
Exemple #4
0
func genericDES(c block.Cipher, t *testing.T, tt cbcTest) {
	test := tt.name

	var crypt bytes.Buffer
	w := block.NewCBCEncrypter(c, tt.iv, &crypt)
	var r io.Reader = bytes.NewBuffer(tt.in)
	n, err := io.Copy(w, r)
	if n != int64(len(tt.in)) || err != nil {
		t.Errorf("%s: CBCEncrypter io.Copy = %d, %v want %d, nil", test, n, err, len(tt.in))
	} else if d := crypt.Bytes(); !same(tt.out, d) {
		t.Errorf("%s: CBCEncrypter\nhave %x\nwant %x", test, d, tt.out)
	}

	var plain bytes.Buffer
	r = block.NewCBCDecrypter(c, tt.iv, bytes.NewBuffer(tt.out))
	w = &plain
	n, err = io.Copy(w, r)
	if n != int64(len(tt.out)) || err != nil {
		t.Errorf("%s: CBCDecrypter io.Copy = %d, %v want %d, nil", test, n, err, len(tt.out))
	} else if d := plain.Bytes(); !same(tt.in, d) {
		t.Errorf("%s: CBCDecrypter\nhave %x\nwant %x", test, d, tt.in)
	}
}