golang zip aes base64

1、zip

package main

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"fmt"
)

func main() {
	var b bytes.Buffer
	w := gzip.NewWriter(&b)
	defer w.Close()

	src := "hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world"
	w.Write([]byte(src))
	w.Write([]byte(src))
	w.Write([]byte(src))
	w.Write([]byte(src))
	w.Flush()
	fmt.Printf("size1:%d  size2:%d  size3:%d  \n", len([]byte(src)), len(b.Bytes()), b.Len())
	sEnc := base64.StdEncoding.EncodeToString(b.Bytes())
	fmt.Printf("enc=[%s]\n", sEnc)
}

2、aes

package main

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"fmt"
)

func padding(src []byte, blocksize int) []byte {
	padnum := blocksize - len(src)%blocksize
	pad := bytes.Repeat([]byte{byte(padnum)}, padnum)
	return append(src, pad...)
}

func unpadding(src []byte) []byte {
	n := len(src)
	unpadnum := int(src[n-1])
	return src[:n-unpadnum]
}

func encryptAES(src []byte, key []byte) []byte {
	block, _ := aes.NewCipher(key)
	src = padding(src, block.BlockSize())
	blockmode := cipher.NewCBCEncrypter(block, key)
	dst := make([]byte, len(src))
	blockmode.CryptBlocks(dst, src)
	return dst
}

func decryptAES(src []byte, key []byte) []byte {
	block, _ := aes.NewCipher(key)
	blockmode := cipher.NewCBCDecrypter(block, key)
	blockmode.CryptBlocks(src, src)
	src = unpadding(src)
	return src
}

func main() {
	x := []byte("中华人民共和国万岁           世界人民大团结万岁")
	key := []byte("hgfedcba87654321") //16*8=128位,所以是AES128
	x1 := encryptAES(x, key)
	x2 := decryptAES(x1, key)
	fmt.Print(string(x2))
}

3、base64

package main

import (
	"encoding/base64"
	"fmt"
	"os"
	"reflect"
	"strings"
)
func main4() {
	s := "Hello World!"
	b := []byte(s)

	sEnc := base64.StdEncoding.EncodeToString(b)
	fmt.Printf("enc=[%s]\n", sEnc)

	sDec, err := base64.StdEncoding.DecodeString(sEnc)
	if err != nil {
		fmt.Printf("base64 decode failure, error=[%v]\n", err)
	} else {
		fmt.Printf("dec=[%s]\n", sDec)
	}
}
func main3(){
	src := []byte("this is a test string.")
	encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
	encoder.Write(src)
	encoder.Close()
}
func main2(){
	src := []byte("this is a test string.")
	encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
	encoder.Write(src)
	encoder.Close()
}
func main1() {
	src := "dGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg=="
	reader := strings.NewReader(src)
	decoder := base64.NewDecoder(base64.StdEncoding, reader)

	buf := make([]byte, 10)
	fmt.Printf("%T-----%T=======%s\n", buf, decoder, reflect.TypeOf(buf))
	dst := ""
	for{
		n,err := decoder.Read(buf)
		if n==0{
			println("n=0")
			break
		}
		if err != nil{
			println("err != nil")
			break
		}
		dst += string(buf[:n])
		println(dst)
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gocryptfs 是一个使用 Go 开发的,加密的覆盖文件系统。gocryptfs 基于 go-fuse FUSE 库和其 LoopbackFileSystem API 构建。gocryptfs 灵感来源于 EncFS,修复了其安全问题,提供更好的性能。gocryptfs 当前只支持 Linux 系统,以后会继续支持 OS X。安装:$ go get github.com/rfjakob/gocryptfs使用:$ mkdir cipher plain $ $GOPATH/bin/gocryptfs --init cipher   [...] $ $GOPATH/bin/gocryptfs cipher plain   [...] $ echo test > plain/test.txt $ ls -l cipher   total 8   -rw-rw-r--. 1 user  user   33  7. Okt 23:23 0ao8Hyyf1A-A88sfNvkUxA==   -rw-rw-r--. 1 user  user  233  7. Okt 23:23 gocryptfs.conf $ fusermount -u plain性能:./benchmark.bash gocryptfs v0.3.1-30-gd69e0df-dirty; on-disk format 2 PASS BenchmarkStreamWrite-2       100      12246070 ns/op      85.63 MB/s BenchmarkStreamRead-2        200       9125990 ns/op     114.90 MB/s BenchmarkCreate0B-2        10000        101284 ns/op BenchmarkCreate1B-2        10000        178356 ns/op       0.01 MB/s BenchmarkCreate100B-2       5000        361014 ns/op       0.28 MB/s BenchmarkCreate4kB-2        5000        375035 ns/op      10.92 MB/s BenchmarkCreate10kB-2       3000        491071 ns/op      20.85 MB/s ok      github.com/rfjakob/gocryptfs/integration_tests  17.216s 标签:文件加密
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值