前幾天文章寫到了關於一些 Azure Logic App 但是有沒有辦法在上面做到加密,這裡面我就稍微研究一下,關於 XOR 簡單加密 [C#] 在 Javascript 與 C# 中 簡單使用 XOR 加解密
,但是我在 Azure Logic App 上面本來想測試的,但是上面不支援 bota (當我看到他是使用 windows.bota 我就知道大事不妙),所以跟一般瀏覽器開發還是有差的,今天我就是分享一下如何在 Azure
Logic App – Inline Code 做到 Base 64 + XOR 加密..
關於解密的部分請參考 :
1.別忘記 如果你要用 Inline Code 必須要開啟 Integration Account
2.建立一個 When A Http Request Is Received,我這邊制定一個 json schema 要傳入 Content ,我會將他編碼後加密
{
"$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"
}
3. Inline Code ,這邊我就是指定 Step 2 的傳入 Content 進行加密,salt : salt1234
function sf(r){return String.fromCharCode(r)}
var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(r){var t,e,a,o,h,n,C,c="",i=0;for(r=Base64._utf8_encode(r);i>2,h=(3&t)<<4|(e=r.charCodeAt(i++))>>4,n=(15&e)<<2|(a=r.charCodeAt(i++))>>6,C=63&a,isNaN(e)?n=C=64:isNaN(a)&&(C=64),c=c+this._keyStr.charAt(o)+this._keyStr.charAt(h)+this._keyStr.charAt(n)+this._keyStr.charAt(C);return c},_utf8_encode:function(r){r=r.replace(/\r\n/g,"\n");for(var t="",e=0;e127&&a<2048?(t+=sf(a>>6|192),t+=sf(63&a|128)):(t+=sf(a>>12|224),t+=sf(a>>6&63|128),t+=sf(63&a|128))}return t}};
function XE(r,o){r=Base64.encode(r);for(var n=Array.from(o),e=[],t=0;t
4. Response Step3 的結果
Test C# Post 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\":\"測試中文Donma許\"}";
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
}
//Result
//RhMFBwd5XllGLQUABEJSfCEmVQFTZXtbAi0pSQ==
Full Auzre 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={_keyStr:\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\",encode:function(r){var t,e,a,o,h,n,C,c=\"\",i=0;for(r=Base64._utf8_encode(r);i<r.length;)o=(t=r.charCodeAt(i++))>>2,h=(3&t)<<4|(e=r.charCodeAt(i++))>>4,n=(15&e)<<2|(a=r.charCodeAt(i++))>>6,C=63&a,isNaN(e)?n=C=64:isNaN(a)&&(C=64),c=c+this._keyStr.charAt(o)+this._keyStr.charAt(h)+this._keyStr.charAt(n)+this._keyStr.charAt(C);return c},_utf8_encode:function(r){r=r.replace(/\\r\\n/g,\"\\n\");for(var t=\"\",e=0;e<r.length;e++){var a=r.charCodeAt(e);a<128?t+=sf(a):a>127&&a<2048?(t+=sf(a>>6|192),t+=sf(63&a|128)):(t+=sf(a>>12|224),t+=sf(a>>6&63|128),t+=sf(63&a|128))}return t}};\r\nfunction XE(r,o){r=Base64.encode(r);for(var n=Array.from(o),e=[],t=0;t<r.length;t++){var a=r.charCodeAt(t)^n[t%n.length].charCodeAt(0);e.push(sf(a))}var c=e.join(\"\");return Base64.encode(c)}\r\n\r\nreturn XE(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": {}
}
關於拿回來的字串要解密 請參考 [C#] 在 Javascript 與 C# 中 簡單使用 XOR
加解密
結論,因為一個 inline code action 只能夠有 1024 個字,所以不能夠隨心所欲的放 code ,所以你在規劃程式流程,這都要設計一個獨立的 action ,甚至要讓 inline code
action + inline code action 做到你要的 response
效果,這邊整理一下,給需要的人參考 ,這裡面我也是改寫下述附上連結的程式碼,為何只放 encode 因為 只能1024個字阿,我是覺得遲早有一天會支持這功能,在這之前先用這頂著吧 :)
reference:
http://jsfiddle.net/gabrieleromanato/qAGHT/