go CBC 加密【AES 和DES的運用】


關於AES cbc的加密 在go AES 加密 和解密已經調到了, 這次主要涉及的內同時cbc 和ecb,剛好網上也有相應的說明, 我就一起整理一下

DES是以64比特的明文為一個單位來進行加密,並生成64比特的密文。由於它每次只能處理特定長度的一塊數據,所以DES屬於分組密碼算法。cypto/des包提供了有關des加密的功能。

模式

由於分組密碼算法只能加密固定長度的分組,所以當加密的明文超過分組密碼的長度時,就需要對分組密碼算法進行迭代,而迭代的方法就稱為分組密碼的模式。模式主要有ECB(電子密碼本)、CBC(密碼分組鏈接模式)、CTR(計數器模式)、OFB(輸出反饋模式)、CFB(密碼反饋模式)五種。下面簡單介紹下前兩種:

  1. ECB(electronic code book)是最簡單的方式,它將明文分組加密后的結果直接成為密文分組。
    優缺點:模式操作簡單;明文中的重復內容將在密文中表現出來,特別對於圖像數據和明文變化較少的數據;適於短報文的加密傳遞。

  2. CBC(cipher block chaining)的原理是加密算法的輸入是當前的明文分組和前一密文分組的異或,第一個明文分組和一個初始向量進行異或,這樣同一個明文分組重復出現時會產生不同的密文分組。
    特點:同一個明文分組重復出現時產生不同的密文分組;加密函數的輸入是當前的明文分組和前一個密文分組的異或;每個明文分組的加密函數的輸入與明文分組之間不再有固定的關系;適合加密長消息。

填充方式

在按8個字節對DES進行加密或解密時,如果最后一段字節不足8位,就需要對數據進行補位。即使加密或解密的數據剛好是8的倍數時,也會再補8位。舉個栗子,如果末尾剛好出現1,這時你就無法判斷這個1是原來數據,還是經過補位得到的1。因此,可以再補8位進行標識。填充方式主要有以下幾種:pkcs7padding、pkcs5padding、zeropadding、iso10126、ansix923。

  1. pkcs7padding和pkcs5padding的填充方式相同,填充字節的值都等於填充字節的個數。例如需要填充4個字節,則填充的值為"4 4 4 4"。
  2. zeropadding填充字節的值都為0。

密碼

DES的密鑰長度是64比特,但由於每隔7個比特會設置一個用於錯誤檢測的比特,因此其實質密鑰長度為56比特。

偏移量

上面模式中,例如CBC,再加密第一個明文分組時,由於不存在“前一個密文分組”,因此需要事先准備一個長度為一個分組的比特序列來代替“前一個密文分組”,這個比特序列成為初始化向量,也稱偏移量,通常縮寫為IV。一般來說,每次加密時都會隨機產生一個不同的比特序列來作為初始化向量。偏移量的長度必須和塊的大小相同。

輸出

加密后的字節在顯示時可以進行hex和base64編碼,hex是十六進制編碼,base64是一種基於64個可打印字符來標識二進制數據的方法。

下面以上面提到的幾種模式和填充方式為例,進行演示如何在代碼中使用。

加密模式采用ECB、填充方式采用pkcs5padding、密碼使用"12345678",輸出時經hex編碼。自己可以通過一些在線測試工具進行測試,看結果是否一致。

一定要注意DES CBC的key長度為8, AES CBC 的key 為16,24,32

package utils
 
import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/des"
    "encoding/hex"
    "fmt"
)
 
/*
DES CBC加密
key的長度為8個字節, iv必須相同長度
*/
func EncryptDES_CBC(src, key, iv string) string {
    data := []byte(src)
    keyByte := []byte(key)
    block, err := des.NewCipher(keyByte)
    if err != nil {
        panic(err)
    }
    data = PKCS5Padding(data, block.BlockSize())
    //獲取CBC加密模式
    //iv := keyByte //用密鑰作為向量(不建議這樣使用)
    ivByte := []byte(iv)
    mode := cipher.NewCBCEncrypter(block, ivByte)
    out := make([]byte, len(data))
    mode.CryptBlocks(out, data)
    return fmt.Sprintf("%X", out)
}
 
//DESC CBC解密
func DecryptDES_CBC(src, key, iv string) string {
    keyByte := []byte(key)
    data, err := hex.DecodeString(src)
    if err != nil {
        panic(err)
    }
    block, err := des.NewCipher(keyByte)
    if err != nil {
        panic(err)
    }
    ivBye := []byte(iv)
    mode := cipher.NewCBCDecrypter(block, ivBye)
    plaintext := make([]byte, len(data))
    mode.CryptBlocks(plaintext, data)
    plaintext = PKCS5UnPadding(plaintext)
    return string(plaintext)
}
 
//ECB加密
func EncryptDES_ECB(src, key string) string {
    data := []byte(src)
    keyByte := []byte(key)
    block, err := des.NewCipher(keyByte)
    if err != nil {
        panic(err)
    }
    bs := block.BlockSize()
    //對明文數據進行補碼
    data = PKCS5Padding(data, bs)
    if len(data)%bs != 0 {
        panic("Need a multiple of the blocksize")
    }
    out := make([]byte, len(data))
    dst := out
    for len(data) > 0 {
        //對明文按照blocksize進行分塊加密
        //必要時可以使用go關鍵字進行並行加密
        block.Encrypt(dst, data[:bs])
        data = data[bs:]
        dst = dst[bs:]
    }
    return fmt.Sprintf("%X", out)
}
 
//ECB解密
func DecryptDES_ECB(src, key string) string {
    data, err := hex.DecodeString(src)
    if err != nil {
        panic(err)
    }
    keyByte := []byte(key)
    block, err := des.NewCipher(keyByte)
    if err != nil {
        panic(err)
    }
    bs := block.BlockSize()
    if len(data)%bs != 0 {
        panic("crypto/cipher: input not full blocks")
    }
    out := make([]byte, len(data))
    dst := out
    for len(data) > 0 {
        block.Decrypt(dst, data[:bs])
        data = data[bs:]
        dst = dst[bs:]
    }
    out = PKCS5UnPadding(out)
    return string(out)
}
 
/*
    key參數的長度 iv必須相同長度
    16 字節 - AES-128
    24 字節 - AES-192
    32 字節 - AES-256
*/
func EncryptAES_CBC(src, key, iv string) string {
    data := []byte(src)
    keyByte := []byte(key)
    block, err := aes.NewCipher(keyByte)
    if err != nil {
        panic(err)
    }
    data = PKCS5Padding(data, block.BlockSize())
    //獲取CBC加密模式
    //iv := keyByte //用密鑰作為向量(不建議這樣使用)
    ivByte := []byte(iv)
    mode := cipher.NewCBCEncrypter(block, ivByte)
    out := make([]byte, len(data))
    mode.CryptBlocks(out, data)
    return fmt.Sprintf("%X", out)
}
 
//AES CBC解密
func DecryptAES_CBC(src, key, iv string) string {
    keyByte := []byte(key)
    data, err := hex.DecodeString(src)
    if err != nil {
        panic(err)
    }
    block, err := aes.NewCipher(keyByte)
    if err != nil {
        panic(err)
    }
    //iv := keyByte //用密鑰作為向量(不建議這樣使用)
    ivBye := []byte(iv)
    mode := cipher.NewCBCDecrypter(block, ivBye)
    plaintext := make([]byte, len(data))
    mode.CryptBlocks(plaintext, data)
    plaintext = PKCS5UnPadding(plaintext)
    return string(plaintext)
}
 
//明文補碼算法
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
    padding := blockSize - len(ciphertext)%blockSize
    padtext := bytes.Repeat([]byte{byte(padding)}, padding)
    return append(ciphertext, padtext...)
}
 
//明文減碼算法
func PKCS5UnPadding(origData []byte) []byte {
    length := len(origData)
    unpadding := int(origData[length-1])
    return origData[:(length - unpadding)]
}

第三方包

github.com/marspere/goencrypt包實現了多種加密算法,包括對稱加密和非對稱加密等。

package main
 
import (
    "fmt"
 
    "github.com/marspere/goencrypt"
)
 
func main() {
    // key為12345678
    // iv為空
    // 采用ECB分組模式
    // 采用pkcs7padding填充模式
    // 輸出結果使用base64進行加密
    cipher := goencrypt.NewDESCipher([]byte("12345678"), []byte(""), goencrypt.ECBMode, goencrypt.Pkcs7, goencrypt.PrintBase64)
    cipherText, err := cipher.DESEncrypt([]byte("hello world"))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(cipherText)
    plainText, err := cipher.DESDecrypt(cipherText)
    fmt.Println(plainText)
}

 ---------------------2021-4-21-------------

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

// PKCS7加填充/和PKCS5填充一樣,只是填充字段多少的區別
func PKCS7Padding(cipherText []byte, blockSize int) []byte {
   padding := blockSize - len(cipherText)%blockSize
   padText := bytes.Repeat([]byte{byte(padding)}, padding)
   return append(cipherText, padText...)
}

// PKCS7解填充/和PKCS5填充一樣,只是填充字段多少的區別
func PKCS7UnPadding(encrypt []byte) []byte {
   length := len(encrypt)
   unPadding := int(encrypt[length-1])
   return encrypt[:(length - unPadding)]
}

// AES/ECB/PKCS7模式加密--簽名加密方式
func AesECBEncrypt(data, key []byte) ([]byte, error) {
   block, err := aes.NewCipher(key)
   if err != nil {
      return []byte{}, err
   }
   ecb := NewECBEncryptEr(block)
   // 加PKCS7填充
   content := PKCS7Padding(data, block.BlockSize())
   encryptData := make([]byte, len(content))
   // 生成加密數據
   ecb.CryptBlocks(encryptData, content)
   return encryptData, nil
}

// AES/ECB/PKCS7模式解密--簽名解密方式
func AesECBDecrypt(data, key []byte) ([]byte, error) {
   block, err := aes.NewCipher(key)
   if err != nil {
      return []byte{}, err
   }
   ecb := NewECBDecryptEr(block)
   retData := make([]byte, len(data))
   ecb.CryptBlocks(retData, data)
   // 解PKCS7填充
   retData = PKCS7UnPadding(retData)
   return retData, nil
}



type ecb struct {
   b         cipher.Block
   blockSize int
}

func newECB(b cipher.Block) *ecb {
   return &ecb{
      b:         b,
      blockSize: b.BlockSize(),
   }
}

type ecbEncryptEr ecb

func NewECBEncryptEr(b cipher.Block) cipher.BlockMode {
   return (*ecbEncryptEr)(newECB(b))
}

func (x *ecbEncryptEr) BlockSize() int { return x.blockSize }

func (x *ecbEncryptEr) CryptBlocks(dst, src []byte) {
   if len(src)%x.blockSize != 0 {
      panic("crypto/cipher: input not full blocks")
   }
   if len(dst) < len(src) {
      panic("crypto/cipher: output smaller than input")
   }
   for len(src) > 0 {
      x.b.Encrypt(dst, src[:x.blockSize])
      src = src[x.blockSize:]
      dst = dst[x.blockSize:]
   }
}

// ecb解密方法
type ecbDecryptEr ecb

func NewECBDecryptEr(b cipher.Block) cipher.BlockMode {
   return (*ecbDecryptEr)(newECB(b))
}

func (x *ecbDecryptEr) BlockSize() int { return x.blockSize }

func (x *ecbDecryptEr) CryptBlocks(dst, src []byte) {
   if len(src)%x.blockSize != 0 {
      panic("crypto/cipher: input not full blocks")
   }
   if len(dst) < len(src) {
      panic("crypto/cipher: output smaller than input")
   }
   for len(src) > 0 {
      x.b.Decrypt(dst, src[:x.blockSize])
      src = src[x.blockSize:]
      dst = dst[x.blockSize:]
   }
}

java實現:

public static String encode(String reqstr, String reqkey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
    SecretKeySpec keySpec = new SecretKeySpec(reqkey.getBytes("UTF-8"), "AES");
    cipher.init(1, keySpec);
    byte[] result = cipher.doFinal(reqstr.getBytes("UTF-8"));
    return new String(Base64.encode(result));
}
public static String decodeByAES(String reskey, String resstr) throws Exception {
    byte[] responseByte = Base64.decode(resstr);
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
    SecretKeySpec keySpec = new SecretKeySpec(hex2bytes(reskey), "AES");
    cipher.init(2, keySpec);
    byte[] decoded = cipher.doFinal(responseByte);
    return new String(decoded, "UTF-8");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM