当前位置:网站首页>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)
}
}
边栏推荐
- [HNOI2002]营业额统计
- 中英文说明书丨CalBioreagents ACTH N端单克隆抗体
- Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
- Getting Started with MATLAB Image Processing
- S7-200SMART PLC Modbus TCP communication
- 牛客每日刷题之链表
- Deep Learning - Principles of Neural Networks 2
- Unity 五子棋游戏设计和简单AI(2)
- IQ Products CMV Brite Turbo试剂盒的原理
- Qt learning (3) - Qt module
猜你喜欢
力扣刷题180
SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
Xilinx Zynq ZynqMP DNA
vs番茄助手的方便功能和便捷快捷键介绍
AIOT介绍
Data center project preliminary summary
51 serial communication (on)
带头双向循环链表的增删查改(C语言实现)
Used to import the data type
二硫化钼/二氧化铪的复合纳米材料(MoS2/HfO2)|钽掺杂二氧化铪纳米颗粒(齐岳bio)
随机推荐
直接用的zip包 缺少很多依赖,pip没有,感觉用anaconda create一个环境会方便点
kubernetes 安全
中英文说明书丨TRC D-阿卓糖(D-Altrose)
How to pass a two-dimensional array to a function in C language?
Chapter 7. Image-based lighting
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
Introduction to AIOT
多行字符串排序在线工具
域名批量查询在线工具
22年下高项论文题目预测
正则表达式-判断字符串是否匹配“AABB”模式
Xilinx Zynq ZynqMP DNA
phpstudy 安装 flarum论坛
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
【R语言】对文件进行归一化整理到各文件类型文件夹
力扣刷题180
Regular Expression - Determine if a string matches the "AABB" pattern
具有CT造影功能的硫化铋纳米棒|硫化铋-锌原卟啉复合材料(PAMAM/Bi2S3复合纳米粒子)
SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
Can Jincang Database Set Transaction Automatic Commit?