douzhong2954 2014-04-29 19:34
浏览 272

Golang AES-CBC 256使用CryptoJS解密

Been working for days trying to get Golang AES-CBC to CryptoJS working (or vice-versa), I fixed most of the errors but not getting decryption even though i have confirmed the key, iv, ciphertext is the same on both ends.

There must be someone who knows, there is no working example anywhere on the net for this...

//golang

    if a == "test64bytes" {
        output = "AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD"
    }
    // encrypt ajax response
    iv := decodeBase64("AAAAAAAAAAAAAAAAAAAAAA==")
    ciphertext := []byte(output)
    ckey := decodeBase64(string(PLAINkey[0:32]))

    c, err := aes.NewCipher(ckey)
    cfbdec := cipher.NewCBCDecrypter(c, iv)
    plaintext := make([]byte, len(ciphertext))
    cfbdec.CryptBlocks(plaintext, ciphertext)
    crypt := string(encodeBase64(plaintext))
    fmt.Fprintf(res, "%v", crypt)

    fmt.Println(encodeBase64(ckey))
    fmt.Println(encodeBase64(iv))
    fmt.Println(crypt)

// javascript

    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        var enc = {};
                        enc["key"] = CryptoJS.enc.Base64.parse(keyseed.substring(0,32));
                        enc["iv"] = CryptoJS.enc.Base64.parse("AAAAAAAAAAAAAAAAAAAAAA==");
                        enc["ciphertext"] = CryptoJS.enc.Base64.parse(xmlhttp.responseText);
                        enc["salt"] = "";
                        console.log("RESPONSE:", xmlhttp.responseText, atob(xmlhttp.responseText));
                                      // check i'm using same data
                        console.log(CryptoJS.enc.Base64.stringify(enc["key"]));
                        console.log(CryptoJS.enc.Base64.stringify(enc["iv"]));
                        console.log(CryptoJS.enc.Base64.stringify(enc["ciphertext"]));
                        var options = { keySize: 256 / 8, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: enc["iv"] };
                        de = CryptoJS.AES.decrypt(enc, enc["key"], options);
                        document.getElementById(target).innerHTML = de.toString();
                        console.log(de.toString(CryptoJS.enc.Utf8));
                        console.log("DECRYPTION FINISHED");
                    }
  • 写回答

1条回答 默认 最新

  • dtp87205 2014-04-30 03:50
    关注

    After methodically trying all possible AES configurations I can now decrypt my text..

    ...using a blank iv ("AAAAAAAAAAAAAAAAAAAAAA==") for this example. If you use a different one it will become the first block of plaintext when encrypting...

    Go > CryptoJS

    // Go

    plaintext := []byte("THIS NEEDS TO BE MULTIPLE OF BLOCK LENGTH (16) I THINK")
    // encrypt ajax response
    iv := decodeBase64("AAAAAAAAAAAAAAAAAAAAAA==")
    ckey := decodeBase64(string(PLAINkey[0:32]))
    
    c, err := aes.NewCipher(ckey)
    cfbdec := cipher.NewCBCEncrypter(c, iv)
    ciphertext := make([]byte, len(plaintext))
    cfbdec.CryptBlocks(ciphertext, plaintext)
    crypt := string(encodeBase64(ciphertext))
    fmt.Fprintf(res, "%v", crypt)
    

    // JavaScript Ajax

    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var symkey = keyseed.substring(0,32);
        var cipherParams = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Base64.parse(xmlhttp.responseText) });
        var options = { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding, iv: CryptoJS.enc.Base64.parse("AAAAAAAAAAAAAAAAAAAAAA==") };
        de = CryptoJS.AES.decrypt(cipherParams, CryptoJS.enc.Base64.parse(symkey), options);
        document.getElementById(target).innerHTML = de.toString(CryptoJS.enc.Utf8);
        console.log("DECRYPTION FINISHED");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记