Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Golang的crypto库加密解密怎么做 AES与RSA算法实现_创想鸟

Golang的crypto库加密解密怎么做 AES与RSA算法实现

Go语言中crypto库支持AES和RSA加密:AES使用GCM模式实现对称加解密,需随机IV和密钥;RSA用于非对称加密,推荐OAEP填充,常结合使用AES加密数据、RSA加密密钥以保障安全传输

golang的crypto库加密解密怎么做 aes与rsa算法实现

在Go语言中,crypto 库提供了强大的加密支持,其中 AES(对称加密)和 RSA(非对称加密)是两种最常用的加密算法。下面介绍如何使用标准库实现 AES 和 RSA 的基本加解密操作。

AES 对称加密(CBC模式示例)

AES 使用同一个密钥进行加密和解密,适合加密大量数据。以下是一个使用 AES-CBC 模式的简单实现:

加密流程:

密钥必须是 16、24 或 32 字节(对应 AES-128、192、256) 使用随机生成的初始化向量(IV)保证安全性 明文需填充至块大小(16字节)的倍数

代码示例:

立即学习“go语言免费学习笔记(深入)”;

func aesEncrypt(plaintext []byte, key []byte) ([]byte, error) {    block, err := aes.NewCipher(key)    if err != nil {        return nil, err    }    gcm, err := cipher.NewGCM(block)    if err != nil {        return nil, err    }    nonce := make([]byte, gcm.NonceSize())    if _, err := io.ReadFull(rand.Reader, nonce); err != nil {        return nil, err    }    ciphertext := gcm.Seal(nonce, nonce, plaintext, nil)    return ciphertext, nil}

解密流程:

从密文中提取 nonce 使用相同密钥和 nonce 进行解密

代码示例:

立即学习“go语言免费学习笔记(深入)”;

func aesDecrypt(ciphertext []byte, key []byte) ([]byte, error) {    block, err := aes.NewCipher(key)    if err != nil {        return nil, err    }    gcm, err := cipher.NewGCM(block)    if err != nil {        return nil, err    }    nonceSize := gcm.NonceSize()    if len(ciphertext) < nonceSize {        return nil, errors.New("ciphertext too short")    }    nonce, cipherblob := ciphertext[:nonceSize], ciphertext[nonceSize:]    plaintext, err := gcm.Open(nil, nonce, cipherblob, nil)    if err != nil {        return nil, err    }    return plaintext, nil}

RSA 非对称加密(公钥加密,私钥解密)

RSA 适用于加密小数据(如密钥),不适合大文件。以下演示生成密钥对、加密和解密过程。

生成密钥对:

代码示例:

立即学习“go语言免费学习笔记(深入)”;

func generateRSAKeys() (*rsa.PrivateKey, *rsa.PublicKey, error) {    privateKey, err := rsa.GenerateKey(rand.Reader, 2048)    if err != nil {        return nil, nil, err    }    return privateKey, &privateKey.PublicKey, nil}

使用公钥加密:

数据长度受限(如 2048位密钥最多加密 245 字节) 推荐使用 OAEP 填充(比 PKCS1v15 更安全)

代码示例:

立即学习“go语言免费学习笔记(深入)”;

func rsaEncrypt(plaintext []byte, pubKey *rsa.PublicKey) ([]byte, error) {    ciphertext, err := rsa.EncryptOAEP(        sha256.New(),        rand.Reader,        pubKey,        plaintext,        nil,    )    return ciphertext, err}

使用私钥解密:

代码示例:

立即学习“go语言免费学习笔记(深入)”;

func rsaDecrypt(ciphertext []byte, privKey *rsa.PrivateKey) ([]byte, error) {    plaintext, err := rsa.DecryptOAEP(        sha256.New(),        rand.Reader,        privKey,        ciphertext,        nil,    )    return plaintext, err}

实际使用建议

在真实场景中,通常结合使用 AES 和 RSA:

用 AES 加密原始数据(高效) 用 RSA 加密 AES 密钥(安全传输) 接收方先用 RSA 私钥解密出 AES 密钥,再解密数据

注意密钥管理、随机数安全、填充方式选择等细节,避免使用 ECB 模式或弱填充。

基本上就这些,Go 的 crypto 库设计清晰,只要理解加密原理,实现起来并不复杂但容易忽略安全细节。

以上就是Golang的crypto库加密解密怎么做 AES与RSA算法实现的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1398443.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Go 语言中函数与方法的区别
上一篇 2025年12月15日 15:26:38
Go语言如何将字符串转换为字节数组
下一篇 2025年12月15日 15:26:49

相关推荐

发表回复

登录后才能评论
关注微信