问题内容
我使用 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) 登录后复制
结果:�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�+
快速尝试:https://goplay.tools/snippet/QooAajJ4ky0
请帮助我使用 Go 解密该数据或修复我的代码。谢谢
正确答案
正如@Topaco在评论中明确提到的那样,
根据CryptoJS
文档,默认模式为CBC
。所以你的密文是用CBC
模式加密的。
但是在您的go
代码中,您使用了CFB
模式,通过使用cipher.NewCFBDecrypter(block, iv)
来解密密文,这显然不是正确的模式。
您必须使用 NewCFBDecrypter /pkg.go.dev/crypto/cipher#NewCBCDecrypter" rel="nofollow noreferrer">NewCBCDecrypter
我可以使用 https://www.php.cn/link/2c6ee860dfb322d0fc5dbbf5a2aa0b39成功解密> 包。
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: %sn", err)
}
fmt.Printf("Decrypted text: %sn", string(dec))
}
登录后复制
输出
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的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!