Golang里的AES、DES、3DES加解密是怎样的
AES
1、AES(Advanced Encryption Standard,高级加密标准),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。Golang里提供了AES加密的实现,可以使用aes包,其中包含了AES的基本实现,以及CBC、CFB等模式的实现。
2、AES加密的实现代码如下:
package main
import (
"crypto/aes"
"crypto/cipher"
"fmt"
)
func main() {
// 要加密的字符串
src := []byte("Hello World!")
// 密钥
key := []byte("1234567890123456")
// 创建一个加密算法aes
block, err := aes.NewCipher(key)
if err != nil {
fmt.Println("key error", err)
return
}
// 加密字符串
cipherText := make([]byte, len(src))
blockMode := cipher.NewCBCEncrypter(block, key)
blockMode.CryptBlocks(cipherText, src)
fmt.Println(cipherText)DES
1、DES(Data Encryption Standard,数据加密标准),是一种使用密钥加密的块算法,Golang里提供了DES加解密的实现,可以使用des包,其中包含了DES的基本实现,以及CBC、CFB等模式的实现。
2、DES加密的实现代码如下:
package main
import (
"crypto/des"
"crypto/cipher"
"fmt"
)
func main() {
// 要加密的字符串
src := []byte("Hello World!")
// 密钥
key := []byte("12345678")
// 创建一个加密算法aes
block, err := des.NewCipher(key)
if err != nil {
fmt.Println("key error", err)
return
}
// 加密字符串
cipherText := make([]byte, len(src))
blockMode := cipher.NewCBCEncrypter(block, key)
blockMode.CryptBlocks(cipherText, src)
fmt.Println(cipherText)3DES
1、3DES(Triple Data Encryption Standard,三重数据加密标准),是DES的一种模式,它使用3条56位的密钥对数据进行三次加密,强度更高。Golang里提供了3DES加解密的实现,可以使用des包,其中包含了3DES的基本实现,以及CBC、CFB等模式的实现。
2、3DES加密的实现代码如下:
package main
import (
"crypto/des"
"crypto/cipher"
"fmt"
)
func main() {
// 要加密的字符串
src := []byte("Hello World!")
// 密钥
key := []byte("1234567890123456")
// 创建一个加密算法aes
block, err := des.NewTripleDESCipher(key)
if err != nil {
fmt.Println("key error", err)
return
}
// 加密字符串
cipherText := make([]byte, len(src))
blockMode := cipher.NewCBCEncrypter(block, key)
blockMode.CryptBlocks(cipherText, src)
fmt.Println(cipherText)猜您想看
-
spark 3.0中如何实现查询计划
1、什么是查询...
2023年05月22日 -
油猴脚本调试技巧:使用 Tampermonkey Console Plus 插件
Tamperm...
2023年05月13日 -
JVM的垃圾回收算法详细介绍
一、JVM垃圾...
2023年05月26日 -
如何进行SAP UI5和Angular里控制器Controller实现逻辑比较
SAP UI5...
2023年07月22日 -
Qt如何实现海康sdk本地播放
1.Qt实现海...
2023年05月26日 -
如何用ElasticSearch实现基于标签的兴趣推荐
一、Elast...
2023年05月26日