2015-09-10 45 views
0

流的預期端以我project,我已經寫谷歌電子表格腳本使用sjcl破譯加密單元格內容。但我失敗了。JSON.parse()來 - 語法錯誤:在炭2

function encryptCell() { 
    var masterKey = Browser.inputBox('Enter masterKey'); 
    var spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); 
    var cell = spreadSheet.getActiveSheet().getActiveCell(); 
    var input = cell.getValue(); 

    var encJson = sjcl.encrypt(masterKey, input); 
/* 
{"iv":"4psT+LTIh/aT7WWv7Ye7qw==","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"PjSOiia9TCM=","ct":"3hwmBbwQ7y/fsjk="} 
*/ 
    var encStr = JSON.stringify(encJson); 
/* 
"{\"iv\":\"4psT+LTIh/aT7WWv7Ye7qw==\",\"v\":1,\"iter\":1000,\"ks\":128,\"ts\":64,\"mode\":\"ccm\",\"adata\":\"\",\"cipher\":\"aes\",\"salt\":\"PjSOiia9TCM=\",\"ct\":\"3hwmBbwQ7y/fsjk=\"}" 
*/ 
    var encB64 = Utilities.base64Encode(encStr); 
    Browser.msgBox(encB64); 

    var rencStr = Utilities.base64Decode(encB64); 
/* 
34,123,92,34,105,118,92,34,58,92,34,52,112,115,84,43,76,84,73,104,47,97,84,55,87,87,118,55,89,101,55,113,119,61,61,92,34,44,92,34,118,92,34,58,49,44,92,34,105,116,101,114,92,34,58,49,48,48,48,44,92,34,107,115,92,34,58,49,50,56,44,92,34,116,115,92,34,58,54,52,44,92,34,109,111,100,101,92,34,58,92,34,99,99,109,92,34,44,92,34,97,100,97,116,97,92,34,58,92,34,92,34,44,92,34,99,105,112,104,101,114,92,34,58,92,34,97,101,115,92,34,44,92,34,115,97,108,116,92,34,58,92,34,80,106,83,79,105,105,97,57,84,67,77,61,92,34,44,92,34,99,116,92,34,58,92,34,51,104,119,109,66,98,119,81,55,121,47,102,115,106,107,61,92,34,125,34 
*/ 
    var rencJson = JSON.parse(rencStr); 
    var rinput = sjcl.decrypt(masterKey, rencJson); 
    Browser.msgBox(rinput); 
} 

加密是好的,我可以使用this tool解密。 JSON.parse()有什麼不對。

+2

打印出JSON,它看起來像什麼?忘記提及 – Corvusoft

+0

,我已將_sjcl.min.js_內容包含到Google腳本中。 – Nilesh

+0

謝謝@Corvusoft。解決方案是[這裏](http://stackoverflow.com/a/20210010/4417818) – Nilesh

回答

0
var rencStr = Utilities.base64Decode(encB64); 
/* 
34,123,92,34,105,118,92,34,58,92,34,52,112,115,84,43,76,84,73,104,47,97,84,55,87,87,118,55,89,101,55,113,119,61,61,92,34,44,92,34,118,92,34,58,49,44,92,34,105,116,101,114,92,34,58,49,48,48,48,44,92,34,107,115,92,34,58,49,50,56,44,92,34,116,115,92,34,58,54,52,44,92,34,109,111,100,101,92,34,58,92,34,99,99,109,92,34,44,92,34,97,100,97,116,97,92,34,58,92,34,92,34,44,92,34,99,105,112,104,101,114,92,34,58,92,34,97,101,115,92,34,44,92,34,115,97,108,116,92,34,58,92,34,80,106,83,79,105,105,97,57,84,67,77,61,92,34,44,92,34,99,116,92,34,58,92,34,51,104,119,109,66,98,119,81,55,121,47,102,115,106,107,61,92,34,125,34 
*/ 

如果你看一下,那不是JSON。你可能錯過了那個步驟的JSON.stringify();步驟?

,如果它不是JSON你不能解析它。

+0

問題'Utilities.base64Decode(encB64);' – Nilesh

1

Utilities.base64Decode()解碼你有一個額外的步驟,如果你想獲得一個字符串返回可循。如果你看Google Scripts reference它說base64Decode返回一個字節數組而不是一個字符串。看看你的代碼,你的解碼返回

var rencStr = Utilities.base64Decode(encB64); 
/*34,123,92,34,105,118,92,34,58,92,34,52,112,115,84,43,76,84,73,104,47,97,84...*/ 

這是你的Unicode字符串的數字表示。如果您通過Utilities.newBlob(rencStr).getDataAsString()按照參考建議運行返回值,那麼您將擁有可以解析回加密字符串的JSON。