[Azure] Azure Logic App - 使用 Inline Code 製作 Base64 Encode + XOR 加密

2021-01-21

前幾天文章寫到了關於一些 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<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}}; function 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)} return XE(workflowContext.trigger.outputs.body.Content,'salt1234');

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 :

{ &quot;definition&quot;: { &quot;$schema&quot;: &quot;https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#&quot;, &quot;actions&quot;: { &quot;JS_B64XOR&quot;: { &quot;inputs&quot;: { &quot;code&quot;: &quot;function sf(r){return String.fromCharCode(r)}\r\nvar Base64={_keyStr:\&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\&quot;,encode:function(r){var t,e,a,o,h,n,C,c=\&quot;\&quot;,i=0;for(r=Base64._utf8_encode(r);i&lt;r.length;)o=(t=r.charCodeAt(i++))&gt;&gt;2,h=(3&amp;t)&lt;&lt;4|(e=r.charCodeAt(i++))&gt;&gt;4,n=(15&amp;e)&lt;&lt;2|(a=r.charCodeAt(i++))&gt;&gt;6,C=63&amp;a,isNaN(e)?n=C=64:isNaN(a)&amp;&amp;(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,\&quot;\\n\&quot;);for(var t=\&quot;\&quot;,e=0;e&lt;r.length;e++){var a=r.charCodeAt(e);a&lt;128?t+=sf(a):a&gt;127&amp;&amp;a&lt;2048?(t+=sf(a&gt;&gt;6|192),t+=sf(63&amp;a|128)):(t+=sf(a&gt;&gt;12|224),t+=sf(a&gt;&gt;6&amp;63|128),t+=sf(63&amp;a|128))}return t}};\r\nfunction XE(r,o){r=Base64.encode(r);for(var n=Array.from(o),e=[],t=0;t&lt;r.length;t++){var a=r.charCodeAt(t)^n[t%n.length].charCodeAt(0);e.push(sf(a))}var c=e.join(\&quot;\&quot;);return Base64.encode(c)}\r\n\r\nreturn XE(workflowContext.trigger.outputs.body.Content,&#39;salt1234&#39;);&quot; }, &quot;runAfter&quot;: {}, &quot;type&quot;: &quot;JavaScriptCode&quot; }, &quot;Response&quot;: { &quot;inputs&quot;: { &quot;body&quot;: &quot;@outputs(&#39;JS_B64XOR&#39;)?[&#39;body&#39;]&quot;, &quot;statusCode&quot;: 200 }, &quot;kind&quot;: &quot;http&quot;, &quot;runAfter&quot;: { &quot;JS_B64XOR&quot;: [ &quot;Succeeded&quot; ] }, &quot;type&quot;: &quot;Response&quot; } }, &quot;contentVersion&quot;: &quot;1.0.0.0&quot;, &quot;outputs&quot;: {}, &quot;parameters&quot;: {}, &quot;triggers&quot;: { &quot;manual&quot;: { &quot;inputs&quot;: { &quot;schema&quot;: { &quot;$id&quot;: &quot;http://example.com/example.json&quot;, &quot;$schema&quot;: &quot;http://json-schema.org/draft-07/schema&quot;, &quot;additionalProperties&quot;: true, &quot;default&quot;: {}, &quot;description&quot;: &quot;The root schema comprises the entire JSON document.&quot;, &quot;examples&quot;: [ { &quot;Content&quot;: &quot;你好我是Donma許&quot; } ], &quot;properties&quot;: { &quot;Content&quot;: { &quot;$id&quot;: &quot;#/properties/Content&quot;, &quot;default&quot;: &quot;&quot;, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ &quot;你好我是Donma許&quot; ], &quot;title&quot;: &quot;The Content schema&quot;, &quot;type&quot;: &quot;string&quot; } }, &quot;required&quot;: [ &quot;Content&quot; ], &quot;title&quot;: &quot;The root schema&quot;, &quot;type&quot;: &quot;object&quot; } }, &quot;kind&quot;: &quot;Http&quot;, &quot;type&quot;: &quot;Request&quot; } } }, &quot;parameters&quot;: {} }


關於拿回來的字串要解密 請參考 [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/


當麻許的碎念筆記 2014 | Donma Hsu Design.