当前位置:网站首页>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)
}
}
边栏推荐
- sql problem solving statement to create a table
- 【R语言】交互作用 测试数据
- uniapp实现防抖搜索
- The difference between LDO and DC-DC
- [R language] interaction test data
- pdf加密、找回密码
- SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
- [MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
- kubernetes apparmor 简介
- 记一个 nest.js 路由匹配后面所有路径问题
猜你喜欢
PDF不能打印和复制的问题如何解决?
Xilinx Zynq ZynqMP DNA
GNNExplainer applied to node classification task
一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
Chapter 3 Search and Graph Theory (1)
5年测试开发工程师感悟——写给还在迷茫中的朋友
超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
sqlserver导入数据类型问题
How to automatically fill down an excel table without dragging the mouse down
随机推荐
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
How to pass a two-dimensional array to a function in C language?
static静态关键字和继承
Kubernetes apparmor profile
VScode安装了ESlint以后不管用、不生效的解决办法
中英文说明书丨TRC D-阿卓糖(D-Altrose)
Chapter 3 Search and Graph Theory (1)
Getting Started with MATLAB Image Processing
MYSQLg高级------批量插入百万级数据量
【R语言】对文件进行归一化整理到各文件类型文件夹
[Deep Learning] Let's talk about what is convolutional neural network and what does convolution mean?
sql problem solving statement to create a table
el-table缓存数据
【R语言】把文件夹下的所有文件提取到特定文件夹
具有CT造影功能的硫化铋纳米棒|硫化铋-锌原卟啉复合材料(PAMAM/Bi2S3复合纳米粒子)
Domain name batch query online tool
著名数字藏品专家、数藏大会创始人高泽龙接受中国企业家杂志采访
Invalid argument(s) appears when redis runs lua script
Selection of MOS tube
RT201 Domestic PA RF Power Amplifier Compatible with RFX2401C