go語言對稱加密算法

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/des"
)

// des的CBC加密
// 編寫填充函數, 若是最後一個分組字節數不夠, 填充
// ......字節數恰好合適, 添加一個新的分組
// 填充個的字節的值 == 缺乏的字節的數
func paddingLastGroup(plainText []byte, bloclSize int) []byte {
    // 1. 求出最後一個組中剩餘的字節數 28 % 8 = 3...4  32 % 8 = 4 ...0
    padNum := bloclSize - len(plainText) % bloclSize
    // 2. 建立新的切片, 長度 == padNum, 每一個字節值 byte(padNum)
    char := []byte{byte(padNum)} // 長度1,
    // 切片建立, 並初始化
    newPlain := bytes.Repeat(char, padNum)
    // 3. newPlain數組追加到原始明文的後邊
    newText := append(plainText, newPlain...)
    return newText
}

// 去掉填充的數據
func unPaddingLastGrooup(plainText []byte) []byte {
    // 1. 拿去切片中的最後一個字節
    length := len(plainText)
    lastChar := plainText[length -1]    //
    number := int(lastChar)    // 尾部填充的字節個數
    return plainText[:length - number]
}

// des加密
func desEncrypt(plainText, key []byte) []byte {
    // 1. 建一個底層使用des的密碼接口
    block, err := des.NewCipher(key)
    if err != nil {
        panic(err)
    }
    // 2. 明文填充
    newText := paddingLastGroup(plainText, block.BlockSize())
    // 3. 建立一個使用cbc分組接口
    iv := []byte("12345678")
    blockMode := cipher.NewCBCEncrypter(block, iv)
    // 4. 加密
    cipherText := make([]byte, len(newText))
    blockMode.CryptBlocks(cipherText, newText)
    // blockMode.CryptBlocks(newText, newText)
    return cipherText
}

// des解密
func desDecrypt(cipherText, key []byte) []byte {
    // 1. 建一個底層使用des的密碼接口
    block, err := des.NewCipher(key)
    if err != nil {
        panic(err)
    }
    // 2. 建立一個使用cbc模式解密的接口
    iv := []byte("12345678")
    blockMode := cipher.NewCBCDecrypter(block, iv)
    // 3. 解密
    blockMode.CryptBlocks(cipherText, cipherText)
    // 4. cipherText如今存儲的是明文, 須要刪除加密時候填充的尾部數據
    plainText := unPaddingLastGrooup(cipherText)
    return plainText
}

// aes加密, 分組模式ctr
func aesEncrypt(plainText, key []byte) []byte {
    // 1. 建一個底層使用aes的密碼接口
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    // 2. 建立一個使用ctr分組接口
    iv := []byte("12345678abcdefgh")
    stream := cipher.NewCTR(block, iv)

    // 4. 加密
    cipherText := make([]byte, len(plainText))
    stream.XORKeyStream(cipherText, plainText)

    return cipherText
}

// des解密
func aesDecrypt(cipherText, key []byte) []byte {
    // 1. 建一個底層使用des的密碼接口
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    // 2. 建立一個使用ctr模式解密的接口
    iv := []byte("12345678abcdefgh")
    stream := cipher.NewCTR(block, iv)
    // 3. 解密
    stream.XORKeyStream(cipherText, cipherText)

    return cipherText
}
package main

import "fmt"


// 測試文件
func main() {
    fmt.Println("des 加解密")
    key := []byte("1234abdd")
    src := []byte("特色: 密文沒有規律,  明文分組是和一個數據流進行的按位異或操做, 最終生成了密文")
    cipherText := desEncrypt(src, key)
    plainText := desDecrypt(cipherText, key)
    fmt.Printf("解密以後的數據: %s\n", string(plainText))

    fmt.Println("aes 加解密 ctr模式 ... ")
    key1 := []byte("1234abdd12345678")
    cipherText = aesEncrypt(src, key1)
    plainText = aesDecrypt(cipherText, key1)
    fmt.Printf("解密以後的數據: %s\n", string(plainText))
}
相關文章
相關標籤/搜索