package loader

import ( 'crypto/aes' 'fmt'

'crypto/cipher'
'crypto/rand'
'io'

)

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
}

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

} func Decrypt(key []byte, ciphertext []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
}

nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
	return nil, fmt.Errorf('ciphertext too short')
}

nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
return gcm.Open(nil, nonce, ciphertext, nil)

}

Go语言 AES 加密解密模块

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

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