分类 Go 下的文章

“Go是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。 罗伯特·格瑞史莫、罗勃·派克及肯·汤普逊于2007年9月开始设计Go,稍后伊恩·兰斯·泰勒、拉斯·考克斯加入项目。Go是基于Inferno操作系统所开发的。”

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。现在,高级加密标准已然成为对称密钥加密中最流行的算法之一。

aes-192-cbc相较于aes-128-cbc,主要差别在于密钥的长度,aes-192-cbc密钥的长度为24字节,aes-256-cbc密钥的长度为32字节,aes-128-cbc密码的长度为16字节。

代码:

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/md5"
    "fmt"
)

func main() {
    fmt.Println("go crypto aes-192-cbc demo/example.")
    message := []byte("https://const.net.cn")
    //指定密钥
    key := []byte("123456781234567812345678")
    //加密
    cipherText := AES_CBC_Encrypt(message, key)
    fmt.Printf("加密后:%x\n", cipherText)
    fmt.Printf("MD5后为:%x\n", md5.Sum(cipherText))
    //解密
    plainText := AES_CBC_Decrypt(cipherText, key)
    fmt.Println("解密后为:", string(plainText))
}

//对明文进行填充
func Padding(plainText []byte, blockSize int) []byte {
    //计算要填充的长度
    n := blockSize - len(plainText)%blockSize
    //对原来的明文填充n个n
    temp := bytes.Repeat([]byte{byte(n)}, n)
    plainText = append(plainText, temp...)
    return plainText
}

//对密文删除填充
func UnPadding(cipherText []byte) []byte {
    //取出密文最后一个字节end
    end := cipherText[len(cipherText)-1]
    //删除填充
    cipherText = cipherText[:len(cipherText)-int(end)]
    return cipherText
}

//AES加密(CBC模式)
func AES_CBC_Encrypt(plainText []byte, key []byte) []byte {
    //指定加密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    //进行填充
    plainText = Padding(plainText, block.BlockSize())
    //指定初始向量vi,长度和block的块尺寸一致
    iv := []byte("1234567812345678")
    //指定分组模式,返回一个BlockMode接口对象
    blockMode := cipher.NewCBCEncrypter(block, iv)
    //加密连续数据库
    cipherText := make([]byte, len(plainText))
    blockMode.CryptBlocks(cipherText, plainText)
    //返回密文
    return cipherText
}

//AES解密(CBC模式)
func AES_CBC_Decrypt(cipherText []byte, key []byte) []byte {
    //指定解密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    //指定初始化向量IV,和加密的一致
    iv := []byte("1234567812345678")
    //指定分组模式,返回一个BlockMode接口对象
    blockMode := cipher.NewCBCDecrypter(block, iv)
    //解密
    plainText := make([]byte, len(cipherText))
    blockMode.CryptBlocks(plainText, cipherText)
    //删除填充
    plainText = UnPadding(plainText)
    return plainText
}

输出:

go run .
go crypto aes-192-cbc demo/example.
加密后:52a880797d51a84f7eb659185fd02da1d84839a415bd802cef844ba41fa0f538
MD5后为:e3642d3d1059f59bbf5601b5687df351
解密后为: https://const.net.cn
echo -n "https://const.net.cn" | openssl enc -aes-192-cbc -K 313233343536373831323334353637383132333435363738 -iv 31323334353637383132333435363738 |openssl dgst -md5
(stdin)= e3642d3d1059f59bbf5601b5687df351
echo -n "https://const.net.cn" | openssl enc -aes-192-cbc -K 313233343536373831323334353637383132333435363738 -iv 31323334353637383132333435363738 |hexdump -e '32/1 "%02x" "\n"'
52a880797d51a84f7eb659185fd02da1d84839a415bd802cef844ba41fa0f538

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。现在,高级加密标准已然成为对称密钥加密中最流行的算法之一。

aes-192-cbc相较于aes-128-cbc,主要差别在于密钥的长度,aes-192-cbc密钥的长度为24字节,aes-256-cbc密钥的长度为32字节,aes-128-cbc密码的长度为16字节。

代码:

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/md5"
    "fmt"
)

func main() {
    fmt.Println("go crypto aes-256-cbc demo/example.")
    message := []byte("https://const.net.cn")
    //指定密钥
    key := []byte("12345678123456781234567812345678")
    //加密
    cipherText := AES_CBC_Encrypt(message, key)
    fmt.Printf("加密后:%x\n", cipherText)
    fmt.Printf("MD5后为:%x\n", md5.Sum(cipherText))
    //解密
    plainText := AES_CBC_Decrypt(cipherText, key)
    fmt.Println("解密后为:", string(plainText))
}

//对明文进行填充
func Padding(plainText []byte, blockSize int) []byte {
    //计算要填充的长度
    n := blockSize - len(plainText)%blockSize
    //对原来的明文填充n个n
    temp := bytes.Repeat([]byte{byte(n)}, n)
    plainText = append(plainText, temp...)
    return plainText
}

//对密文删除填充
func UnPadding(cipherText []byte) []byte {
    //取出密文最后一个字节end
    end := cipherText[len(cipherText)-1]
    //删除填充
    cipherText = cipherText[:len(cipherText)-int(end)]
    return cipherText
}

//AES加密(CBC模式)
func AES_CBC_Encrypt(plainText []byte, key []byte) []byte {
    //指定加密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    //进行填充
    plainText = Padding(plainText, block.BlockSize())
    //指定初始向量vi,长度和block的块尺寸一致
    iv := []byte("1234567812345678")
    //指定分组模式,返回一个BlockMode接口对象
    blockMode := cipher.NewCBCEncrypter(block, iv)
    //加密连续数据库
    cipherText := make([]byte, len(plainText))
    blockMode.CryptBlocks(cipherText, plainText)
    //返回密文
    return cipherText
}

//AES解密(CBC模式)
func AES_CBC_Decrypt(cipherText []byte, key []byte) []byte {
    //指定解密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    //指定初始化向量IV,和加密的一致
    iv := []byte("1234567812345678")
    //指定分组模式,返回一个BlockMode接口对象
    blockMode := cipher.NewCBCDecrypter(block, iv)
    //解密
    plainText := make([]byte, len(cipherText))
    blockMode.CryptBlocks(plainText, cipherText)
    //删除填充
    plainText = UnPadding(plainText)
    return plainText
}

输出:

go run .
go crypto aes-256-cbc demo/example.
加密后:72cb73478c241dc0a6a2e1ca44052d8cda2b18c2b4f23108d305dbb8804e4228
MD5后为:a5971f392f389c63cf3e05c5c187285e
解密后为: https://const.net.cn
echo -n "https://const.net.cn" | openssl enc -aes-256-cbc -K 3132333435363738313233343536373831323334353637383132333435363738 -iv 31323334353637383132333435363738 |openssl dgst -md5
(stdin)= a5971f392f389c63cf3e05c5c187285e
echo -n "https://const.net.cn" | openssl enc -aes-256-cbc -K 3132333435363738313233343536373831323334353637383132333435363738 -iv 31323334353637383132333435363738 |hexdump -e '32/1 "%02x" "\n"'
72cb73478c241dc0a6a2e1ca44052d8cda2b18c2b4f23108d305dbb8804e4228

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。现在,高级加密标准已然成为对称密钥加密中最流行的算法之一。
aes-192-ecb密钥的长度为24字节,aes-256-ecb密钥的长度为32字节,aes-128-ecb密码的长度为16字节。aes-ecb只需要密钥就行,不需要iv。
代码:

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/md5"
    "fmt"
)

func main() {
    fmt.Println("go crypto aes-128-ecb demo/example.")
    message := []byte("https://const.net.cn")
    //指定密钥
    key := []byte("1234567812345678")
    //加密
    cipherText := AES_ECB_Encrypt(message, key)
    fmt.Printf("加密后:%x\n", cipherText)
    fmt.Printf("MD5后为:%x\n", md5.Sum(cipherText))
    //解密
    plainText := AES_ECB_Decrypt(cipherText, key)
    fmt.Println("解密后为:", string(plainText))
}

//对明文进行填充
func Padding(plainText []byte, blockSize int) []byte {
    //计算要填充的长度
    n := blockSize - len(plainText)%blockSize
    //对原来的明文填充n个n
    temp := bytes.Repeat([]byte{byte(n)}, n)
    plainText = append(plainText, temp...)
    return plainText
}

//对密文删除填充
func UnPadding(cipherText []byte) []byte {
    //取出密文最后一个字节end
    end := cipherText[len(cipherText)-1]
    //删除填充
    cipherText = cipherText[:len(cipherText)-int(end)]
    return cipherText
}

//AES加密(ECB模式)
func AES_ECB_Encrypt(plainText []byte, key []byte) []byte {
    //指定加密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    //进行填充
    plainText = Padding(plainText, block.BlockSize())
    //加密连续数据库
    cipherText := make([]byte, len(plainText))
    //存储每次加密的数据
    tmpData := make([]byte, block.BlockSize())
    //分组分块加密
    for index := 0; index < len(plainText); index += block.BlockSize() {
        block.Encrypt(tmpData, plainText[index:index+block.BlockSize()])
        copy(cipherText[index:], tmpData)
    }
    //返回密文
    return cipherText
}

//AES解密(ECB模式)
func AES_ECB_Decrypt(cipherText []byte, key []byte) []byte {
    //指定解密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    //解密
    plainText := make([]byte, len(cipherText))
    //存储每次加密的数据
    tmpData := make([]byte, block.BlockSize())
    //分组分块解密
    for index := 0; index < len(cipherText); index += block.BlockSize() {
        block.Decrypt(tmpData, cipherText[index:index+block.BlockSize()])
        copy(plainText[index:], tmpData)
    }
    //删除填充
    plainText = UnPadding(plainText)
    return plainText
}

输出:

go run .
go crypto aes-128-ecb demo/example.
加密后:9882d87cbbc7bb54db8c93db801b3065db891343be73de721702accbee7a2d5d
MD5后为:c09ba340f5b51f531e72a366e73760e4
解密后为: https://const.net.cn
echo -n "https://const.net.cn" | openssl enc -aes-128-ecb -K 31323334353637383132333435363738  |hexdump -e '32/1 "%02x" "\n"'
9882d87cbbc7bb54db8c93db801b3065db891343be73de721702accbee7a2d5d
echo -n "https://const.net.cn" | openssl enc -aes-128-ecb -K 31323334353637383132333435363738  |openssl dgst -md5
(stdin)= c09ba340f5b51f531e72a366e73760e4

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。现在,高级加密标准已然成为对称密钥加密中最流行的算法之一。
aes-192-ecb密钥的长度为24字节,aes-256-ecb密钥的长度为32字节,aes-128-ecb密码的长度为16字节。aes-ecb只需要密钥就行,不需要iv。
电码本模式(Electronic Codebook Book (ECB)).
代码:

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/md5"
    "fmt"
)

func main() {
    fmt.Println("go crypto aes-192-ecb demo/example.")
    message := []byte("https://const.net.cn")
    //指定密钥
    key := []byte("123456781234567812345678")
    //加密
    cipherText := AES_ECB_Encrypt(message, key)
    fmt.Printf("加密后:%x\n", cipherText)
    fmt.Printf("MD5后为:%x\n", md5.Sum(cipherText))
    //解密
    plainText := AES_ECB_Decrypt(cipherText, key)
    fmt.Println("解密后为:", string(plainText))
}

//对明文进行填充
func Padding(plainText []byte, blockSize int) []byte {
    //计算要填充的长度
    n := blockSize - len(plainText)%blockSize
    //对原来的明文填充n个n
    temp := bytes.Repeat([]byte{byte(n)}, n)
    plainText = append(plainText, temp...)
    return plainText
}

//对密文删除填充
func UnPadding(cipherText []byte) []byte {
    //取出密文最后一个字节end
    end := cipherText[len(cipherText)-1]
    //删除填充
    cipherText = cipherText[:len(cipherText)-int(end)]
    return cipherText
}

//AES加密(ECB模式)
func AES_ECB_Encrypt(plainText []byte, key []byte) []byte {
    //指定加密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    //进行填充
    plainText = Padding(plainText, block.BlockSize())
    //加密连续数据库
    cipherText := make([]byte, len(plainText))
    //存储每次加密的数据
    tmpData := make([]byte, block.BlockSize())
    //分组分块加密
    for index := 0; index < len(plainText); index += block.BlockSize() {
        block.Encrypt(tmpData, plainText[index:index+block.BlockSize()])
        copy(cipherText[index:], tmpData)
    }
    //返回密文
    return cipherText
}

//AES解密(ECB模式)
func AES_ECB_Decrypt(cipherText []byte, key []byte) []byte {
    //指定解密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    //解密
    plainText := make([]byte, len(cipherText))
    //存储每次加密的数据
    tmpData := make([]byte, block.BlockSize())
    //分组分块解密
    for index := 0; index < len(cipherText); index += block.BlockSize() {
        block.Decrypt(tmpData, cipherText[index:index+block.BlockSize()])
        copy(plainText[index:], tmpData)
    }
    //删除填充
    plainText = UnPadding(plainText)
    return plainText
}

输出:

go run .
go crypto aes-192-ecb demo/example.
加密后:a3760cfd4180c09864fd9fb3446489d65b403f8968d270c27ba7999490bb3a53
MD5后为:bd60b5160c7f4114b46d971e498095ba
解密后为: https://const.net.cn
echo -n "https://const.net.cn" | openssl enc -aes-192-ecb -K 313233343536373831323334353637383132333435363738  |hexdump -e '32/1 "%02x" "\n"'
a3760cfd4180c09864fd9fb3446489d65b403f8968d270c27ba7999490bb3a53
echo -n "https://const.net.cn" | openssl enc -aes-192-ecb -K 313233343536373831323334353637383132333435363738  |openssl dgst -md5
(stdin)= bd60b5160c7f4114b46d971e498095ba

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。现在,高级加密标准已然成为对称密钥加密中最流行的算法之一。
aes-192-ecb密钥的长度为24字节,aes-256-ecb密钥的长度为32字节,aes-128-ecb密码的长度为16字节。aes-ecb只需要密钥就行,不需要iv。
电码本模式(Electronic Codebook Book (ECB)).
代码:

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/md5"
    "fmt"
)

func main() {
    fmt.Println("go crypto aes-256-ecb demo/example.")
    message := []byte("https://const.net.cn")
    //指定密钥
    key := []byte("12345678123456781234567812345678")
    //加密
    cipherText := AES_ECB_Encrypt(message, key)
    fmt.Printf("加密后:%x\n", cipherText)
    fmt.Printf("MD5后为:%x\n", md5.Sum(cipherText))
    //解密
    plainText := AES_ECB_Decrypt(cipherText, key)
    fmt.Println("解密后为:", string(plainText))
}

//对明文进行填充
func Padding(plainText []byte, blockSize int) []byte {
    //计算要填充的长度
    n := blockSize - len(plainText)%blockSize
    //对原来的明文填充n个n
    temp := bytes.Repeat([]byte{byte(n)}, n)
    plainText = append(plainText, temp...)
    return plainText
}

//对密文删除填充
func UnPadding(cipherText []byte) []byte {
    //取出密文最后一个字节end
    end := cipherText[len(cipherText)-1]
    //删除填充
    cipherText = cipherText[:len(cipherText)-int(end)]
    return cipherText
}

//AES加密(ECB模式)
func AES_ECB_Encrypt(plainText []byte, key []byte) []byte {
    //指定加密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    //进行填充
    plainText = Padding(plainText, block.BlockSize())
    //加密连续数据库
    cipherText := make([]byte, len(plainText))
    //存储每次加密的数据
    tmpData := make([]byte, block.BlockSize())
    //分组分块加密
    for index := 0; index < len(plainText); index += block.BlockSize() {
        block.Encrypt(tmpData, plainText[index:index+block.BlockSize()])
        copy(cipherText[index:], tmpData)
    }
    //返回密文
    return cipherText
}

//AES解密(ECB模式)
func AES_ECB_Decrypt(cipherText []byte, key []byte) []byte {
    //指定解密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    //解密
    plainText := make([]byte, len(cipherText))
    //存储每次加密的数据
    tmpData := make([]byte, block.BlockSize())
    //分组分块解密
    for index := 0; index < len(cipherText); index += block.BlockSize() {
        block.Decrypt(tmpData, cipherText[index:index+block.BlockSize()])
        copy(plainText[index:], tmpData)
    }
    //删除填充
    plainText = UnPadding(plainText)
    return plainText
}

输出:

go run .

go crypto aes-256-ecb demo/example.
加密后:7b30a4eda5f7497ddf1e2a5864031a199fdaff46146511dd964969971442ae93
MD5后为:8e6ada06f55cdb4dea5be922be612244
解密后为: https://const.net.cn

echo -n "https://const.net.cn" | openssl enc -aes-256-ecb -K 3132333435363738313233343536373831323334353637383132333435363738  |openssl dgst -md5

(stdin)= 8e6ada06f55cdb4dea5be922be612244

echo -n "https://const.net.cn" | openssl enc -aes-256-ecb -K 3132333435363738313233343536373831323334353637383132333435363738  |hexdump -e '32/1 "%02x" "\n"'

7b30a4eda5f7497ddf1e2a5864031a199fdaff46146511dd964969971442ae93