package loader

import ( "crypto/aes" "fmt" "crypto/cipher" "crypto/rand" "io" "os" )

func ToString(payload []byte) string { return fmt.Sprint([]byte(payload)) }

func Encrypt(key []byte, plaintext []byte) ([]byte, error) { c, err := aes.NewCipher(key) if err != nil { return nil, err }

gcm, err := cipher.NewGCM(c)
if err != nil {
	return nil, err
}

nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
	return nil, err
}

// 输出密钥到文件
f, err := os.Create('miyao.txt')
if err != nil {
	return nil, err
}
defer f.Close()

_, err = f.Write(key)
if err != nil {
	return nil, err
}

return gcm.Seal(nonce, nonce, plaintext, nil), nil

}

Go语言AES加密实现:生成密钥并写入文件

原文地址: https://www.cveoy.top/t/topic/lXut 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录