首頁  >  文章  >  後端開發  >  如何在 Go 中實作 CryptoJS.AES.decrypt

如何在 Go 中實作 CryptoJS.AES.decrypt

WBOY
WBOY轉載
2024-02-05 21:45:031006瀏覽

如何在 Go 中实现 CryptoJS.AES.decrypt

問題內容

我使用 JavaScript 中的 CryptoJS 使用給定金鑰進行「簡單」AES 解密

#
var CryptoJS = require('crypto-js');

let encrypted = 'U2FsdGVkX19dGrnVQb3BYZ9SWtQBCt2XZDeWFBEfWoi3qnSHg26aQnADUKjTDtSgpPUDzXc59XrIBsTZp9IRg4dYy9Lo0/GjSVn8qlINhE8NJXozQvUNPJSs6nvWSiivCsh9KHGsWFEusMeTefM9V2hKRpQHnFp5VHDLPgGImjHXOQO71eO4qFoQ3ESqUdnDO94nlTjcJU8AP+GabRMjHP08OVz87uyV4SfvT7AZhAYuOPOumHbJaV5nHz2ouqBZgXR3OK07AUk6V+Faz43BT3Qd2+xGLyz8C+teekmudVhmGlnzvMK/VUs0bNDUttdVVnkz/VbXVOj++07uhDsJoMKzZQYTNLkIl9HnuXDmhKOrLSYs2EYpNrH9T/2ZP3nwqJaI7bbLBCfCpNFGNLY4jyNou4Bl5GEK6sxg0zraUCZTgvfuuvBpKWFcFhPvYf8eJGoI3dOWTRWyRnS6e4j3bg==';
let key = 'KBlgcIOrad7EYpiz9LNNTMSg98QoqSVK';

var decrypted = CryptoJS.AES.decrypt(encrypted, key);

let data = decrypted.toString(CryptoJS.enc.Utf8);

console.log(data);

我試圖在 Go (Golang) 中解密該數據,但失敗了。我對 AES 一無所知,這是我到目前為止的程式碼

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "encoding/base64"
    "fmt"
)

func main() {
    encrypted := "U2FsdGVkX19dGrnVQb3BYZ9SWtQBCt2XZDeWFBEfWoi3qnSHg26aQnADUKjTDtSgpPUDzXc59XrIBsTZp9IRg4dYy9Lo0/GjSVn8qlINhE8NJXozQvUNPJSs6nvWSiivCsh9KHGsWFEusMeTefM9V2hKRpQHnFp5VHDLPgGImjHXOQO71eO4qFoQ3ESqUdnDO94nlTjcJU8AP+GabRMjHP08OVz87uyV4SfvT7AZhAYuOPOumHbJaV5nHz2ouqBZgXR3OK07AUk6V+Faz43BT3Qd2+xGLyz8C+teekmudVhmGlnzvMK/VUs0bNDUttdVVnkz/VbXVOj++07uhDsJoMKzZQYTNLkIl9HnuXDmhKOrLSYs2EYpNrH9T/2ZP3nwqJaI7bbLBCfCpNFGNLY4jyNou4Bl5GEK6sxg0zraUCZTgvfuuvBpKWFcFhPvYf8eJGoI3dOWTRWyRnS6e4j3bg=="
    key := "KBlgcIOrad7EYpiz9LNNTMSg98QoqSVK"

    e, _ := base64.StdEncoding.DecodeString(encrypted)
    k := []byte(key)

    dst, err := decryptAES(e, k)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(dst))
}

func decryptAES(ciphertext []byte, key []byte) ([]byte, error) {
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, err
    }

    if len(ciphertext) < aes.BlockSize {
        return nil, fmt.Errorf("ciphertext is too short")
    }

    iv := ciphertext[:aes.BlockSize]
    ciphertext = ciphertext[aes.BlockSize:]

    stream := cipher.NewCFBDecrypter(block, iv)
    plaintext := make([]byte, len(ciphertext))
    stream.XORKeyStream(plaintext, ciphertext)

    return plaintext, nil
}

結果:�9�����ff�������������jTY:���SV�PZLDw%�z��0���&� � �j@'��_H��0 ����b�8�b}�;Y���d��0�^P얿L�aC�3�2�m�� d׺ �h� -׸��%Z(0��c�v�r�:�=�>}���b��n������g�lu}������&�3� .�%|M��fxdGW27z�D��J���"(�[D� <��n�z�4:����� ��iZs��ix��G��$��.=��3J��5{"g��#z ����Z��#���@����Xs��# ��ZR;����VI ������ ��w�X/�bp�r ��

快速嘗試:https://goplay.tools/snippet/QooAajJ4ky0

請幫我使用 Go 解密該資料或修復我的程式碼。謝謝<3


正確答案


正如@Topaco在評論中明確提到的那樣,

根據CryptoJS 文件,預設模式為CBC 。所以你的密文是用CBC模式加密的。

但是在您的go程式碼中,您使用了CFB模式,透過使用cipher.NewCFBDecrypter(block, iv)來解密密文,這顯然不是正確的模式。

您必須使用 NewCFBDecrypter /pkg.go.dev/crypto/cipher#NewCB​​CDecrypter" rel="nofollow noreferrer">NewCB​​CDecrypter

我可以使用 https://www.php.cn/link/2c6ee860dfb322d0fc5dbbf5a2aa0b39成功解密> 套件。

<code>package main

import (
    "fmt"
    "github.com/Luzifer/go-openssl"
)

func main() {
    data := "U2FsdGVkX19dGrnVQb3BYZ9SWtQBCt2XZDeWFBEfWoi3qnSHg26aQnADUKjTDtSgpPUDzXc59XrIBsTZp9IRg4dYy9Lo0/GjSVn8qlINhE8NJXozQvUNPJSs6nvWSiivCsh9KHGsWFEusMeTefM9V2hKRpQHnFp5VHDLPgGImjHXOQO71eO4qFoQ3ESqUdnDO94nlTjcJU8AP+GabRMjHP08OVz87uyV4SfvT7AZhAYuOPOumHbJaV5nHz2ouqBZgXR3OK07AUk6V+Faz43BT3Qd2+xGLyz8C+teekmudVhmGlnzvMK/VUs0bNDUttdVVnkz/VbXVOj++07uhDsJoMKzZQYTNLkIl9HnuXDmhKOrLSYs2EYpNrH9T/2ZP3nwqJaI7bbLBCfCpNFGNLY4jyNou4Bl5GEK6sxg0zraUCZTgvfuuvBpKWFcFhPvYf8eJGoI3dOWTRWyRnS6e4j3bg=="
    secret := "KBlgcIOrad7EYpiz9LNNTMSg98QoqSVK"

    o := openssl.New()

    dec, err := o.DecryptBytes(secret, []byte(data))
    if err != nil {
        fmt.Printf("An error occurred: %s\n", err)
    }

    fmt.Printf("Decrypted text: %s\n", string(dec))
}

</code>

輸出

Decrypted text: [{"file":"https://eno.tendoloads.com/_v6/71f87b4028d27b3ba749bd2029f3248245618a740ca81a9a9863f257784436f85c939482f4d306945639b935dc612f232173cae4f207297dea8798f69741cdadcf03986938ae645355b02ac49101bd99d26dbcacac3e6ab00b678324a21474728d09a70cb4b5086fbc36943efb9f1695c522b23382b639d8f473c8ce9a528151/master.m3u8","type":"hls"}]

如果你想在CryptoJS中使用CFB,那麼你必須明確指定模式。

var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
  mode: CryptoJS.mode.CFB,
});

以上是如何在 Go 中實作 CryptoJS.AES.decrypt的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除