Golang AES-CBC 256使用CryptoJS解密

时间:2014-04-29 19:34:07

标签: go cryptography aes cryptojs

已经工作了几天试图让Golang AES-CBC工作(反之亦然),我修复了大部分错误但没有得到解密,即使我已经确认密钥,iv,密文在两者上都是相同的结束。

必须有人知道,网上任何地方都没有工作示例......

//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 个答案:

答案 0 :(得分:6)

在有条不紊地尝试所有可能的AES配置后,我现在可以解密我的文本..

...在本例中使用空白iv(“AAAAAAAAAAAAAAAAAAAAAA ==”)。如果您使用另一个,它将成为加密时的第一个明文块...

Go> CryptoJS

//去

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");
}