Go 中打字稿解密中的 AES 加密

我正在尝试加密 TypeScript 中的数据,并将加密输出传递给 Go 中的解密函数,但 Go 中的输出与 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));

这是我的 Go 代码:


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

}


慕神8447489
浏览 103回答 1
1回答

慕森王

请使用相同的密钥“7061737323313233”。使用相同的静脉注射。十二月全文。func main() {&nbsp; &nbsp; d2, err := Decrypt("VMlk9qzp2BKZi5wZjlzst2iwEre0uD/VHVc6xm2bhXY=", "7061737323313233")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(d2)}// Decrypt decrypts cipher text string into plain text stringfunc Decrypt(encrypted string, CIPHER_KEY string) (string, error) {&nbsp; &nbsp; key := []byte(CIPHER_KEY)&nbsp; &nbsp; cipherText, _ := base64.StdEncoding.DecodeString(encrypted) ////hex.DecodeString(encrypted)&nbsp; &nbsp; block, err := aes.NewCipher(key)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; if len(cipherText) < aes.BlockSize {&nbsp; &nbsp; &nbsp; &nbsp; panic("cipherText too short")&nbsp; &nbsp; }&nbsp; &nbsp; // iv := cipherText[:aes.BlockSize]&nbsp; &nbsp; iv := []byte("7061737323313233")&nbsp; &nbsp; cipherText = cipherText[:]&nbsp; &nbsp; if len(cipherText)%aes.BlockSize != 0 {&nbsp; &nbsp; &nbsp; &nbsp; panic("cipherText is not a multiple of the block size")&nbsp; &nbsp; }&nbsp; &nbsp; // cipherText, _ = Pad(cipherText, aes.BlockSize)&nbsp; &nbsp; mode := cipher.NewCBCDecrypter(block, iv)&nbsp; &nbsp; mode.CryptBlocks(cipherText, cipherText)&nbsp; &nbsp; return fmt.Sprintf("%s", cipherText), nil}
打开App,查看更多内容
随时随地看视频慕课网APP