高级加密标准(英语: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-192-ccm demo/example.")
    message := []byte("https://const.net.cn")
    //指定密钥
    key, _ := hex.DecodeString("C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF0102030405060708")
    //加密
    cipherText := AES_CCM_Encrypt(message, key)
    fmt.Printf("加密后cipher:%X\n", cipherText[:len(cipherText)-8])
    fmt.Printf("加密后tag:%X\n", cipherText[len(cipherText)-8:])
    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-192-ccm demo/example.
加密后cipher:D54ED5B00AEC6BFF806F26CE0358BB1A0C737008
加密后tag:D70AE3DCF87AFA42
MD5后为:1560BC9CBBE9679C5EDA81B915BDF02C
解密后为:https://const.net.cn

c++ openssl 验证:

plaintext = https://const.net.cn
plain hexstring = 68747470733A2F2F636F6E73742E6E65742E636E
key hexstring = C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF0102030405060708
nonce hexstring = 00000003020100A0A1A2A3A4A5
adata hexstring = 0001020304050607
cipher hexstring = D54ED5B00AEC6BFF806F26CE0358BB1A0C737008
tag hexstring = D70AE3DCF87AFA42
本文链接地址:https://const.net.cn/112.html

标签: none

添加新评论