当前位置:网站首页>golang zip aes base64
golang zip aes base64
2022-08-09 06:21:00 【ggaofeng】
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)
}
}
边栏推荐
- [R language] Normalize and organize files into folders of various file types
- static静态关键字和继承
- How to automatically fill down an excel table without dragging the mouse down
- 【R语言】对文件进行归一化整理到各文件类型文件夹
- Qt 学习(三) —— Qt 模块
- SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
- Invalid argument(s) appears when redis runs lua script
- Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
- 5年测试开发工程师感悟——写给还在迷茫中的朋友
- shell regular expressions
猜你喜欢
Unity 五子棋游戏设计和简单AI(2)
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
手把手教你用C语言制作七夕流星雨---优雅永不过时(详细教程)
shell regular expressions
qt发送邮件程序
sql problem solving statement to create a table
RT201 Domestic PA RF Power Amplifier Compatible with RFX2401C
GNNExplainer applied to node classification task
- [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop"/>
Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
Xilinx Zynq ZynqMP DNA
随机推荐
tidb crash test
excel表格如何不需鼠标往下拖动而自动往下填
Bismuth sulfide nanorods with CT imaging function | Bismuth sulfide-zinc protoporphyrin composites (PAMAM/Bi2S3 composite nanoparticles)
sql问题解答创建表的语句
RNN-T
static静态关键字和继承
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
数据中台项目前期总结
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
A day to learn a public company: Sophia
【R语言】把文件夹下的所有文件提取到特定文件夹
【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
Used to import the data type
sqlserver导入数据类型问题
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
AIOT介绍
sql problem solving statement to create a table
Unity 五子棋游戏设计和简单AI(2)
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用