亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

Go
慕神8447489 2023-07-31 17:16:04
我正在嘗試加密 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 stringfunc 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}
查看完整描述

1 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

  1. 請使用相同的密鑰“7061737323313233”。

  2. 使用相同的靜脈注射。

  3. 十二月全文。

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

}


查看完整回答
反對 回復 2023-07-31
  • 1 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號