高级加密标准(英语: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-gcm不需要填充。
GCM ( Galois/Counter Mode) 指的是该对称加密采用Counter模式,并带有GMAC消息认证码。
aes-gcm 在线加密工具:https://const.net.cn/tool/aes/aes-gcm/
代码:

package main

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

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

//AES加密(GCM模式)
func AES_GCM_Encrypt(plainText []byte, key []byte) []byte {
    //指定加密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    //指定初始向量vi,长度为12
    iv := []byte("123456781234")
    additionalData := []byte("12345678")
    //指定分组模式,返回一个BlockMode接口对象
    blockMode, _ := cipher.NewGCMWithNonceSize(block, len(iv))
    //blockMode, _ := cipher.NewGCM(block)
    //加密连续
    cipherText := make([]byte, len(plainText))
    cipherText = blockMode.Seal(cipherText[:0], iv, plainText, additionalData)
    //返回密文
    return cipherText
}

//AES解密(GCM模式)
func AES_GCM_Decrypt(cipherText []byte, key []byte) []byte {
    //指定解密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    //指定初始化向量IV,和加密的一致
    iv := []byte("123456781234")
    additionalData := []byte("12345678")
    //指定分组模式,返回一个BlockMode接口对象
    blockMode, _ := cipher.NewGCMWithNonceSize(block, len(iv))
    //解密
    plainText := make([]byte, len(cipherText))
    plainText, _ = blockMode.Open(plainText[:0], iv, cipherText, additionalData)

    return plainText
}

输出 :

go run .
go crypto aes-192-gcm demo/example.
key.size = 24
加密后:085d12a881561fd6d0f4977222ada32a49d48b24e7a862a26d6c459344d9f6e57083476dd3d0107c6d13a0229e3809fdf1a1052ae652ac624867d729b9a49f len = 63
MD5后为:2db9ab0073956f1fb4ac61e4e92241fe
解密后为: https://const.net.cn/ aes-192-gcm test vectors.
本文链接地址:https://const.net.cn/115.html

标签: crypto, aes, aes-gcm

添加新评论