Node crypto sha256 not working with whitespace - node.js

Am I doing something wrong or is this a bug?
When using
var checkSum = crypto.createHash("sha256").update(scriptInnerHTML, "utf-8").digest("base64")
To generate the sha256 for this script tag:
<script>
console.warn("works");
var some code ...
</script>
And using it inside a Content-Security-Policy like this:
<meta http-equiv="Content-Security-Policy" content="script-src 'sha256-8O+YTKIDgMhMvSanTZx1Om5XY2ERB+kIxN8AcO2r6Ok='">
Things works as it should, but however this one, identical except without the warning-log, doesn't work. Notice the couple of new-lines and a tab before the code.
<script>
var some code ...
</script>
It seems as Node interprets tabs (or rather newlines+tab) differently if there is text afterwards. Weird!
Tested both in Safari and Chrome so it should not be a browser issue.
Updates:
I of-course re-generate the hash for each input.
Examples:
(The script tag is at the bottom)
Working hash ('sha256-5++3ItSu+9maCZiuuXH60RG7EugmibMmhxhwpsynAn0='): http://aggressive.se/test/works.html
Not working hash ('sha256-hIRDHGUSaEmjNiVhNabY+8l4GNQdj/PXD4XHA21gdRM='): http://aggressive.se/test/fail.html
SOLVED:
The problem was due to the serialize() function in JSDom which I use to generate the HTML in node. Calling dom.serialize() after calculating the hash, it removed one unnecessary tab which changed the source (in a hard to notice way).
But just so you know, the problem was not due to node nor the crypto module. (Hope someone has use for this)

I am not sure how you obtained the scriptInnerHTML variable, but if I access the innerHTML property of the script of interest in the fail.html example, I get this hexdump:
00000000 0a 0a 0a 09 76 61 72 20 6c 61 79 6f 75 74 20 3d |....var layout =|
00000010 20 6e 65 77 20 41 67 67 72 65 73 73 69 76 65 4c | new AggressiveL|
00000020 61 79 6f 75 74 28 29 3b 0a 09 41 67 67 72 65 73 |ayout();..Aggres|
00000030 73 69 76 65 4c 61 79 6f 75 74 2e 70 72 6f 74 6f |siveLayout.proto|
00000040 74 79 70 65 2e 73 65 74 75 70 46 75 6e 63 74 69 |type.setupFuncti|
00000050 6f 6e 20 3d 20 66 75 6e 63 74 69 6f 6e 28 29 0a |on = function().|
00000060 09 7b 0a 09 09 76 61 72 20 65 6c 65 6d 65 6e 74 |.{...var element|
00000070 20 3d 20 64 6f 63 75 6d 65 6e 74 2e 63 72 65 61 | = document.crea|
00000080 74 65 45 6c 65 6d 65 6e 74 28 22 64 69 76 22 29 |teElement("div")|
00000090 3b 0a 09 09 65 6c 65 6d 65 6e 74 2e 69 6e 6e 65 |;...element.inne|
000000a0 72 48 54 4d 4c 20 3d 20 22 49 74 20 77 6f 72 6b |rHTML = "It work|
000000b0 73 22 3b 0a 09 09 74 68 69 73 2e 65 6c 65 6d 65 |s";...this.eleme|
000000c0 6e 74 73 2e 72 6f 6f 74 45 6c 65 6d 65 6e 74 2e |nts.rootElement.|
000000d0 61 70 70 65 6e 64 43 68 69 6c 64 28 65 6c 65 6d |appendChild(elem|
000000e0 65 6e 74 29 0a 09 7d 0a |ent)..}.|
the corresponding hash of which is bR9Os+NBLWNZ3/wFVRhBilP05u9OeSj0ABRo+T8QF+g=. If I use this hash in the html file, it seems to work...
An example with JSDOM:
const fs = require('fs');
const crypto = require('crypto');
const { JSDOM } = require('jsdom');
const dom = new JSDOM(fs.readFileSync('./fail.html'));
const data = dom.window.document.querySelector('head > script:last-child').innerHTML;
console.log(crypto.createHash('sha256').update(data, 'utf-8').digest('base64'));
//bR9Os+NBLWNZ3/wFVRhBilP05u9OeSj0ABRo+T8QF+g=

Related

Receiving <Buffer> from Node JS Websocket

I'm simply trying to log messages from a websocket server but I seem to only receive Buffer data. The below example is trying to connect to https://www.piesocket.com/websocket-tester
What am I doing wrong?
import WebSocket from 'ws'
const demoStreamer = new WebSocket('wss://demo.piesocket.com/v3/channel_1?
api_key=oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm&notify_self')
demoStreamer.on('message', (data) => {
console.log(data);
});
// OUTPUT:
// <Buffer 7b 22 69 6e 66 6f 22 3a 22 59 6f 75 20 61 72 65 20 75 73 69 6e 67 20 61 20 74 65 73
// 74 20 61 70 69 20 6b 65 79 22 7d>
// <Buffer 54 65 73 74 20 6d 65 73 73 61 67 65>
// <Buffer 54 65 73 74 20 6d 65 73 73 61 67 65>
7b 22 69 6e 66 6f 22 3a 22 59 6f 75 20 61 72 65 20 75 73 69 6e 67 20 61 20 74 65 73 74 20 61 70 69 20 6b 65 79 22 7d is a hexadecimal representation of {"info":"You are using a test api key"}
Just convert the buffer to string:
demoStreamer.on('message', (data) => {
console.log(data.toString()); // "{\"info\":\"You are using a test api key\"}"
// Retrieve the info message:
console.log(JSON.parse(data).info); // "You are using a test api key"
});

strace does not show the complete write

Im trying to view the data being written to an HTTP socket using strace
However, although I have given -e write=all, I still cannot see all the data being written
strace -o /tmp/capture.log -p <pid> -e trace=all -e write=all -e read=all -f -tt
..
29620 16:09:14.723120 write(1899, "POST /task/native.wsdl HTTP/1.1\r"..., 210) = 210
29620 16:09:14.723319 write(1899, "<soap:Envelope xmlns:soap=\"http:"..., 450) = 450
What is strange is that, it shows the complete data during some other writes to other sockets
31145 16:09:28.110571 write(359, "POST /task/native.wsdl HTTP/1.1\r"..., 210) = 210
| 00000 50 4f 53 54 20 2f 74 61 73 6b 2f 6e 61 74 69 76 POST /task/nativ |
| 00010 65 2e 77 73 64 6c 20 48 54 54 50 2f 31 2e 31 0d e.wsdl HTTP/1.1. |
| 00020 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 .Content-Type: t |
| 00030 65 78 74 2f 78 6d 6c 3b 20 63 68 61 72 73 65 74 ext/xml; charset |
| 00040 3d 55 54 46 2d 38 0d 0a 41 63 63 65 70 74 3a 20 =UTF-8..Accept: |
| 00050 2a 2f 2a 0d 0a 53 4f 41 50 41 63 74 69 6f 6e 3a */*..SOAPAction: |
| 00060 20 22 22 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a ""..User-Agent: |
| 00070 20 41 70 61 63 68 65 20 43 58 46 20 32 2e 37 2e Apache CXF 2.7. |
| 00080 31 31 0d 0a 48 6f 73 74 3a 20 65 73 2d 73 76 63 11..Host: es-svc |
| 00090 73 2e 69 74 2e 61 74 74 2e 63 6f 6d 3a 37 30 30 s.it.att.com:700 |
| 000a0 33 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 3..Connection: K |
| 000b0 65 65 70 2d 41 6c 69 76 65 0d 0a 43 6f 6e 74 65 eep-Alive..Conte |
| 000c0 6e 74 2d 4c 65 6e 67 74 68 3a 20 34 35 30 0d 0a nt-Length: 450.. |
| 000d0 0d 0a .. |
Can somebody please explain. Is it possible to get data from all writes
The strace -e write=a,b flag shows write syscalls for file descriptors numbered from a to b, and -e write=all shows write syscalls to all file descriptors.
What you're looking for is -e abbrev=none; see the manpage:
-e abbrev=set
Abbreviate the output from printing each member of large structures.
The default is abbrev=all. The -v option has the effect of abbrev=none.

Nodejs sync-request not returning HTML Response

const cheerio = require('cheerio');
const request = require('sync-request');
const fs = require('fs');
var res = request('GET', 'https://edition.cnn.com/');
console.log(res.getBody())
This is my code. I want to get HTML code of https://edition.cnn.com/. But it returning following:
<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 3c 68 74 6d 6c 20 63 6c 61 73 73 3d 22 6e 6f 2d 6a 73 22 3e 3c 68 65 61 64 3e 3c 6d 65 74 61 20 63 6f 6e ... >
You are receiving a buffer currently, you need to convert it to string. Use this -
console.log(res.getBody().toString())

Network request to TV works from any device but Raspberry Pi

I'm trying to control my Panasonic TV via network. I had all my code (node) running from the laptop when I transferred it to the Raspberry. For some reason, it does not work from there. (A similar project controlling my Denon receiver works very well.)
By "Not Working", I mean, that the TV can be reached, but returns a 403 error. When TV is off, it's unreachable. So the Raspberry calls out to the TV.
I tried many different ways and eventually I'm working with a curl request. It works from everywhere, but not from the Raspberry:
curl 'http://192.168.178.142:55000/nrc/control_0' \
-H 'content-type: text/xml; charset="utf-8"' \
-H 'user-agent: net.thlabs.nodecontrol' \
-H 'soapaction: "urn:panasonic-com:service:p00NetworkControl:1#X_SendKey"' \
--data-binary '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:X_SendKey xmlns:u="urn:panasonic-com:service:p00NetworkControl:1"><X_KeyEvent>NRC_MUTE-ONOFF</X_KeyEvent></u:X_SendKey></s:Body></s:Envelope>' \
--compressed
It does work from the Raspberry, if I proxy through any other device e.g. my laptop. However, it does not work, when I proxy from laptop through the Raspberry.
Trying for two days now, does anybody have a clue?
Edit: I reduced the curl even more: Without user-agent, content-type and compression this stilll works on any laptop (running windows, also tried WSL shell), but on my Raspberries (1+3, Rasbian and ArchLinux), I receive 403. When I additionally omit the soapaction I receive error 400 everywhere.
curl 'http://192.168.178.142:55000/nrc/control_0' \
-H 'soapaction: "urn:panasonic-com:service:p00NetworkControl:1#X_SendKey"' \
--data-binary '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:X_SendKey xmlns:u="urn:panasonic-com:service:p00NetworkControl:1"><X_KeyEvent>NRC_MUTE-ONOFF</X_KeyEvent></u:X_SendKey></s:Body></s:Envelope>' \
-w '%{http_code}'
Update 1: verbose curl
curl -v ... result on Laptop:
* Hostname was NOT found in DNS cache
* Trying 192.168.178.142...
* Connected to 192.168.178.142 (192.168.178.142) port 55000 (#0)
> POST /nrc/control_0 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 192.168.178.142:55000
> Accept: */\*
> soapaction: "urn:panasonic-com:service:p00NetworkControl:1#X_SendKey"
> Content-Length: 314
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 314 out of 314 bytes
< HTTP/1.1 200 OK
< CONTENT-LENGTH: 301
< Content-Type: text/xml; charset="utf-8"
< EXT:
* Server Panasonic-VIErA/1, UPnP/1.0, Panasonic MIL DLNA SERVER is not blacklisted
< SERVER: Panasonic-VIErA/1, UPnP/1.0, Panasonic MIL DLNA SERVER
< CONNECTION: close
<
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:X_SendKeyResponse xmlns:u="urn:panasoniccom:service:p00NetworkControl:1"></u:X_SendKeyResponse>
</s:Body>
</s:Envelope>
* Closing connection 0
curl -v ... result on Raspberries:
* Hostname was NOT found in DNS cache
* Trying 192.168.178.142...
* Connected to 192.168.178.142 (192.168.178.142) port 55000 (#0)
> POST /nrc/control_0 HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 192.168.178.142:55000
> Accept: */\*
> soapaction: "urn:panasonic-com:service:p00NetworkControl:1#X_SendKey"
> Content-Length: 314
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 314 out of 314 bytes
< HTTP/1.1 403 Forbidden
< CONNECTION: close
<
* Closing connection 0
Update 2: Traceroutes
On Windows Laptop (Powershell):
tracert 192.168.178.142
Routenverfolgung zu COM-MID1.fritz.box [192.168.178.142] über maximal 30 Hops:
1 4 ms 3 ms 3 ms COM-MID1.fritz.box [192.168.178.142]
On Raspberry Pi:
pi#raspberrypi:~ $ traceroute 192.168.178.142
traceroute to 192.168.178.142 (192.168.178.142), 30 hops max, 60 byte packets
1 COM-MID1.fritz.box (192.168.178.142) 4.545 ms 4.411 ms 4.347 ms
Update 3: HTTP Requests captured by Wireshark
From Laptop:
0000 c0 d9 62 53 1b 03 00 c2 c6 cd ce 81 08 00 45 00 ..bS..........E.
0010 02 52 00 08 40 00 80 06 12 7f c0 a8 b2 3f c0 a8 .R..#........?..
0020 b2 8e ff 53 d6 d8 9f 96 36 7f e1 55 0f 6c 50 18 ...S....6..U.lP.
0030 fa f0 4d 2d 00 00 50 4f 53 54 20 2f 6e 72 63 2f ..M-..POST /nrc/
0040 63 6f 6e 74 72 6f 6c 5f 30 20 48 54 54 50 2f 31 control_0 HTTP/1
0050 2e 31 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 .1..User-Agent:
0060 63 75 72 6c 2f 37 2e 33 35 2e 30 0d 0a 48 6f 73 curl/7.35.0..Hos
0070 74 3a 20 31 39 32 2e 31 36 38 2e 31 37 38 2e 31 t: 192.168.178.1
0080 34 32 3a 35 35 30 30 30 0d 0a 41 63 63 65 70 74 42:55000..Accept
0090 3a 20 2a 2f 2a 0d 0a 73 6f 61 70 61 63 74 69 6f : *\*..soapactio
00a0 6e 3a 20 22 75 72 6e 3a 70 61 6e 61 73 6f 6e 69 n: "urn:panasoni
00b0 63 2d 63 6f 6d 3a 73 65 72 76 69 63 65 3a 70 30 c-com:service:p0
00c0 30 4e 65 74 77 6f 72 6b 43 6f 6e 74 72 6f 6c 3a 0NetworkControl:
00d0 31 23 58 5f 53 65 6e 64 4b 65 79 22 0d 0a 43 6f 1#X_SendKey"..Co
00e0 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 33 31 ntent-Length: 31
00f0 34 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 4..Content-Type:
0100 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 77 application/x-w
0110 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 ww-form-urlencod
0120 65 64 0d 0a 0d 0a 3c 3f 78 6d 6c 20 76 65 72 73 ed....<?xml vers
0130 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 ion="1.0" encodi
0140 6e 67 3d 22 75 74 66 2d 38 22 3f 3e 3c 73 3a 45 ng="utf-8"?><s:E
0150 6e 76 65 6c 6f 70 65 20 78 6d 6c 6e 73 3a 73 3d nvelope xmlns:s=
0160 22 68 74 74 70 3a 2f 2f 73 63 68 65 6d 61 73 2e "http://schemas.
0170 78 6d 6c 73 6f 61 70 2e 6f 72 67 2f 73 6f 61 70 xmlsoap.org/soap
0180 2f 65 6e 76 65 6c 6f 70 65 2f 22 20 73 3a 65 6e /envelope/" s:en
0190 63 6f 64 69 6e 67 53 74 79 6c 65 3d 22 68 74 74 codingStyle="htt
01a0 70 3a 2f 2f 73 63 68 65 6d 61 73 2e 78 6d 6c 73 p://schemas.xmls
01b0 6f 61 70 2e 6f 72 67 2f 73 6f 61 70 2f 65 6e 63 oap.org/soap/enc
01c0 6f 64 69 6e 67 2f 22 3e 3c 73 3a 42 6f 64 79 3e oding/"><s:Body>
01d0 3c 75 3a 58 5f 53 65 6e 64 4b 65 79 20 78 6d 6c <u:X_SendKey xml
01e0 6e 73 3a 75 3d 22 75 72 6e 3a 70 61 6e 61 73 6f ns:u="urn:panaso
01f0 6e 69 63 2d 63 6f 6d 3a 73 65 72 76 69 63 65 3a nic-com:service:
0200 70 30 30 4e 65 74 77 6f 72 6b 43 6f 6e 74 72 6f p00NetworkContro
0210 6c 3a 31 22 3e 3c 58 5f 4b 65 79 45 76 65 6e 74 l:1"><X_KeyEvent
0220 3e 4e 52 43 5f 4d 55 54 45 2d 4f 4e 4f 46 46 3c >NRC_MUTE-ONOFF<
0230 2f 58 5f 4b 65 79 45 76 65 6e 74 3e 3c 2f 75 3a /X_KeyEvent></u:
0240 58 5f 53 65 6e 64 4b 65 79 3e 3c 2f 73 3a 42 6f X_SendKey></s:Bo
0250 64 79 3e 3c 2f 73 3a 45 6e 76 65 6c 6f 70 65 3e dy></s:Envelope>
From Raspberry:
0000 c0 d9 62 53 1b 03 b8 27 eb fb b4 2f 08 00 45 00 ..bS...'.../..E.
0010 02 5e 7a 2d 40 00 40 06 d8 6f c0 a8 b2 1d c0 a8 .^z-#.#..o......
0020 b2 8e ad e4 d6 d8 af d0 76 fc ef 51 f3 ed 80 18 ........v..Q....
0030 00 e5 6f 0b 00 00 01 01 08 0a 00 09 0c d0 76 94 ..o...........v.
0040 27 05 50 4f 53 54 20 2f 6e 72 63 2f 63 6f 6e 74 '.POST /nrc/cont
0050 72 6f 6c 5f 30 20 48 54 54 50 2f 31 2e 31 0d 0a rol_0 HTTP/1.1..
0060 55 73 65 72 2d 41 67 65 6e 74 3a 20 63 75 72 6c User-Agent: curl
0070 2f 37 2e 33 38 2e 30 0d 0a 48 6f 73 74 3a 20 31 /7.38.0..Host: 1
0080 39 32 2e 31 36 38 2e 31 37 38 2e 31 34 32 3a 35 92.168.178.142:5
0090 35 30 30 30 0d 0a 41 63 63 65 70 74 3a 20 2a 2f 5000..Accept: *\
00a0 2a 0d 0a 73 6f 61 70 61 63 74 69 6f 6e 3a 20 22 *..soapaction: "
00b0 75 72 6e 3a 70 61 6e 61 73 6f 6e 69 63 2d 63 6f urn:panasonic-co
00c0 6d 3a 73 65 72 76 69 63 65 3a 70 30 30 4e 65 74 m:service:p00Net
00d0 77 6f 72 6b 43 6f 6e 74 72 6f 6c 3a 31 23 58 5f workControl:1#X_
00e0 53 65 6e 64 4b 65 79 22 0d 0a 43 6f 6e 74 65 6e SendKey"..Conten
00f0 74 2d 4c 65 6e 67 74 68 3a 20 33 31 34 0d 0a 43 t-Length: 314..C
0100 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 70 ontent-Type: app
0110 6c 69 63 61 74 69 6f 6e 2f 78 2d 77 77 77 2d 66 lication/x-www-f
0120 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 64 0d 0a orm-urlencoded..
0130 0d 0a 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d ..<?xml version=
0140 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 "1.0" encoding="
0150 75 74 66 2d 38 22 3f 3e 3c 73 3a 45 6e 76 65 6c utf-8"?><s:Envel
0160 6f 70 65 20 78 6d 6c 6e 73 3a 73 3d 22 68 74 74 ope xmlns:s="htt
0170 70 3a 2f 2f 73 63 68 65 6d 61 73 2e 78 6d 6c 73 p://schemas.xmls
0180 6f 61 70 2e 6f 72 67 2f 73 6f 61 70 2f 65 6e 76 oap.org/soap/env
0190 65 6c 6f 70 65 2f 22 20 73 3a 65 6e 63 6f 64 69 elope/" s:encodi
01a0 6e 67 53 74 79 6c 65 3d 22 68 74 74 70 3a 2f 2f ngStyle="http://
01b0 73 63 68 65 6d 61 73 2e 78 6d 6c 73 6f 61 70 2e schemas.xmlsoap.
01c0 6f 72 67 2f 73 6f 61 70 2f 65 6e 63 6f 64 69 6e org/soap/encodin
01d0 67 2f 22 3e 3c 73 3a 42 6f 64 79 3e 3c 75 3a 58 g/"><s:Body><u:X
01e0 5f 53 65 6e 64 4b 65 79 20 78 6d 6c 6e 73 3a 75 _SendKey xmlns:u
01f0 3d 22 75 72 6e 3a 70 61 6e 61 73 6f 6e 69 63 2d ="urn:panasonic-
0200 63 6f 6d 3a 73 65 72 76 69 63 65 3a 70 30 30 4e com:service:p00N
0210 65 74 77 6f 72 6b 43 6f 6e 74 72 6f 6c 3a 31 22 etworkControl:1"
0220 3e 3c 58 5f 4b 65 79 45 76 65 6e 74 3e 4e 52 43 ><X_KeyEvent>NRC
0230 5f 4d 55 54 45 2d 4f 4e 4f 46 46 3c 2f 58 5f 4b _MUTE-ONOFF</X_K
0240 65 79 45 76 65 6e 74 3e 3c 2f 75 3a 58 5f 53 65 eyEvent></u:X_Se
0250 6e 64 4b 65 79 3e 3c 2f 73 3a 42 6f 64 79 3e 3c ndKey></s:Body><
0260 2f 73 3a 45 6e 76 65 6c 6f 70 65 3e /s:Envelope>
Update 4: Files with captured packets
Laptop: https://drive.google.com/open?id=1P_uVc0ndGG37kizuFBZwApvQ-SePKbZd
RaspPi: https://drive.google.com/open?id=1V4-t3qi5TS8NpYeGqVXLo_zi8PxXbRnp
I have resolved the problem by resetting the factory parameters of my Panasonic TV. But I don't understand the root cause of the problem.

awk part of bash script not working properly

Everything seems fine when using script as #/bin/sh on ubuntu but now on using the same script with #/bin/bash on red hat this part is creating problem.
awk '{
for (i = NF - p - 2; i < NF - 2; i++)
printf "%s", ($i (i < NF - 2 - 1 ? OFS : ORS))
}' p="$_padlen" RS= ORS='\n' decrypt.txt > pad.txt
also this one ..
awk '{
for (i = NF - l - p - 2; i < NF - p - 2; i++)
printf "%s", ($i (i < NF - p - 2 - 1 ? OFS : ORS))
}' l="$_length" p="$_padlen" RS= ORS='\n' decrypt.txt > prot_n_data.txt
assuming $padlen=1 and $length=13 these are the respective outputs...
[root#localhost scripts]# cat decrypt.txt
00 15 00 15 00 0d dc ff 61 62 63 64 0a 00 01 11
Among last three bytes which are 00 01 11. the 00 is padding and 01 is padding length.
[root#localhost scripts]# cat pad.txt
0a
[root#localhost scripts]# cat prot_n_data.txt
00 15 00 15 00 0d dc ff 61 62 63 64 0a 00 01 11 00 15 00 15 00 0d dc ff
61 62 63 64
While the desired output is ...
# cat pad.txt
00
# cat prot_n_data.txt
00 15 00 15 00 0d dc ff 61 62 63 64 0a
Completely stucked getting no clue ...Please help me .
OR Atleast suggest me some alternative ...
EDIT:*
This is another sample file where the suggested solution not working out assuming $_padlen=3 and $_length=1159.
[root#localhost scripts]# cat decrypt.txt
00 17 00 17 04 87 5d c5 74 68 69 73 20 69 73 20
73 61 6d 70 6c 65 20 64 61 74 61 20 74 6f 20 73
65 6e 64 0a 77 65 20 73 68 6f 75 6c 64 20 63 6f
6e 63 65 6e 74 72 61 74 65 20 6f 6e 20 69 74 2e
0a 74 68 69 73 20 69 73 20 73 61 6d 70 6c 65 20
64 61 74 61 20 74 6f 20 73 65 6e 64 0a 77 65 20
73 68 6f 75 6c 64 20 63 6f 6e 63 65 6e 74 72 61
74 65 20 6f 6e 20 69 74 2e 0a 74 68 69 73 20 69
73 20 73 61 6d 70 6c 65 20 64 61 74 61 20 74 6f
20 73 65 6e 64 0a 77 65 20 73 68 6f 75 6c 64 20
63 6f 6e 63 65 6e 74 72 61 74 65 20 6f 6e 20 69
74 2e 0a 74 68 69 73 20 69 73 20 73 61 6d 70 6c
65 20 64 61 74 61 20 74 6f 20 73 65 6e 64 0a 77
65 20 73 68 6f 75 6c 64 20 63 6f 6e 63 65 6e 74
72 61 74 65 20 6f 6e 20 69 74 2e 0a 74 68 69 73
20 69 73 20 73 61 6d 70 6c 65 20 64 61 74 61 20
74 6f 20 73 65 6e 64 0a 77 65 20 73 68 6f 75 6c
64 20 63 6f 6e 63 65 6e 74 72 61 74 65 20 6f 6e
20 69 74 2e 0a 74 68 69 73 20 69 73 20 73 61 6d
70 6c 65 20 64 61 74 61 20 74 6f 20 73 65 6e 64
0a 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e 63 65
6e 74 72 61 74 65 20 6f 6e 20 69 74 2e 0a 74 68
69 73 20 69 73 20 73 61 6d 70 6c 65 20 64 61 74
61 20 74 6f 20 73 65 6e 64 0a 77 65 20 73 68 6f
75 6c 64 20 63 6f 6e 63 65 6e 74 72 61 74 65 20
6f 6e 20 69 74 2e 0a 74 68 69 73 20 69 73 20 73
61 6d 70 6c 65 20 64 61 74 61 20 74 6f 20 73 65
6e 64 0a 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e
63 65 6e 74 72 61 74 65 20 6f 6e 20 69 74 2e 0a
74 68 69 73 20 69 73 20 73 61 6d 70 6c 65 20 64
61 74 61 20 74 6f 20 73 65 6e 64 0a 77 65 20 73
68 6f 75 6c 64 20 63 6f 6e 63 65 6e 74 72 61 74
65 20 6f 6e 20 69 74 2e 0a 74 68 69 73 20 69 73
20 73 61 6d 70 6c 65 20 64 61 74 61 20 74 6f 20
73 65 6e 64 0a 77 65 20 73 68 6f 75 6c 64 20 63
6f 6e 63 65 6e 74 72 61 74 65 20 6f 6e 20 69 74
2e 0a 74 68 69 73 20 69 73 20 73 61 6d 70 6c 65
20 64 61 74 61 20 74 6f 20 73 65 6e 64 0a 77 65
20 73 68 6f 75 6c 64 20 63 6f 6e 63 65 6e 74 72
61 74 65 20 6f 6e 20 69 74 2e 0a 74 68 69 73 20
69 73 20 73 61 6d 70 6c 65 20 64 61 74 61 20 74
6f 20 73 65 6e 64 0a 77 65 20 73 68 6f 75 6c 64
20 63 6f 6e 63 65 6e 74 72 61 74 65 20 6f 6e 20
69 74 2e 0a 74 68 69 73 20 69 73 20 73 61 6d 70
6c 65 20 64 61 74 61 20 74 6f 20 73 65 6e 64 0a
77 65 20 73 68 6f 75 6c 64 20 63 6f 6e 63 65 6e
74 72 61 74 65 20 6f 6e 20 69 74 2e 0a 74 68 69
73 20 69 73 20 73 61 6d 70 6c 65 20 64 61 74 61
20 74 6f 20 73 65 6e 64 0a 77 65 20 73 68 6f 75
6c 64 20 63 6f 6e 63 65 6e 74 72 61 74 65 20 6f
6e 20 69 74 2e 0a 74 68 69 73 20 69 73 20 73 61
6d 70 6c 65 20 64 61 74 61 20 74 6f 20 73 65 6e
64 0a 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e 63
65 6e 74 72 61 74 65 20 6f 6e 20 69 74 2e 0a 74
68 69 73 20 69 73 20 73 61 6d 70 6c 65 20 64 61
74 61 20 74 6f 20 73 65 6e 64 0a 77 65 20 73 68
6f 75 6c 64 20 63 6f 6e 63 65 6e 74 72 61 74 65
20 6f 6e 20 69 74 2e 0a 74 68 69 73 20 69 73 20
73 61 6d 70 6c 65 20 64 61 74 61 20 74 6f 20 73
65 6e 64 0a 77 65 20 73 68 6f 75 6c 64 20 63 6f
6e 63 65 6e 74 72 61 74 65 20 6f 6e 20 69 74 2e
0a 74 68 69 73 20 69 73 20 73 61 6d 70 6c 65 20
64 61 74 61 20 74 6f 20 73 65 6e 64 0a 77 65 20
73 68 6f 75 6c 64 20 63 6f 6e 63 65 6e 74 72 61
74 65 20 6f 6e 20 69 74 2e 0a 74 68 69 73 20 69
73 20 73 61 6d 70 6c 65 20 64 61 74 61 20 74 6f
20 73 65 6e 64 0a 77 65 20 73 68 6f 75 6c 64 20
63 6f 6e 63 65 6e 74 72 61 74 65 20 6f 6e 20 69
74 2e 0a 74 68 69 73 20 69 73 20 73 61 6d 70 6c
65 20 64 61 74 61 20 74 6f 20 73 65 6e 64 0a 77
65 20 73 68 6f 75 6c 64 20 63 6f 6e 63 65 6e 74
72 61 74 65 20 6f 6e 20 69 74 2e 0a 0a 0a 0a 0a
0a 0a 0a 0a 0a 0a 0a 00 00 00 03 11
The command I used is ..
[root#localhost scripts]# awk '{printf $0}' decrypt.txt | awk 'NF{for (i=NF-l-p-1;
i<NF-p-1;i++) printf $i" "; print ""}' p=3 l=1159
awk: (FILENAME=- FNR=1) fatal: attempt to access field -71
l is simply calculated as wc -w decrypt.txt|cut -d " " -f1 minus p minus 2
Try this (you might have to test it against few more samples)
[jaypal~/Temp]$ cat file.txt
00 15 00 15 00 0d dc ff 61 62 63 64 0a 00 01 11
For Pad.txt:
[jaypal~/Temp]$ awk 'NF{for (i=NF-p-1;i<NF-1;i++) printf $i; print ""}' p=1 file.txt
00
For Prot_n_data.txt:
[jaypal~/Temp]$ awk 'NF{for (i=NF-l-p-1;i<NF-p-1;i++) printf $i" "; print ""}' p=1 l=13 file.txt
00 15 00 15 00 0d dc ff 61 62 63 64 0a
UPDATED:
Since your feed file overlaps into a new line, NF needs to be handled differently. NF is number of fields on a particular record (which by default is a line).
pad.txt
awk 'BEGIN{RS=""} {for (i=NF-p-1;i<NF-1;i++) printf $i" "; print ""}' p=1 file.txt | sed 's/..\{47\}/&\n/g'
prot_n_data.txt
awk 'BEGIN{RS=""} {for (i=NF-l-p-1;i<NF-p-1;i++) printf $i" "; print ""}' p=3 l=1159 file1.txt | sed 's/..\{47\}/&\n/g'

Resources