Go 利用crypto实现aes-128-ccm计算方法
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。现在,高级加密标准已然成为对称密钥加密中最流行的算法之一。
aes-192密钥的长度为24字节,aes-256密钥的长度为32字节,aes-128密码的长度为16字节。aes-ccm需要key,nonce,adata,另外aes-ccm不需要填充。
Counter with CBC-MAC (CCM)
代码:
package main
import (
"bytes"
"crypto/aes"
"crypto/md5"
"encoding/hex"
"fmt"
aesccm "github.com/pschlump/AesCCM"
)
func main() {
fmt.Println("go crypto aes-128-ccm demo/example.")
message := []byte("https://const.net.cn")
//message, _ := hex.DecodeString("08090A0B0C0D0E0F101112131415161718191A1B1C1D1E")
//指定密钥
key, _ := hex.DecodeString("C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF")
//加密
cipherText := AES_CCM_Encrypt(message, key)
fmt.Printf("加密后:%x\n", cipherText)
fmt.Printf("MD5后为:%x\n", md5.Sum(cipherText))
//解密
plainText := AES_CCM_Decrypt(cipherText, key)
fmt.Printf("解密后为:%s\n", string(plainText))
}
//AES加密(CCM模式)
func AES_CCM_Encrypt(plainText []byte, key []byte) []byte {
//指定加密算法,返回一个AES算法的Block接口对象
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
//指定初始向量vi,长度和block的块尺寸一致
nonce, _ := hex.DecodeString("00000003020100A0A1A2A3A4A5")
addidata, _ := hex.DecodeString("0001020304050607")
//指定分组模式,返回一个BlockMode接口对象
blockMode, err := aesccm.NewCCM(block, 8, len(nonce))
if err != nil {
panic(err)
}
//加密数据
cipherText := make([]byte, len(plainText))
cipherText = blockMode.Seal(cipherText[:0], nonce, plainText, addidata)
//返回密文
return cipherText
}
//AES解密(CCM模式)
func AES_CCM_Decrypt(cipherText []byte, key []byte) []byte {
//指定解密算法,返回一个AES算法的Block接口对象
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
nonce, _ := hex.DecodeString("00000003020100A0A1A2A3A4A5")
addidata, _ := hex.DecodeString("0001020304050607")
//指定分组模式,返回一个BlockMode接口对象
blockMode, _ := aesccm.NewCCM(block, 8, len(nonce))
//解密
plainText := make([]byte, len(cipherText))
plainText, _ = blockMode.Open(plainText[:0], nonce, cipherText, addidata)
return plainText
}
输出:
go run .
go crypto aes-128-ccm demo/example.
加密后:38f1e9e11ef142f28318aca2a0c2f1f201681214584151a8d5b82edf
MD5后为:672413b665d0ea5ade639df117ff0d59
解密后为:https://const.net.cn
aes-128-ccm Test Vectors from RFC 3610「https://datatracker.ietf.org/doc/html/rfc3610」
key, nonce, adata, plaintext, ciphertext string
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "00000003020100A0A1A2A3A4A5",
- "0001020304050607",
- "08090A0B0C0D0E0F101112131415161718191A1B1C1D1E",
- "588C979A61C663D2F066D0C2C0F989806D5F6B61DAC38417E8D12CFDF926E0",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "00000004030201A0A1A2A3A4A5",
- "0001020304050607",
- "08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
- "72C91A36E135F8CF291CA894085C87E3CC15C439C9E43A3BA091D56E10400916",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "00000005040302A0A1A2A3A4A5",
- "0001020304050607",
- "08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20",
- "51B1E5F44A197D1DA46B0F8E2D282AE871E838BB64DA8596574ADAA76FBD9FB0C5",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "00000006050403A0A1A2A3A4A5",
- "000102030405060708090A0B",
- "0C0D0E0F101112131415161718191A1B1C1D1E",
- "A28C6865939A9A79FAAA5C4C2A9D4A91CDAC8C96C861B9C9E61EF1",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "00000007060504A0A1A2A3A4A5",
- "000102030405060708090A0B",
- "0C0D0E0F101112131415161718191A1B1C1D1E1F",
- "DCF1FB7B5D9E23FB9D4E131253658AD86EBDCA3E51E83F077D9C2D93",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "00000008070605A0A1A2A3A4A5",
- "000102030405060708090A0B",
- "0C0D0E0F101112131415161718191A1B1C1D1E1F20",
- "6FC1B011F006568B5171A42D953D469B2570A4BD87405A0443AC91CB94",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "00000009080706A0A1A2A3A4A5",
- "0001020304050607",
- "08090A0B0C0D0E0F101112131415161718191A1B1C1D1E",
- "0135D1B2C95F41D5D1D4FEC185D166B8094E999DFED96C048C56602C97ACBB7490",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "0000000A090807A0A1A2A3A4A5",
- "0001020304050607",
- "08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
- "7B75399AC0831DD2F0BBD75879A2FD8F6CAE6B6CD9B7DB24C17B4433F434963F34B4",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "0000000B0A0908A0A1A2A3A4A5",
- "0001020304050607",
- "08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20",
- "82531A60CC24945A4B8279181AB5C84DF21CE7F9B73F42E197EA9C07E56B5EB17E5F4E",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "0000000C0B0A09A0A1A2A3A4A5",
- "000102030405060708090A0B",
- "0C0D0E0F101112131415161718191A1B1C1D1E",
- "07342594157785152B074098330ABB141B947B566AA9406B4D999988DD",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "0000000D0C0B0AA0A1A2A3A4A5",
- "000102030405060708090A0B",
- "0C0D0E0F101112131415161718191A1B1C1D1E1F",
- "676BB20380B0E301E8AB79590A396DA78B834934F53AA2E9107A8B6C022C",
- },
- {
- "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF",
- "0000000E0D0C0BA0A1A2A3A4A5",
- "000102030405060708090A0B",
- "0C0D0E0F101112131415161718191A1B1C1D1E1F20",
- "C0FFA0D6F05BDB67F24D43A4338D2AA4BED7B20E43CD1AA31662E7AD65D6DB",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "00412B4EA9CDBE3C9696766CFA",
- "0BE1A88BACE018B1",
- "08E8CF97D820EA258460E96AD9CF5289054D895CEAC47C",
- "4CB97F86A2A4689A877947AB8091EF5386A6FFBDD080F8E78CF7CB0CDDD7B3",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "0033568EF7B2633C9696766CFA",
- "63018F76DC8A1BCB",
- "9020EA6F91BDD85AFA0039BA4BAFF9BFB79C7028949CD0EC",
- "4CCB1E7CA981BEFAA0726C55D378061298C85C92814ABC33C52EE81D7D77C08A",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "00103FE41336713C9696766CFA",
- "AA6CFA36CAE86B40",
- "B916E0EACC1C00D7DCEC68EC0B3BBB1A02DE8A2D1AA346132E",
- "B1D23A2220DDC0AC900D9AA03C61FCF4A559A4417767089708A776796EDB723506",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "00764C63B8058E3C9696766CFA",
- "D0D0735C531E1BECF049C244",
- "12DAAC5630EFA5396F770CE1A66B21F7B2101C",
- "14D253C3967B70609B7CBB7C499160283245269A6F49975BCADEAF",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "00F8B678094E3B3C9696766CFA",
- "77B60F011C03E1525899BCAE",
- "E88B6A46C78D63E52EB8C546EFB5DE6F75E9CC0D",
- "5545FF1A085EE2EFBF52B2E04BEE1E2336C73E3F762C0C7744FE7E3C",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "00D560912D3F703C9696766CFA",
- "CD9044D2B71FDB8120EA60C0",
- "6435ACBAFB11A82E2F071D7CA4A5EBD93A803BA87F",
- "009769ECABDF48625594C59251E6035722675E04C847099E5AE0704551",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "0042FFF8F1951C3C9696766CFA",
- "D85BC7E69F944FB8",
- "8A19B950BCF71A018E5E6701C91787659809D67DBEDD18",
- "BC218DAA947427B6DB386A99AC1AEF23ADE0B52939CB6A637CF9BEC2408897C6BA",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "00920F40E56CDC3C9696766CFA",
- "74A0EBC9069F5B37",
- "1761433C37C5A35FC1F39F406302EB907C6163BE38C98437",
- "5810E6FD25874022E80361A478E3E9CF484AB04F447EFFF6F0A477CC2FC9BF548944",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "0027CA0C7120BC3C9696766CFA",
- "44A3AA3AAE6475CA",
- "A434A8E58500C6E41530538862D686EA9E81301B5AE4226BFA",
- "F2BEED7BC5098E83FEB5B31608F8E29C38819A89C8E776F1544D4151A4ED3A8B87B9CE",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "005B8CCBCD9AF83C9696766CFA",
- "EC46BB63B02520C33C49FD70",
- "B96B49E21D621741632875DB7F6C9243D2D7C2",
- "31D750A09DA3ED7FDDD49A2032AABF17EC8EBF7D22C8088C666BE5C197",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "003EBE94044B9A3C9696766CFA",
- "47A65AC78B3D594227E85E71",
- "E2FCFBB880442C731BF95167C8FFD7895E337076",
- "E882F1DBD38CE3EDA7C23F04DD65071EB41342ACDF7E00DCCEC7AE52987D",
- },
- {
- "D7828D13B2B0BDC325A76236DF93CC6B",
- "008D493B30AE8B3C9696766CFA",
- "6E37A6EF546D955D34AB6059",
- "ABF21C0B02FEB88F856DF4A37381BCE3CC128517D4",
- "F32905B88A641B04B9C9FFB58CC390900F3DA12AB16DCE9E82EFA16DA62059",
- },
本文链接地址:https://const.net.cn/111.html