Created
May 12, 2021 03:59
-
-
Save SolemnJoker/111dcd4714d0de85ccd3908f70499437 to your computer and use it in GitHub Desktop.
[go aes加密解密] #go #aes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/aes" | |
"crypto/cipher" | |
"encoding/base64" | |
"errors" | |
"fmt" | |
"io/ioutil" | |
) | |
//高级加密标准(Adevanced Encryption Standard ,AES) | |
//16,24,32位字符串的话,分别对应AES-128,AES-192,AES-256 加密方法 | |
//key不能泄露 | |
var PwdKey = []byte("DIS**#KKKDJJSKDI") | |
//PKCS7 填充模式 | |
func PKCS7Padding(ciphertext []byte, blockSize int) []byte { | |
padding := blockSize - len(ciphertext)%blockSize | |
//Repeat()函数的功能是把切片[]byte{byte(padding)}复制padding个,然后合并成新的字节切片返回 | |
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | |
return append(ciphertext, padtext...) | |
} | |
//填充的反向操作,删除填充字符串 | |
func PKCS7UnPadding(origData []byte) ([]byte, error) { | |
//获取数据长度 | |
length := len(origData) | |
if length == 0 { | |
return nil, errors.New("加密字符串错误!") | |
} else { | |
//获取填充字符串长度 | |
unpadding := int(origData[length-1]) | |
//截取切片,删除填充字节,并且返回明文 | |
return origData[:(length - unpadding)], nil | |
} | |
} | |
//实现加密 | |
func AesEcrypt(origData []byte, key []byte) ([]byte, error) { | |
//创建加密算法实例 | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
//获取块的大小 | |
blockSize := block.BlockSize() | |
//对数据进行填充,让数据长度满足需求 | |
origData = PKCS7Padding(origData, blockSize) | |
//采用AES加密方法中CBC加密模式 | |
blocMode := cipher.NewCBCEncrypter(block, key[:blockSize]) | |
crypted := make([]byte, len(origData)) | |
//执行加密 | |
blocMode.CryptBlocks(crypted, origData) | |
return crypted, nil | |
} | |
//实现解密 | |
func AesDeCrypt(cypted []byte, key []byte) ([]byte, error) { | |
//创建加密算法实例 | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
//获取块大小 | |
blockSize := block.BlockSize() | |
//创建加密客户端实例 | |
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) | |
origData := make([]byte, len(cypted)) | |
//这个函数也可以用来解密 | |
blockMode.CryptBlocks(origData, cypted) | |
//去除填充字符串 | |
origData, err = PKCS7UnPadding(origData) | |
if err != nil { | |
return nil, err | |
} | |
return origData, err | |
} | |
//加密base64 | |
func EnPwdCode(pwd []byte) (string, error) { | |
result, err := AesEcrypt(pwd, PwdKey) | |
if err != nil { | |
return "", err | |
} | |
return base64.StdEncoding.EncodeToString(result), err | |
} | |
//解密 | |
func DePwdCode(pwd string) ([]byte, error) { | |
//解密base64字符串 | |
pwdByte, err := base64.StdEncoding.DecodeString(pwd) | |
if err != nil { | |
return nil, err | |
} | |
//执行AES解密 | |
return AesDeCrypt(pwdByte, PwdKey) | |
} | |
func EnCodeFile(filename string) (string, error) { | |
src, err := ioutil.ReadFile("01.wav") | |
if err != nil { | |
return "", err | |
} | |
pwd, err := EnPwdCode(src) | |
return pwd, err | |
} | |
func main() { | |
// pwd,err := EnCodeFile("01.wav") | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
// bytes, err := DePwdCode(pwd) | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
// ioutil.WriteFile("01out.wav",bytes,600) | |
var compressTime float32 = 0.0 | |
var unpackTime float32 = 0.0 | |
n := 1 | |
for i := 0; i < n; i++ { | |
compressTime += BenchmarkCompress() | |
unpackTime += BenchmarkUnpack() | |
} | |
fmt.Printf("compress time : %f sec \n", compressTime/float32(n)) | |
fmt.Printf("unpackTime time : %f sec \n", unpackTime/float32(n)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment