aesgzip

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/hunyxv/aesgzip"
)

func init () {
	// 修改 aes 初始化变量(16字节)
	aesgzip.IV  = []byte("90-b@de.ghi^765=")
}

func main() {
	aesKey := []byte("123abc456def7890")

	// 文件压缩加密
    plaintext, err := os.Open("input.txt")
	if err != nil {
		log.Fatalln(err)
	}
	defer plaintext.Close()
	dstPath := "output.gz.aes"

	err = aesgzip.GzipEncryption(plaintext, dstPath, aesKey)
	if err != nil {
		log.Fatalln(err)
	}

	// 解密解压缩

	ciphertext, err := os.Open("output.gz.aes")
	if err != nil {
		log.Fatalln(err)
	}
	defer ciphertext.Close()
	dstPath = "output.txt"
	
	err = aesgzip.DecryptUngzip(ciphertext, dstPath, aesKey)
	if err != nil {
		log.Fatalln(err)
	}

	// 一句话加解密
	text := []byte("How are you? I'm fine, thanks!")
	fmt.Println("aes加密前:", text)
	aesText, _ := aesgzip.Encrypt(text, aesKey)
	fmt.Println("aes加密后:", aesText)
	pText, _ := aesgzip.Decrypt(aesText, aesKey)
	fmt.Println("aes解密后:", pText)

	// 一句话加密并压缩 和 解密解压缩
	data := []byte(`{
		"version": "0.1",
		"timestamp": 1586519299,
		"param": {
			"type": 1,
			"list": [
				{"video_id": "v_19rwstgyo4", "update_time": 1586514239, "event": "Update"},
				{"video_id": "v_19rsd3g6o4", "update_time": 1586514249, "event": "Update"},
				{"video_id": "v_19rws3jjr9", "update_time": 1586514259, "event": "Update"}
			]
		}
	}`)

	xx, err := aesgzip.RowGzipEncryption(data, key)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(xx)

	xd, err := aesgzip.RowDecryptUngzip(xx, key)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(xd)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IV []byte = []byte("0123456789876543")

aes 初始化向量

Functions

func Decrypt

func Decrypt(crypted []byte, key []byte) ([]byte, error)

@title Decrypt @description aes 解密 @param crypted []byte 密文数据, key []byte aes密钥 @return []byte 明文数据, error

func DecryptUngzip

func DecryptUngzip(src io.Reader, dstPath string, aesKey []byte) error

@title DecryptUngzip @description 将 src 中的数据aes解密gzip解压缩然后写入 dstPath 路径 @param src io.Reader 提供密文数据、 @param dstPath string 解密解压缩后文件路径 @param aesKey []byte aes 密钥 @return error

func Encrypt

func Encrypt(origData []byte, key []byte) ([]byte, error)

@title Encrypt @description aes 加密 @param origData []byte 明文数据, key []byte aes密钥 @return []byte 密文, error

func GzipEncryption

func GzipEncryption(src io.Reader, dstPath string, aesKey []byte) error

@title GzipEncryption @description 将 src 中的数据gzip压缩然后aes加密写入 dstPath 路径 @param src io.Reader 提供明文数据、 @param dstPath string 压缩加密后文件路径 @param aesKey []byte aes 密钥 @return error

func PKCS5Padding

func PKCS5Padding(origData []byte, blockSize int) []byte

@title PKCS5Padding @description 填充明文 @param origData []byte 明文数据,blockSize int aes分组长度 @return []byte

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

@title PKCS5UnPadding @description 去除填充 @param origData []byte 明文数据 @return []byte

func RowDecryptUngzip added in v0.0.6

func RowDecryptUngzip(d, key []byte) ([]byte, error)

对一行数据解密解压缩为

func RowGzipEncryption added in v0.0.6

func RowGzipEncryption(d, key []byte) ([]byte, error)

对一行数据压缩加密

Types

type AesDecryptR

type AesDecryptR struct {
	// contains filtered or unexported fields
}

AesDecryptW 流式解密 io.Write --AES--> []byte

func NewAesDecryptR

func NewAesDecryptR(f io.Reader, aesKey []byte) (*AesDecryptR, error)

@title NewAesDecryptR @description 创建 *AesDecryptW @param f io.Reader 加密后数据写入 f @param aesKey []byte aes密钥 @return *NewAesEncryptW

func (*AesDecryptR) Read

func (d *AesDecryptR) Read(b []byte) (n int, err error)

@title Write @description io.Writer 接口

type AesDecryptW

type AesDecryptW struct {
	// contains filtered or unexported fields
}

AesDecryptW 流式解密 []byte --AES--> io.Writer

func NewAesDecryptW

func NewAesDecryptW(f io.WriteCloser, aesKey []byte) (*AesDecryptW, error)

@title NewAesDecryptW @description 创建 *AesDecryptW @param f io.Writer 加密后数据写入 f @param aesKey []byte aes密钥 @return *NewAesEncryptW

func (*AesDecryptW) Close

func (d *AesDecryptW) Close() error

Close 会关闭下层 io

func (*AesDecryptW) Write

func (d *AesDecryptW) Write(b []byte) (n int, err error)

@title Write @description io.Writer 接口

type AesEncrypt

type AesEncrypt struct {
	// contains filtered or unexported fields
}

AesDecryptR 流式加密 io.Reader --AES--> []byte

func NewAesEncrypt

func NewAesEncrypt(f io.Reader, aesKey []byte) (*AesEncrypt, error)

@title NewAesDecryptR @description 创建 *NewAesDecryptR @param f io.Reader 提供明文数据, aesKey []byte aes密钥 @return *NewAesDecryptR

func (*AesEncrypt) Read

func (e *AesEncrypt) Read(b []byte) (n int, err error)

Read io.Reader

type AesEncryptW

type AesEncryptW struct {
	// contains filtered or unexported fields
}

AesEncryptW 流式加密 []byte --AES--> io.Writer

func NewAesEncryptW

func NewAesEncryptW(f io.WriteCloser, aesKey []byte) (*AesEncryptW, error)

@title NewAesEncryptW @description 创建 *AesEncryptW @param f io.Writer 加密后数据写入 f @param aesKey []byte aes密钥 @return *NewAesEncryptW

func (*AesEncryptW) Close

func (e *AesEncryptW) Close() error

Close 会关闭下层 io

func (*AesEncryptW) Flush

func (e *AesEncryptW) Flush() error

Flush flush @description 在最后一轮加密后,使用此函数将buf中不足 roundSize 大小的数据加密

func (*AesEncryptW) Write

func (e *AesEncryptW) Write(b []byte) (n int, err error)

@title Write @description io.Writer 接口,从上游接收数据缓存、加密 写入下游 io.Writer @param b []byte @return n int, 成功写入的数据, err error 异常

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL