CBC解密器解密了加密文本,但其中一些文本被随机字符替换了。

huangapple go评论83阅读模式
英文:

CBC Decrypter decrypts the encrypted text but some of the text is replaced with random characters

问题

假设我有这个Base64加密的文本:(请理解这只是测试数据)

0Ns8cv7CHb0Pn/L1RPQEJjAaPJbBuhJxf9RCm3k/qG6DoNgd06jSgUwiaM/ym1Xc9DOBMyZvMNZQKwv/cgr9WGP4ogmqNguaM/zIOIh/GpMGCbTh+5UmP3x+La5eQKCmhim+nxVIBVzcaepUGUyjyPncu2RPQB8maA2AjgmqFb2tk3rcRozj8rTjcQcykxk2i7omhmt1FHBj4YBJoT4oc6B9bCUj8nhIZPCOj8T1z7A3MvZJwSmMa5rFnnozMCQ3no7lxORJQnN9d7gkLge1BAGwrxbXZapkuBx/juqTK3sJ46oiPDCdB0IzZ6Q1PZTrJPcWghOlQwfvHfKgu8kHtW2y2QskP+1uzVQBainXxcCHX7ZBq7Vhdl7n3WQjjok3P/A/6ELJa6qI9eL/NTfEavr7m0PVyrm6Zci4piK2Gjdz/LJZWEKbX6/wUq3pnDClQXAv1DV6g8P46H0p2ZCb0/WsbyZmgxuWYMx/KV8236IUQwY4ylyxnhCHpgHZAeM/CC9qgnMw6zjEtud+uWCx3iWZpQdOmxvaEVMQOQFte/PkUkXgH/9Z5nqJWoftKRcQSyC2+gl5ORgjA1GjbFcB5cr36DFj4su+whgKfyDHtCVVqFK+lIkcu2uLJQX7MwImuvKEAY48raCt8eE3qj2dfjK+dNhOfB8TbU4qAsRTVh7FtD0Hfto7sdVRLWAZRnqo7tHqD0R1LWkXg/VhxFGMnJkUxdJonlEFYLadUTI2PIr4acnpHcr6P91dTU+onXdFaYumeJrayrxBAujmXmAzZki+KrYyviORMlxmpK87ZJ2jP0psRx0hD+SlnHnX3o5nJVMCEPy1Gh9lbU3F4O2PtZ5edc7GEq8TFMoAXtRqL3qg2m/vfyrgzjbU9127pHvk4HNpF0ow9PlC3oi827npONncormAml5Ii+4sz5KF8aN1JEiqt5jvrRnbAOOcOCdCysl1jyDyapchdxcHKk/wmzFVrykkaehfoYYDIAMqzPaBCmGFjZ53eXwvjP2XrKaJDvS2DbIkHnT+G0/lv58fYzAGwDlyxqA/oetN+Y5WolmS6gJVaws4qSYKaqVmIdz4pP7xOQNP22qnnjAoB49A0aKpQfGkL3W8zppB3Qa3rtvK0piPemX3HbNLAdKFFBEh1+AEbYNyUE/EjdkkVEwIDayw6L3WurLTytPX+05kKSkfG9AmJzqYmd1y50UQ5GxQu7V65nRL73LBoVWxkUO/T+rnyILmQ8dh91HuMgXdLlH41gfiRzk4axZr7LlSnsXVODoBYUHLOZ273WtFnixmKYCyzdI7gPnwN/BPBpVScpymKO0RZcQjFEp3TBOpF14W0+2RqVl/d+OSmXzeA3j7yvZVeUU59KNSMZzebBI4U9kTu0koHDBJzNHItRkrqsjw3NobpK6JvWjWXatPU9ytJ0gX2smtWGuuzjr/asV0X4omKmovOCllMYh+xRICkSIYB8C6rZ/a8ToVusmA62E6W+61t27e5wN4TCrtq9w3e12kNhZ73T/l2GHKa1gEqV6hBFm/VBEgxdiCtJeerqkbuotmJVMzFpJ+s+NM+xfw2w8uyKUjJFX75nwwI4idhbTl

我有这个IV:db64fa140a888b41

还有这个密钥:62448f7b7128e8b55224dd667b7c1a7e

如果我使用这个网站https://www.devglan.com/online-tools/aes-encryption-decryption来解密它,使用CBC、256位密钥大小和Base64解密,我得到了正确的字符串:

"{\"totaldistance\":7.116600000000001,\"totaltraveltime\":9,\"fare\":{\"vehicle_list\":[{\"name\":\"Electric Limousine\",\"category_image\":\"image-1667907996681.png\",\"seat_count\":\"4\",\"unique_category_id\":6,\"available_for\":\"2\",\"schedule_before\":\"20\",\"schedule_upto\":\"10800\",\"nearest_driver\":{\"fare\":52,\"min_fare\":35},\"schedule_only\":true},{\"name\":\"Limousine\",\"category_image\":\"image-1667908122302.png\",\"seat_count\":\"4\",\"unique_category_id\":7,\"available_for\":\"2\",\"schedule_before\":\"20\",\"schedule_upto\":\"10800\",\"nearest_driver\":{\"fare\":48,\"min_fare\":32},\"schedule_only\":true},{\"name\":\"XL Limousine\",\"category_image\":\"image-1667907858244.png\",\"seat_count\":\"6\",\"unique_category_id\":13,\"available_for\":\"2\",\"schedule_before\":\"20\",\"schedule_upto\":\"10800\",\"nearest_driver\":{\"fare\":59,\"min_fare\":40},\"schedule_only\":true},{\"name\":\"Ameera Limousine\",\"category_image\":\"image-1667907336189.png\",\"seat_count\":\"4\",\"unique_category_id\":16,\"available_for\":\"2\",\"schedule_before\":\"20\",\"schedule_upto\":\"60\",\"nearest_driver\":{\"fare\":48,\"min_fare\":32},\"schedule_only\":true}]}}"

但是每当我使用相同的输入使用我的CBC解密函数时,我得到了这个解密后的文本:

"��(|���
Җ
�Dvltraveltime\\\"9,\\\"fare\\\":{\\\"vehicle_list\\\":[{\\\"name\\\":\\\"Electric Limousine\\\",\\\"category_image\\\":\\\"image-1667907996681.png\\\",\\\"seat_count\\\":\\\"4\\\",\\\"unique_category_id\\\":6,\\\"available_for\\\":\\\"2\\\",\\\"schedule_before\\\":\\\"20\\\",\\\"schedule_upto\\\":\\\"10800\\\",\\\"nearest_driver\\\":{\\\"fare\\\":52,\\\"min_fare\\\":35},\\\"schedule_only\\\":true},{\\\"name\\\":\\\"Limousine\\\",\\\"category_image\\\":\\\"image-1667908122302.png\\\",\\\"seat_count\\\":\\\"4\\\",\\\"unique_category_id\\\":7,\\\"available_for\\\":\\\"2\\\",\\\"schedule_before\\\":\\\"20\\\",\\\"schedule_upto\\\":\\\"10800\\\",\\\"nearest_driver\\\":{\\\"fare\\\":48,\\\"min_fare\\\":32},\\\"schedule_only\\\":true},{\\\"name\\\":\\\"XL Limousine\\\",\\\"category_image\\\":\\\"image-1667907858244.png\\\",\\\"seat_count\\\":\\\"6\\\",\\\"unique_category_id\\\":13,\\\"available_for\\\":\\\"2\\\",\\\"schedule_before\\\":\\\"20\\\",\\\"schedule_upto\\\":\\\"10800\\\",\\\"nearest_driver\\\":{\\\"fare\\\":59,\\\"min_fare\\\":40},\\\"schedule_only\\\":true},{\\\"name\\\":\\\"Ameera Limousine\\\",\\\"category_image\\\":\\\"image-1667907336189.png\\\",\\\"seat_count\\\":\\\"4\\\",\\\"unique_category_id\\\":16,\\\"available_for\\\":\\\"2\\\",\\\"schedule_before\\\":\\\"20\\\",\\\"schedule_upto\\\":\\\"60\\\",\\\"nearest_driver\\\":{\\\"fare\\\":48,\\\"min_fare\\\":32},\\\"schedule_only\\\":true}]}}\"\v\v\v\v\v\v\v\v\v\v"

我正在使用这个解密函数:

func DecryptCBC(key, ciphertext []byte, iv string) (plaintext []byte, err error) {
    var block cipher.Block
	ciphertext, err = b64.StdEncoding.DecodeString(string(ciphertext))
    if block, err = aes.NewCipher(key); err != nil {
        return
    }

    if len(ciphertext) < aes.BlockSize {
        fmt.Printf("ciphertext too short")
        return
    }

    ciphertext = ciphertext[aes.BlockSize:]

    cbc := cipher.NewCBCDecrypter(block,[]byte(iv))
    cbc.CryptBlocks(ciphertext, ciphertext)

    plaintext = ciphertext

    return
}

我做错了什么?

我尝试不传递向量并执行以下操作:

iv := ciphertext[0:aes.BlockSize]

但它返回相同的结果。

加密的文本是从第三方API返回的,该API还从另一个API中提供密钥和令牌。

英文:

Lets say I've this base64 encrypted text: (Please understand this is all test data)

0Ns8cv7CHb0Pn/L1RPQEJjAaPJbBuhJxf9RCm3k/qG6DoNgd06jSgUwiaM/ym1Xc9DOBMyZvMNZQKwv/cgr9WGP4ogmqNguaM/zIOIh/GpMGCbTh+5UmP3x+La5eQKCmhim+nxVIBVzcaepUGUyjyPncu2RPQB8maA2AjgmqFb2tk3rcRozj8rTjcQcykxk2i7omhmt1FHBj4YBJoT4oc6B9bCUj8nhIZPCOj8T1z7A3MvZJwSmMa5rFnnozMCQ3no7lxORJQnN9d7gkLge1BAGwrxbXZapkuBx/juqTK3sJ46oiPDCdB0IzZ6Q1PZTrJPcWghOlQwfvHfKgu8kHtW2y2QskP+1uzVQBainXxcCHX7ZBq7Vhdl7n3WQjjok3P/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/a8ToVusmA62E6W+61t27e5wN4TCrtq9w3e12kNhZ73T/l2GHKa1gEqV6hBFm/VBEgxdiCtJeerqkbuotmJVMzFpJ+s+NM+xfw2w8uyKUjJFX75nwwI4idhbTl

I've this IV: db64fa140a888b41

And this secret key: 62448f7b7128e8b55224dd667b7c1a7e

If I use this site https://www.devglan.com/online-tools/aes-encryption-decryption to decrypt it using CBC, 256 key size and base64 decrypt, I get the correct string which is:

&quot;{\&quot;totaldistance\&quot;:7.116600000000001,\&quot;totaltraveltime\&quot;:9,\&quot;fare\&quot;:{\&quot;vehicle_list\&quot;:[{\&quot;name\&quot;:\&quot;Electric Limousine\&quot;,\&quot;category_image\&quot;:\&quot;image-1667907996681.png\&quot;,\&quot;seat_count\&quot;:\&quot;4\&quot;,\&quot;unique_category_id\&quot;:6,\&quot;available_for\&quot;:\&quot;2\&quot;,\&quot;schedule_before\&quot;:\&quot;20\&quot;,\&quot;schedule_upto\&quot;:\&quot;10800\&quot;,\&quot;nearest_driver\&quot;:{\&quot;fare\&quot;:52,\&quot;min_fare\&quot;:35},\&quot;schedule_only\&quot;:true},{\&quot;name\&quot;:\&quot;Limousine\&quot;,\&quot;category_image\&quot;:\&quot;image-1667908122302.png\&quot;,\&quot;seat_count\&quot;:\&quot;4\&quot;,\&quot;unique_category_id\&quot;:7,\&quot;available_for\&quot;:\&quot;2\&quot;,\&quot;schedule_before\&quot;:\&quot;20\&quot;,\&quot;schedule_upto\&quot;:\&quot;10800\&quot;,\&quot;nearest_driver\&quot;:{\&quot;fare\&quot;:48,\&quot;min_fare\&quot;:32},\&quot;schedule_only\&quot;:true},{\&quot;name\&quot;:\&quot;XL Limousine\&quot;,\&quot;category_image\&quot;:\&quot;image-1667907858244.png\&quot;,\&quot;seat_count\&quot;:\&quot;6\&quot;,\&quot;unique_category_id\&quot;:13,\&quot;available_for\&quot;:\&quot;2\&quot;,\&quot;schedule_before\&quot;:\&quot;20\&quot;,\&quot;schedule_upto\&quot;:\&quot;10800\&quot;,\&quot;nearest_driver\&quot;:{\&quot;fare\&quot;:59,\&quot;min_fare\&quot;:40},\&quot;schedule_only\&quot;:true},{\&quot;name\&quot;:\&quot;Ameera Limousine\&quot;,\&quot;category_image\&quot;:\&quot;image-1667907336189.png\&quot;,\&quot;seat_count\&quot;:\&quot;4\&quot;,\&quot;unique_category_id\&quot;:16,\&quot;available_for\&quot;:\&quot;2\&quot;,\&quot;schedule_before\&quot;:\&quot;20\&quot;,\&quot;schedule_upto\&quot;:\&quot;60\&quot;,\&quot;nearest_driver\&quot;:{\&quot;fare\&quot;:48,\&quot;min_fare\&quot;:32},\&quot;schedule_only\&quot;:true}]}}&quot;

But whenever I use my CBC decrypt function with the same input, I get this decrypted text:

&quot;��(|���\u000bҖ�\b�Dvltraveltime\\\&quot;:9,\\\&quot;fare\\\&quot;:{\\\&quot;vehicle_list\\\&quot;:[{\\\&quot;name\\\&quot;:\\\&quot;Electric Limousine\\\&quot;,\\\&quot;category_image\\\&quot;:\\\&quot;image-1667907996681.png\\\&quot;,\\\&quot;seat_count\\\&quot;:\\\&quot;4\\\&quot;,\\\&quot;unique_category_id\\\&quot;:6,\\\&quot;available_for\\\&quot;:\\\&quot;2\\\&quot;,\\\&quot;schedule_before\\\&quot;:\\\&quot;20\\\&quot;,\\\&quot;schedule_upto\\\&quot;:\\\&quot;10800\\\&quot;,\\\&quot;nearest_driver\\\&quot;:{\\\&quot;fare\\\&quot;:52,\\\&quot;min_fare\\\&quot;:35},\\\&quot;schedule_only\\\&quot;:true},{\\\&quot;name\\\&quot;:\\\&quot;Limousine\\\&quot;,\\\&quot;category_image\\\&quot;:\\\&quot;image-1667908122302.png\\\&quot;,\\\&quot;seat_count\\\&quot;:\\\&quot;4\\\&quot;,\\\&quot;unique_category_id\\\&quot;:7,\\\&quot;available_for\\\&quot;:\\\&quot;2\\\&quot;,\\\&quot;schedule_before\\\&quot;:\\\&quot;20\\\&quot;,\\\&quot;schedule_upto\\\&quot;:\\\&quot;10800\\\&quot;,\\\&quot;nearest_driver\\\&quot;:{\\\&quot;fare\\\&quot;:48,\\\&quot;min_fare\\\&quot;:32},\\\&quot;schedule_only\\\&quot;:true},{\\\&quot;name\\\&quot;:\\\&quot;XL Limousine\\\&quot;,\\\&quot;category_image\\\&quot;:\\\&quot;image-1667907858244.png\\\&quot;,\\\&quot;seat_count\\\&quot;:\\\&quot;6\\\&quot;,\\\&quot;unique_category_id\\\&quot;:13,\\\&quot;available_for\\\&quot;:\\\&quot;2\\\&quot;,\\\&quot;schedule_before\\\&quot;:\\\&quot;20\\\&quot;,\\\&quot;schedule_upto\\\&quot;:\\\&quot;10800\\\&quot;,\\\&quot;nearest_driver\\\&quot;:{\\\&quot;fare\\\&quot;:59,\\\&quot;min_fare\\\&quot;:40},\\\&quot;schedule_only\\\&quot;:true},{\\\&quot;name\\\&quot;:\\\&quot;Ameera Limousine\\\&quot;,\\\&quot;category_image\\\&quot;:\\\&quot;image-1667907336189.png\\\&quot;,\\\&quot;seat_count\\\&quot;:\\\&quot;4\\\&quot;,\\\&quot;unique_category_id\\\&quot;:16,\\\&quot;available_for\\\&quot;:\\\&quot;2\\\&quot;,\\\&quot;schedule_before\\\&quot;:\\\&quot;20\\\&quot;,\\\&quot;schedule_upto\\\&quot;:\\\&quot;60\\\&quot;,\\\&quot;nearest_driver\\\&quot;:{\\\&quot;fare\\\&quot;:47,\\\&quot;min_fare\\\&quot;:31},\\\&quot;schedule_only\\\&quot;:true}]}}\&quot;\u000b\u000b\u000b\u000b\u000b\u000b\u000b\u000b\u000b\u000b\u000b&quot;

I'm using this decrypt function:

func DecryptCBC(key, ciphertext []byte, iv string) (plaintext []byte, err error) {
    var block cipher.Block
	ciphertext, err = b64.StdEncoding.DecodeString(string(ciphertext))
    if block, err = aes.NewCipher(key); err != nil {
        return
    }

    if len(ciphertext) &lt; aes.BlockSize {
        fmt.Printf(&quot;ciphertext too short&quot;)
        return
    }

    ciphertext = ciphertext[aes.BlockSize:]

    cbc := cipher.NewCBCDecrypter(block,[]byte(iv))
    cbc.CryptBlocks(ciphertext, ciphertext)

    plaintext = ciphertext

    return
}

What am I doing wrong?

I have tried not passing the vector and do this:

iv := ciphertext[0:aes.BlockSize]

But it's returning the same result.

The encrypted text is returned from a third party API, which also provides the secret key and token from a different API.

答案1

得分: 0

通过将密文、初始化向量(IV)和密钥输入到相关网站的适当框中,成功解密密文的事实告诉我,IV并没有与密文一起传输,因此解密应该从密文的第一个字节开始,而不是第16个字节。所以删除以下这行代码:

    ciphertext = ciphertext[aes.BlockSize:]

我预计你将会得到正确的答案。因为十六进制编码的IV和密钥是直接使用的,而不是解码后使用,所以每个部分的熵最多只有一半。我不知道只有128位熵的256位AES密钥是否存在任何弱点,但我不希望看到只有64位熵的IV。无论如何,这是由加密方决定的事情,不一定是你可以控制的。

英文:

The fact that the ciphertext is correctly decrypted by entering it, the IV, and the key into the appropriate boxes on the linked website tells me that the IV is not transmitted with the ciphertext, and therefore that decryption should start at the first byte of ciphertext rather than the 16th. So remove the line

    ciphertext = ciphertext[aes.BlockSize:]

and I'd expect you'll get the correct answer. Because the hex-encoded IV and key are used as is rather than hex-decoded, there is at most only half as much entropy in each. I'm not aware of any weakness of having a 256-bit AES key with only 128 bits of entropy but I'd rather not see an IV with only 64 bits of entropy. In any event, this is something that's decided by the encrypting party and not necessarily something you can control.

huangapple
  • 本文由 发表于 2022年12月8日 06:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/74723292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定