登录
首页 >  Golang >  Go问答

Go中使用AES加密并在Crypto-js中解密字符串

来源:stackoverflow

时间:2024-02-17 16:39:24 337浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《Go中使用AES加密并在Crypto-js中解密字符串》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

我正在我的 go 应用程序中寻找加密字符串,并使用 crypto-js 解密编码的字符串。

我已经尝试了几个小时但没有成功,尝试了 stackoverflow、github 或 gist 提供的许多解决方案。

如果有人有解决方案,他们会把我从某种神经崩溃中拯救出来,哈哈

my 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功能:

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 的帮助!

解决方案:

转到代码:

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

 }

nodejs 代码:

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)
}

终于介绍完啦!小伙伴们,这篇关于《Go中使用AES加密并在Crypto-js中解密字符串》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>