上一篇文章 說到 Azure Logic App - 使用 Inline
Code 製作 Base64 Encode + XOR 加密 ,為何沒有寫解密呢,因為其實因為我工作上沒有用到在加上 Azure Lgoic App 的 Inline code 只能寫
1024字,所以我那時候就沒寫了,不過覺得事情做一半,趁今天假日我就把他補上吧..
前情提要
關於加密的部分請參考 :
1. 別忘記 如果你要用 Inline Code 必須要開啟 Integration Account
2. 再來就是負責將字串解密用的 Inline Code Javascript Code
function sf(r){return String.fromCharCode(r)}
var Base64={_KS:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",decode:function(r){var e,t,o,c,a,d,h="",n=0;for(r=r.replace(/[^A-Za-z0-9\+\/\=]/g,"");n>4,t=(15&c)<<4|(a=this._KS.indexOf(r.charAt(n++)))>>2,o=(3&a)<<6|(d=this._KS.indexOf(r.charAt(n++))),h+=sf(e),64!=a&&(h+=sf(t)),64!=d&&(h+=sf(o));return h=Base64._decode(h)},_decode:function(r){for(var e="",t=0,o=c1=c2=0;t191&&o<224?(c2=r.charCodeAt(t+1),e+=sf((31&o)<<6|63&c2),t+=2):(c2=r.charCodeAt(t+1),c3=r.charCodeAt(t+2),e+=sf((15&o)<<12|(63&c2)<<6|63&c3),t+=3);return e}};
function XD(r,e){r=Base64.decode(r);for(var o=Array.from(e),t=[],a=0;a
3. 呼叫端的 C# code .
var url="https://prod-27.southeastasia.logic.azure.com:443/workflows/c8f244e4392442aaaf28c707a0c4b117/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=1zOne85ffXnBFplwkuJKTtqyPlgmqBmkKP-ov_9tF7A";
var client = new RestSharp.RestClient();
var request = new RestSharp.RestRequest(url, RestSharp.Method.POST);
string jsonToSend = "{\"Content\":\"RhMFBwd5XllGLQUABEJSfCEmVQFTZXtbAi0pSQ==\"}";
request.AddParameter("application/json; charset=utf-8", jsonToSend, RestSharp.ParameterType.RequestBody);
request.RequestFormat = RestSharp.DataFormat.Json;
try
{
var c = client.ExecuteAsPost(request, "POST");
Response.Write(c.Content);
}
catch (Exception error)
{
// Log
}
4. 所有的 Azure Logic App Code:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"JS_B64XOR": {
"inputs": {
"code": "function sf(r){return String.fromCharCode(r)}\r\nvar Base64={_KS:\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\",decode:function(r){var e,t,o,c,a,d,h=\"\",n=0;for(r=r.replace(/[^A-Za-z0-9\\+\\/\\=]/g,\"\");n<r.length;)e=this._KS.indexOf(r.charAt(n++))<<2|(c=this._KS.indexOf(r.charAt(n++)))>>4,t=(15&c)<<4|(a=this._KS.indexOf(r.charAt(n++)))>>2,o=(3&a)<<6|(d=this._KS.indexOf(r.charAt(n++))),h+=sf(e),64!=a&&(h+=sf(t)),64!=d&&(h+=sf(o));return h=Base64._decode(h)},_decode:function(r){for(var e=\"\",t=0,o=c1=c2=0;t<r.length;)(o=r.charCodeAt(t))<128?(e+=sf(o),t++):o>191&&o<224?(c2=r.charCodeAt(t+1),e+=sf((31&o)<<6|63&c2),t+=2):(c2=r.charCodeAt(t+1),c3=r.charCodeAt(t+2),e+=sf((15&o)<<12|(63&c2)<<6|63&c3),t+=3);return e}};\r\nfunction XD(r,e){r=Base64.decode(r);for(var o=Array.from(e),t=[],a=0;a<r.length;a++){var n=r.charCodeAt(a)^o[a%o.length].charCodeAt(0);t.push(sf(n))}var c=t.join(\"\");return Base64.decode(c)}\r\nreturn XD(workflowContext.trigger.outputs.body.Content,'salt1234');"
},
"runAfter": {},
"type": "JavaScriptCode"
},
"Response": {
"inputs": {
"body": "@outputs('JS_B64XOR')?['body']",
"statusCode": 200
},
"kind": "Http",
"runAfter": {
"JS_B64XOR": [
"Succeeded"
]
},
"type": "Response"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {
"$id": "http://example.com/example.json",
"$schema": "http://json-schema.org/draft-07/schema",
"additionalProperties": true,
"default": {},
"description": "The root schema comprises the entire JSON document.",
"examples": [
{
"Content": "你好我是Donma許"
}
],
"properties": {
"Content": {
"$id": "#/properties/Content",
"default": "",
"description": "An explanation about the purpose of this instance.",
"examples": [
"你好我是Donma許"
],
"title": "The Content schema",
"type": "string"
}
},
"required": [
"Content"
],
"title": "The root schema",
"type": "object"
}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
如果你不看懂我在說啥,可以看 Azure Logic App - 使用
Inline Code 製作 Base64 Encode + XOR 加密 這一篇就知道我在說啥了,這邊就不贅述主要就是分享可以用的 sample code.
reference:
http://jsfiddle.net/gabrieleromanato/qAGHT/