gpt4 book ai didi

typescript - Go中 typescript 解密中的AES加密

转载 作者:行者123 更新时间:2023-12-01 22:27:55 24 4
gpt4 key购买 nike

我正在尝试在 typescript 中加密数据并将加密输出传递给 Go 中的解密函数,但是 Go 中的输出与 typescript 中的输入不匹配,那么问题是什么?

这是我的 typescript 代码:

import * as CryptoJS from 'crypto-js';
var key = CryptoJS.enc.Utf8.parse('7061737323313233');
var iv = CryptoJS.enc.Utf8.parse('7061737323313233');
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse("Im new in aes encryption"), key,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});

var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});

console.log('Encrypted :' + encrypted);
console.log('Key :' + encrypted.key);
console.log('Salt :' + encrypted.salt);
console.log('iv :' + encrypted.iv);
console.log('Decrypted : ' + decrypted);
console.log('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8));

这是我的围棋代码:

func main() {
d2, _ := Decrypt("VMlk9qzp2BKZi5wZjlzst2iwEre0uD/VHVc6xm2bhXY=", "37303631373337333233333133323333")
fmt.Println(d2)
}
// Decrypt decrypts cipher text string into plain text string
func Decrypt(encrypted string, CIPHER_KEY string) (string, error) {
key := []byte(CIPHER_KEY)
cipherText, _ := base64.StdEncoding.DecodeString(encrypted) ////hex.DecodeString(encrypted)

block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}

if len(cipherText) < aes.BlockSize {
panic("cipherText too short")
}
iv := cipherText[:aes.BlockSize]

cipherText = cipherText[aes.BlockSize:]
if len(cipherText)%aes.BlockSize != 0 {
panic("cipherText is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(cipherText, cipherText)

cipherText, _ = pkcs7.Pad(cipherText, aes.BlockSize)
return fmt.Sprintf("%s", cipherText), nil
}

最佳答案

  • 请使用相同的 key “7061737323313233”。
  • 使用相同的 iv。
  • 十二月全文。
  • func main() {

    d2, err := Decrypt("VMlk9qzp2BKZi5wZjlzst2iwEre0uD/VHVc6xm2bhXY=", "7061737323313233")
    if err != nil {
    log.Println(err)
    return
    }
    fmt.Println(d2)

    }

    // Decrypt decrypts cipher text string into plain text string
    func Decrypt(encrypted string, CIPHER_KEY string) (string, error) {
    key := []byte(CIPHER_KEY)
    cipherText, _ := base64.StdEncoding.DecodeString(encrypted) ////hex.DecodeString(encrypted)

    block, err := aes.NewCipher(key)
    if err != nil {
    panic(err)
    }

    if len(cipherText) < aes.BlockSize {
    panic("cipherText too short")
    }
    // iv := cipherText[:aes.BlockSize]
    iv := []byte("7061737323313233")

    cipherText = cipherText[:]
    if len(cipherText)%aes.BlockSize != 0 {
    panic("cipherText is not a multiple of the block size")
    }

    // cipherText, _ = Pad(cipherText, aes.BlockSize)

    mode := cipher.NewCBCDecrypter(block, iv)
    mode.CryptBlocks(cipherText, cipherText)

    return fmt.Sprintf("%s", cipherText), nil
    }

    关于typescript - Go中 typescript 解密中的AES加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58729774/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com