将二进制数据写入文件

时间:2014-11-26 23:16:18

标签: javascript node.js ssl-certificate

我有一个安装脚本。此脚本打开一个网站,允许输入值并上传HTTPS证书。

但结果与输入文件不同。这是我解析多部分主体的代码

var parseForm = function (data) {
    var reg = /-----------------------------.{12}(\r\n|\r|\n)Content-Disposition: form-data; name="/
    var sections = data.split(reg)
    var result = {}
    for (sec in sections) {
        var values = sections[sec].split(/\b"(\r\n|\r|\n)(\r\n|\r|\n)/)
        if (/certificate"; filename/.test(values[0])) {
            var key = values[0].replace(/"; filename=".*/i, '')
            var value = values[3].replace(/Content-Type: application\/x-pkcs12(\r\n|\n|\r)+/i, '').replace(/-----------------------------.{12}--(\r\n|\r|\n)/, '')
            result[key] = value
        } else if (values[3]) {
            var key = values[0]
            var value = values[3].replace(/(\r\n|\r|\n)/, '')
            result[key] = value
        }
    }
    return result
}

这就是我用来保存文件的内容:

fs.writeFile(path.join(__dirname, 'server.p12'), parameter[p], function (err) {
    console.error(err)
    console.error('please copy the file to "' + __dirname + '" and name it server.p12')
})

任何想法,为什么输出错误的数据?或者甚至更好,我如何解决它? :)

修改

也许问题是,文件没有被清除上传。这可能吗?

编辑2

Buffer只是用于检查二进制数据的Notepad ++测试的剩余部分:)

编辑3

这是原始证书:

MIIGugIBAzCCBnYGCSqGSIb3DQEHAaCCBmcEggZjMIIGXzCCA8AGCSqGSIb3DQEHAaCCA7EEggOtMIIDqTCCA6UGCyqGSIb3DQEMCgECoIICtjCCArIwHAYKKoZIhvcNAQwBAzAOBAjE3J2mwFcVBgICB9AEggKQvEMeRbebKva6/EiAdzdiFvjF4v5LXPSAL+kn/nLpnjJhpY2jyP+WHXFQD1i32we9SwquiDUiOP22bgQ2rhjoqgFjb4W1mRXdIULHLRMmW4Zy2Xa4tzhV9ddko2Iw/+aOEV6kB701FDGO8dw472eT7fZ9xCshm53hZ08xhbPk3FIZX0e//hc5Nqq/DbpgAg9mQdDaVKyxv4h2G0cH496443lLH1vwJ1US5pI6ErIIl39tacDzqfWt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KEZmr/xLoC2PP8D8BylClkiE1rcQ6n4f8vTwWTRkjkkUu2Q/gL7p7L1LASKbjmXSlu0MXRQX7GiO6veiKP8kBvL49epAot7/lPS8MAhG8Ask2cd1V5Eg5BqEXZ+9KAA5rNg+cpJ+sbU7sV0tix8RAyZneFP9+QQc07upavI28GuO74TMj/jdVEHlzytbff6W9yBc6QXGKEh2FAhhKBy9YRzNs6S1iHYl4/jygCe8irL4972H/8jYXJwfusDpwU28H3ROPlJ15zc+ZaF3I/DYSyq/8SaXaTvCXUuFhsC/lCquNAGyL6/b2Qcj7TsAGyO3WAw/o2WsFFm1RyKcq5sCfPb6hkrKzHcVP0pB+mpmfEOlnvnC2jR+Av1pp10HwBPT02m58poxNWrYdrsDAbzqp3dgQlOv777J6JqQkxdkt9N4wwY0E0L4szZtl6oIKtcK1tpGNVSzJK4bs1nKkLdQWjGIr2uLn8V95MNmBwTFKKiPIzxLMAr8hlTffaH6EWWuO5loVJ283Ojis4iybTeF60l838QVACOJEMtUITaxHEdMo7FFqyVbGgMP/DtDr6IQ/Zb+vN6nE+Yxz3b7pgvV1gTX1M6kAct/1AuYPLDIyFkbFSpyGaNuC30+DMtF8eDYnRDQRooAJN1ZIWmxfQ0rLuG+XqQFhCfotruJdvrPGOVnd7ON7nIjKQcKbvCtRyrWP7cIORPJz/dz0MyDkg1iF2D4YL1MVMDswHzAHBgUrDgMCGgQUsN+sOMkAQFF3MY7tENSKzkHgxhMEFAy/KjFIFw1dtV5wxho5AOgL8LiCAgIH0A==

这是多部分机构:

-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="COUCHDBSERVERHOST"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="COUCHDBSERVERPORT"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="COUCHDBSERVERUSERNAME"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="COUCHDBSERVERPASSWORD"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="PORT"

1234
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="DEFAULTDOMAIN"

asd
-----------------------------7de17e2d670f22
Content-Disposition: form-data; name="certificate"; filename="D:\serpentes\localhost.p12"
Content-Type: application/x-pkcs12

MO+/vQbvv70CAQMw77+9BnYGCSrvv71I77+977+9DQEHAe+/ve+/vQZnBO+/vQZjMO+/vQZfMO+/vQPvv70GCSrvv71I77+977+9DQEHAe+/ve+/vQPvv70E77+9A++/vTDvv70D77+9MO+/vQPvv70GCyrvv71I77+977+9DQEMCgEC77+977+9Au+/vTDvv70C77+9MBwGCirvv71I77+977+9DQEMAQMwDgQI77+93J3vv73vv71XFQYCAgfvv70E77+9Au+/ve+/vUMeRe+/ve+/vSrvv73vv73vv71I77+9dzdiFu+/ve+/ve+/ve+/vUtc77+977+9L++/vSfvv71y77+977+9MmHvv73vv73vv73vv73vv73vv70dcVAPWO+/ve+/vQfvv71LCu+/ve+/vTUiOO+/ve+/vW4ENu+/vRjvv73vv70BY2/vv73vv73vv70V77+9IULvv70tEyZb77+9cu+/vXbvv73vv704Ve+/ve+/vWTvv71iMO+/ve+/ve+/vRFe77+9B++/vTUUMe+/ve+/ve+/vTjvv71n77+977+977+9fe+/vSsh77+977+977+9Z08x77+977+977+977+9UhlfR++/ve+/vRc5Nu+/ve+/vQ3vv71gAg9mQe+/ve+/vVTvv73vv73vv73vv712G0cH77+93rjvv715Sx9b77+9J1US77+977+9OhLvv70I77+9f21p77+977+977+977+977+9aQcY77+9Omrvv71R77+9Q3/vv73vv716DxPvv73Vr3rvv70D77+9X0Hvv73Kge+/vT8XHw3vv73vv70MPi3vv71l77+9KC7vv70GBT7vv73vv70/77+977+977+977+9P0Tvv71zZu+/vToQd8Ou77+9Whsd77+977+9Se+/ve+/ve+/vVJScWBm77+9Oe+/vTk677+977+977+977+9Ve+/vW4Z5Z6HCyXvv73vv73vv73vv70Abl9uDBDvv73vv73vv73vv70N77+977+977+9DEfvv73vv73vv73vv71HRO+/vX8HRUHvv71z77+9Fe+/vUHvv70T77+9OX3vv70p77+977+977+9U+yrn++/vVnvv70177+977+9ZO+/vWsaZe+/vVoaJH7vv71ORu+/vSYFH++/vW1p77+977+977+977+9eO+/vTLvv71A77+9EV7vv706WO+/ve+/vQ12BWrvv73vv70LINGl77+9Xe+/vXcSdjc877+9Ne+/vUvvv71pNzoTa3Dvv71NBxZsYu+/ve+/ve+/ve+/vRUI77+977+9elRb77+977+9CO+/vXrvv71j77+9OO+/vXvvv73nmqjvv70gEjU2dSXvv71QNhjvv73vv73vv70z77+9JQDvv73vv73vv70u77+977+9G1Pvv73vv71fYe+/ve+/vV8NGe+/vQPvv73vv73vv70jUk0iIXLvv73vv70R77+9Xe+/ve+/vR/vv73vv70dD3EadxV877+9alLvv70D77+977+977+90LEpFu+/vRPvv71g77+977+977+9Zi/vv71/77+977+977+9D2Rn77+9NO+/vX/eju+/ve+/ve+/ve+/vVIwbxlcfhPvv71NPSTvv71B77+977+977+977+9fu+/ve+/vStmKO+/ve+/vTAt77+9Ie+/ve+/ve+/vRBB77+9DUM4Xgrvv70R77+9fe+/ve+/ve+/vTEqC++/vQoG77+9Zu+/vTg777+9KUjvv70Q77+9OO+/ve+tq++/vd2VPu+/ve+/vSFY77+9b++/ve+/ve+/ve+/ve+/vRTvv70u77+9IO+/vUDvv70O77+9ZWs3Te+/vQ3vv73vv71UPHPvv73vv73vv70T77+9B++/ve+/vVQCVnnvv70x77+977+9MBMGCSrvv71I77+977+9DQEJFTEGBAQBAAAAMF0GCSsGAQQB77+9NxEBMVAeTgBNAGkAYwByAG8AcwBvAGYAdAAgAFMAdAByAG8AbgBnACAAQwByAHkAcAB0AG8AZwByAGEAcABoAGkAYwAgAFAAcgBvAHYAaQBkAGUAcjBlBgkq77+9SO+/ve+/vQ0BCRQxWB5WAFAAdgBrAFQAbQBwADoAOABkADEANQBhADkAZABhAC0AOQA0ADgAMgAtADQANwAwADEALQA4AGMAOQA0AC0ANAA4AGIAMwA5ADkAMAA1ADkAZgBmAGUw77+9Au+/vQYJKu+/vUjvv73vv70NAQcG77+977+9Au+/vTDvv70C77+9AgEAMO+/vQJ9Bgkq77+9SO+/ve+/vQ0BBwEwHAYKKu+/vUjvv73vv70NAQwBBjAOBAjvv70BI8ugPdaVAgIH0IDvv70CUDLvv73vv73vv71q5qC6CGLvv73vv73vv73vv71877+977+9W2lB77+9zoEc77+977+977+977+9DO+/vW3vv71oNlxxEh1Hb0/vv71KEe+/ve+/ve+/vRLvv70LY++/ve+/vT8B77+9UO+/ve+/vSE177+977+9Ou+/ve+/ve+/ve+/vTwWTRkj77+9RS7vv70P77+9L++/vXsvUu+/vUjvv73vv73vv71077+977+9QxdFBe+/vRoj77+977+977+977+9P++/vQHvv73vv709eu+/vSjvv73vv73vv709LwwCEe+/vQLvv702ce+/vVXvv71IOQbvv70XZ++/vUoADms2D++/ve+/ve+/ve+/vW1O77+9V0ti77+977+9QMmZ77+9FO+/vX5BBzTvv73vv71a77+977+977+9Gu+/ve+/ve+/vTMj77+9N1UQeXPvv73vv73vv71/77+977+977+9FzpBce+/vRId77+9AhhKBy9YRzNs77+9LWId77+9eO+/vTzvv70J77+9Iu+/ve+/vT3vv71h77+977+9NhcnB++/ve+/vTpwU28H77+9E++/ve+/ve+/vXnvv73PmWhd77+977+9NhLKr++/vUnvv73vv71O77+977+9Uu+/vWHvv70v77+9Cu+/ve+/vQBs77+977+977+977+9Qe+/ve+/vU7vv70G77+977+977+9Aw/vv73vv71rBRZtUcinKu+/ve+/ve+/vT3vv73vv73vv73vv73vv70d77+9T9KQfu+/ve+/ve+/vRDvv71n77+9cO+/ve+/vR/vv73vv71aae+/vUHvv70E77+977+977+9bnzvv73vv71NWu+/vR3vv73vv73vv71vOu+/ve+/ve+/vRDvv73vv73vv73vv73vv716Ju+/vSTvv73vv70t77+977+9MO+/ve+/vQTQvizNm2Xvv73vv70K77+9wrXvv73vv73vv71VLO+/vSvvv73vv73vv71y77+9Le+/vRbvv71iK++/ve+/ve+/ve+/vV95MNmB77+9MUoqI++/ve+/vRLvv70C77+9Ie+/vTfvv71ofu+/vVlr77+977+9WhUnbzc6OO+/ve+/vSzvv71N77+9eu+/vV8377+9BUAI77+9RDLvv70ITe+/vUcR77+9KO+/vVFq77+9VsaA77+977+9Du+/ve+/ve+/ve+/vT9l77+977+9N++/ve+/ve+/ve+/vXPdvu+/ve+/ve+/vXXvv70177+9M++/vQBy77+977+9Au+/vQ8sMjIWRu+/vUrvv73vv71o24Lvv71P77+9Mu+/vXx4NidENBHvv73vv70JN1ZIWmxfQ0rLuG/vv73vv70BYQnvv70t77+977+9Xe+/ve+/ve+/vTlZ77+977+977+9e++/ve+/ve+/vUHCm++/vStRyrXvv73vv73vv70ORO+/vXPvv73vv73vv70zIO+/ve+/vVjvv73vv70+GC9TFTA7MB8wBwYFKw4DAhoEFO+/vd+sOO+/vQBAUXcx77+977+9ENSK77+9Qe+/ve+/vRMEFAzvv70qMUgXDV3vv71ecO+/vRo5AO+/vQvvv73vv73vv70CAgfvv70NCg==
-----------------------------7de17e2d670f22--

证书都是bas64编码的。我希望它有所帮助。它是编码的东西,当我做base64编码并直接在它工作的文件中写它。但我需要手工拆分。整个代码是here

2 个答案:

答案 0 :(得分:1)

如果您的代码在编码字符串上运行良好但是以二进制格式制作,则主要是因为新行签名。简而言之:有几种格式可以打破界限。

此主题涵盖在此处:Regular Expression to match every new line character (\n) inside a <content> tag

然而,您可以通过将这些“隐藏字符”替换为适合您的元素来重新格式化您的文件

此示例使用notepad ++ https://superuser.com/questions/542719/how-to-find-and-replace-new-line-in-notepad

enter image description here

答案 1 :(得分:0)

对于记录,这不回答问题,它只是一种解决方法

我已经解决了这个问题。因为它是一个安装脚本,很明显,你在机器上,你安装的东西 所以我要求用户只复制证书。其他表单数据使用x-url-form-encoded

进行转换