go標準庫的學習-crypto/aes

參考:https://studygolang.com/pkgdocgolang

導入方式:算法

import "crypto/aes"

aes包實現了AES加密算法,參見U.S. Federal Information Processing Standards Publication 197。加密

 

Constants 

const BlockSize = 16

AES字節塊大小。spa

type KeySizeError

type KeySizeError int

func (KeySizeError) Error

func (k KeySizeError) Error() string

func NewCipher

func NewCipher(key []byte) (cipher.Block, error)

建立一個cipher.Block接口。參數key爲密鑰,長度只能是1六、2四、32字節,用以選擇AES-12八、AES-19二、AES-256。code

cipher.Block爲crypto/cipher包中的:orm

type Block

type Block interface {
    // 返回加密字節塊的大小
    BlockSize() int
    // 加密src的第一塊數據並寫入dst,src和dst可指向同一內存地址
    Encrypt(dst, src []byte)
    // 解密src的第一塊數據並寫入dst,src和dst可指向同一內存地址
    Decrypt(dst, src []byte)
}

Block接口表明一個使用特定密鑰的底層塊加/解密器。它提供了加密和解密獨立數據塊的能力。blog

加密舉例:接口

package main

import (
    "fmt"
    "crypto/aes"
)

type CryptTest struct {
    key []byte //密鑰
    in  []byte //被加密的值
    out []byte //加密後返回的值
}

var encryptTests = []CryptTest{
    {
        // Appendix B.
        []byte{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c},
        []byte{0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34},
        []byte{0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32},
    },
    {
        // Appendix C.1.  AES-128
        []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
        []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
        []byte{0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a},
    },
    {
        // Appendix C.2.  AES-192
        []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
        },
        []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
        []byte{0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91},
    },
    {
        // Appendix C.3.  AES-256
        []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
        },
        []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
        []byte{0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89},
    },
}

func main() {
    for i, tt := range encryptTests {
        c, err := aes.NewCipher(tt.key)
        if err != nil {
            fmt.Printf("NewCipher(%d bytes) = %s\n", len(tt.key), err)
            continue
        }
        out := make([]byte, len(tt.in))
        c.Encrypt(out, tt.in) //對tt.in進行加密,並將值寫到out中
        for j, v := range out { //判斷輸出是否和上面的tt.out值相同
            if v != tt.out[j] {
                fmt.Printf("Cipher.Encrypt %d: out[%d] = %#x, want %#x\n", i, j, v, tt.out[j])
                break
            }
        }
    }
}

 

解密舉例:ip

package main

import (
    "fmt"
    "crypto/aes"
)

type CryptTest struct {
    key []byte //密鑰
    in  []byte //被加密的值
    out []byte //加密後返回的值
}

var encryptTests = []CryptTest{
    {
        // Appendix B.
        []byte{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c},
        []byte{0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34},
        []byte{0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32},
    },
    {
        // Appendix C.1.  AES-128
        []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
        []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
        []byte{0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a},
    },
    {
        // Appendix C.2.  AES-192
        []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
        },
        []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
        []byte{0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91},
    },
    {
        // Appendix C.3.  AES-256
        []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
        },
        []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
        []byte{0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89},
    },
}

func main() {
    for i, tt := range encryptTests {
        c, err := aes.NewCipher(tt.key)
        if err != nil {
            fmt.Printf("NewCipher(%d bytes) = %s", len(tt.key), err)
            continue
        }
        plain := make([]byte, len(tt.in))
        c.Decrypt(plain, tt.out) //解密tt.out,並將值寫到plain
        for j, v := range plain { //對比plain的值和tt.in是否相同,不一樣則報錯
            if v != tt.in[j] {
                fmt.Printf("decryptBlock %d: plain[%d] = %#x, want %#x", i, j, v, tt.in[j])
                break
            }
        }
    }
}
相關文章
相關標籤/搜索