1 Star 0 Fork 1

技术狼/go-fun

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
crypto_aes_cfb.go 1005 Bytes
一键复制 编辑 原始数据 按行查看 历史
技术狼 提交于 2024-07-19 18:35 . no message
package fun
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
)
// =================== CFB(密码反馈模式) ======================
func AesEncryptCFB(origData []byte, key []byte) (encrypted []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
encrypted = make([]byte, aes.BlockSize+len(origData))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], origData)
return encrypted, nil
}
func AesDecryptCFB(encrypted []byte, key []byte) (decrypted []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(encrypted) < aes.BlockSize {
panic("ciphertext too short")
}
iv := encrypted[:aes.BlockSize]
encrypted = encrypted[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(encrypted, encrypted)
return encrypted, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jishulangcom/go-fun.git
git@gitee.com:jishulangcom/go-fun.git
jishulangcom
go-fun
go-fun
v0.0.3

搜索帮助