簡體   English   中英

用 Go 加密 AES 字符串並用 Crypto-js 解密

[英]Encrypt AES string with Go and decrypt with Crypto-js

我在我的 Go 應用程序中尋找加密字符串,並使用 Crypto-js 解密編碼的字符串。

我已經嘗試了幾個小時都沒有成功,嘗試了 Stackoverflow、github 或 gist 提供的許多解決方案。

如果有人有解決方案,他們會讓我免於精神崩潰,哈哈

我的Go加密密碼:

func EncryptBody() (encodedmess string, err error) {
  key := []byte("6368616e676520746869732070617373")

  // Create the AES cipher
  block, err := aes.NewCipher(key)
  if err != nil {
      panic(err)
  }
  plaintext, _ := pkcs7Pad([]byte("exampletext"), block.BlockSize())
  // The IV needs to be unique, but not secure. Therefore it's common to
  // include it at the beginning of the ciphertext.
  ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  iv := ciphertext[:aes.BlockSize]
  if _, err := io.ReadFull(rand.Reader, iv); err != nil {
    panic(err)
  }
  bm := cipher.NewCBCEncrypter(block, iv)
  bm.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)

  return fmt.Sprintf("%x", ciphertext), nil
 }

我的 pkcs7Pad function:

 func pkcs7Pad(b []byte, blocksize int) ([]byte, error) {
  if blocksize <= 0 {
      return nil, errors.New("invalid blocksize")
  }
  if b == nil || len(b) == 0 {
      return nil, errors.New("invalid PKCS7 data (empty or not padded)")
  }
  n := blocksize - (len(b) % blocksize)
  pb := make([]byte, len(b)+n)
  copy(pb, b)
  copy(pb[len(b):], bytes.Repeat([]byte{byte(n)}, n))
  return pb, nil
}

我的 Crypto-JS 解密代碼:

public decryptData() {
  const data = "3633fbef042b01da5fc4b69d8f038a83130994a898137bb0386604cf2c1cbbe6"
  
  const key = "6368616e676520746869732070617373"
  const decrypted = CryptoJS.AES.decrypt(data, key, {
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })

  console.log("Result : " + decrypted.toString(CryptoJS.enc.Hex))
  return decrypted.toString(CryptoJS.enc.Hex)
 }

感謝@Topaco 的幫助!

解決方案:

Go 代碼:

func EncryptBody(data string) (encodedmess string, err error) {
  key := []byte("6368616e676520746869732070617373")

  // Create the AES cipher
  block, err := aes.NewCipher(key)
  if err != nil {
      panic(err)
  }
  plaintext, _ := pkcs7Pad([]byte(data), block.BlockSize())
  // The IV needs to be unique, but not secure. Therefore it's common to
  // include it at the beginning of the ciphertext.
  ciphertext := make([]byte, aes.BlockSize+len(plaintext))
  iv := ciphertext[:aes.BlockSize]
  if _, err := io.ReadFull(rand.Reader, iv); err != nil {
      panic(err)
  }
  bm := cipher.NewCBCEncrypter(block, iv)
  bm.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)


  return fmt.Sprintf("%x", ciphertext), nil

 }

節點代碼:

protected decryptData(data: string) {

  const iv = CryptoJS.enc.Hex.parse(data.substr(0,32))
  const ct = CryptoJS.enc.Hex.parse(data.substr(32))
  const key = CryptoJS.enc.Utf8.parse("6368616e676520746869732070617373")
  // @ts-ignore !!!!!!!! IMPORTANT IF YOU USE TYPESCRIPT COMPILER
  const decrypted = CryptoJS.AES.decrypt({ciphertext: ct}, key, {
    mode: CryptoJS.mode.CBC,
    iv: iv
  })

  console.log("Result : " + decrypted.toString(CryptoJS.enc.Utf8))
  return decrypted.toString(CryptoJS.enc.Utf8)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM