首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Go加密AES字符串,用Crypto-js解密

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

Stack Overflow用户
提问于 2021-02-16 17:01:31
回答 1查看 138关注 0票数 0

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

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

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

我的Go加密码:

代码语言:javascript
复制
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函数:

代码语言:javascript
复制
 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解密代码:

代码语言:javascript
复制
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)
 }
EN

回答 1

Stack Overflow用户

发布于 2021-02-16 21:33:28

感谢@Topaco的帮助!

解决方案:

Go代码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66221371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档