AutoHotkey - limiting the number and type of characters taken from the string - autohotkey

I have an ahk script for an IRC client which after entering nick!ident#host into the text field and pressing F4 decrypts the ident which is the encrypted form of the IP address:
F4::
Clipboard =
Send ^a^x
ClipWait, 0
If ErrorLevel
MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
Else Clipboard := decode(SubStr(Clipboard, -15, -8))
Return
decode(str) {
Static code := " " "
( LTrim Join`s
00 0x 02 03 04 0z 06 01 08 09 0B 0b 0c 0d 0e 0H x0 xx x2 x3 x4 xz x6 x1 x8 x9 xB xb xc xd xe xH 20 2x
22 23 24 2z 26 21 28 29 2B 2b 2c 2d 2e 2H 30 3x 32 33 34 3z 36 31 38 39 3B 3b 3c 3d 3e 3H 40 4x 42 43
44 4z 46 41 48 49 4B 4b 4c 4d 4e 4H z0 zx z2 z3 z4 zz z6 z1 z8 z9 zB zb zc zd ze zH 60 6x 62 63 64 6z
66 61 68 69 6B 6b 6c 6d 6e 6H 10 1x 12 13 14 1z 16 11 18 19 1B 1b 1c 1d 1e 1H 80 8x 82 83 84 8z 86 81
88 89 8B 8b 8c 8d 8e 8H 90 9x 92 93 94 9z 96 91 98 99 9B 9b 9c 9d 9e 9H B0 Bx B2 B3 B4 Bz B6 B1 B8 B9
BB Bb Bc Bd Be BH b0 bx b2 b3 b4 bz b6 b1 b8 b9 bB bb bc bd be bH c0 cx c2 c3 c4 cz c6 c1 c8 c9 cB cb
cc cd ce cH d0 dx d2 d3 d4 dz d6 d1 d8 d9 dB db dc dd de dH e0 ex e2 e3 e4 ez e6 e1 e8 e9 eB eb ec ed
ee eH H0 Hx H2 H3 H4 Hz H6 H1 H8 H9 HB Hb Hc Hd He HH
)"
Loop, % StrLen(str) / 2
new .= "." Round((Instr(code, " " SubStr(str, 2 * A_Index - 1, 2), True) - 1) / 3)
Return SubStr(new, 2)
}
Decryption is performed according to the following key:
https://pastebin.com/raw/P8cQtH2v
For example, for user data asdf!~z3040d4B#webchat will decrypt the ident from z3040d4B as 83.4.13.75 and copies this value to the clipboard.
But there are cases when the encoded form of the IP (ident) is longer or shorter than 8 characters or contains characters that aren't in the decryption key. Then it's impossible to decode the IP correctly. So I would like the script to copy the decryption result to the clipboard only if the retrieved string (between ! and #, omitting the ~ sign if present) is 8 characters long and contains the characters contained in the key I entered. Otherwise, the script should clear the clipboard. How to do it?

A regex match approach with e.g the regex !~?[A-z\d]{8}# is surely most convenient:
F4::
Clipboard := ""
SendInput, ^a^x
ClipWait, 0
if (ErrorLevel)
MsgBox, 48, Error, An error occurred while waiting for the clipboard. Aborting.
else if (Clipboard ~= "!~?[A-z\d]{8}#")
Clipboard := decode(SubStr(Clipboard, -15, -8))
else
Clipboard := ""
Return
decode(str)
{
static code := " " "
( LTrim Join`s
00 0x 02 03 04 0z 06 01 08 09 0B 0b 0c 0d 0e 0H x0 xx x2 x3 x4 xz x6 x1 x8 x9 xB xb xc xd xe xH 20 2x
22 23 24 2z 26 21 28 29 2B 2b 2c 2d 2e 2H 30 3x 32 33 34 3z 36 31 38 39 3B 3b 3c 3d 3e 3H 40 4x 42 43
44 4z 46 41 48 49 4B 4b 4c 4d 4e 4H z0 zx z2 z3 z4 zz z6 z1 z8 z9 zB zb zc zd ze zH 60 6x 62 63 64 6z
66 61 68 69 6B 6b 6c 6d 6e 6H 10 1x 12 13 14 1z 16 11 18 19 1B 1b 1c 1d 1e 1H 80 8x 82 83 84 8z 86 81
88 89 8B 8b 8c 8d 8e 8H 90 9x 92 93 94 9z 96 91 98 99 9B 9b 9c 9d 9e 9H B0 Bx B2 B3 B4 Bz B6 B1 B8 B9
BB Bb Bc Bd Be BH b0 bx b2 b3 b4 bz b6 b1 b8 b9 bB bb bc bd be bH c0 cx c2 c3 c4 cz c6 c1 c8 c9 cB cb
cc cd ce cH d0 dx d2 d3 d4 dz d6 d1 d8 d9 dB db dc dd de dH e0 ex e2 e3 e4 ez e6 e1 e8 e9 eB eb ec ed
ee eH H0 Hx H2 H3 H4 Hz H6 H1 H8 H9 HB Hb Hc Hd He HH
)"
Loop, % StrLen(str) / 2
{
if (!InStr(code, block := " " SubStr(str, 2 * A_Index - 1, 2), true))
return ""
new .= "." Round((InStr(code, block, true) - 1) / 3)
}
return SubStr(new, 2)
}
~=(docs) is the RegExMatch()(docs) shorthand.

Related

RSA Private Key PEM in ASN.1 Format Contains Extra Bytes

I'm looking at an RSA private key in PEM format. When I decode the base64 string and review the components of the key, some of them have an extra byte, specifically the modulus, P, DP, and IQ. They all have a leading 0x00 byte. I'm handling this by just trimming the byte[] down to the expected lengths of 256 or 128 so I can use them with .NET RSAParameters and RSACryptoServiceProvider, but wondering why some of these INTEGER structures have the extra byte while others don't. It would appear that online and other libraries that decode the PEM to XML, etc handle this gracefully, so is this part of the RFC, just something that you have to protect against, or only a concern because I'm using the .NET libraries after decoding? Here's an example of the modulus with 257 bytes:
00 B7 55 AA 3F 14 89 BC CE ED AF 80 1C 54 2A DF
AB 3C 6A 44 B4 55 58 90 0E 0D 32 96 E6 EF 35 2D
AD B7 44 A7 AB CE 6F D3 BB 9D B4 4B FD 0A DE 87
96 03 55 23 81 49 FE 1B 3E CE 62 B6 2F B1 4C 33
E4 F8 C2 09 5F 0E 10 78 22 D0 F3 C9 BF B9 AC AC
11 00 17 28 09 23 10 D5 8A C9 2B E2 86 96 A7 E2
57 68 D7 3B 63 BE 74 ED B8 02 E2 63 EF F5 40 85
0C A6 9F D0 B6 88 36 8B 4E 6B 35 27 BE 11 CC C8
C3 0A 66 25 E0 AB B6 DD 6D E6 2B AF 9E 1C D7 11
CE 5F E7 C8 1F EB 3D 79 B3 B2 E1 FF D8 20 6D 76
A2 43 9E 20 67 58 97 39 46 D8 73 F6 F0 76 01 E0
61 8E 4A EE C4 03 A6 44 C7 D3 50 E3 C8 62 CF 33
D1 37 6B 85 F5 D4 3C 6D 1F 1A 14 B3 30 B5 E0 82
A5 94 83 4F 7A 17 DA 86 2B F7 2A 47 A3 5F D2 D5
7B 96 32 86 27 5E 2A 6A 85 6E C6 24 15 A9 09 65
BB 04 8C 0D 39 F7 15 D4 F0 F8 5F 0F B0 1D A7 2F
D7
Here's an example of the "D" parameter that is not padded with a leading 0x00:
04 07 EF 8A 5D 88 3D C7 8B 00 5D DF C1 96 03 BE
FF 20 1D 0C A8 07 BF 7B 1F 9D 2A 26 3F C2 3A 93
E4 40 B5 33 18 E1 EA 94 E8 7D C0 61 EF F8 3E A0
F4 C7 CD 75 0D 4C 72 0A EA 7C CF 26 B3 4E 4A A1
D1 3A 6A FA 55 11 D5 A2 66 57 C5 EA DA 49 4A AB
41 06 41 52 1A 1C 47 A5 BA 90 A5 75 72 20 94 E0
79 24 AA 60 A2 12 6E 1B AA AC 91 A7 F8 0B 88 21
64 14 85 81 4D F3 6D 12 B7 56 BE DD F6 04 3B B1
CC 95 A6 8C 9D A6 8D BF 05 C1 72 A4 0B 03 75 F6
40 B6 8E 25 91 3D 87 84 CD 23 EF 2C 29 13 DD A7
75 6E 48 F4 DE 49 98 4F B7 09 CF 5A A3 F5 39 05
37 C8 2B 79 64 F0 B8 AD 11 EF 79 FD 78 C0 6B 2B
50 7F DE BC 59 3D D1 A1 90 59 B7 7E 57 B4 2C A0
D2 20 D2 D6 7C 4A B3 3C 63 5D FA E6 67 18 58 AC
F3 EF 0E E1 C0 C9 B6 D9 8C D1 8E 3D CE 8A FF F0
12 BF C2 FE 72 DC 07 E4 3C 00 5B BE 05 D9 5A 61
And the DP parameter without leading 0:
3E 50 B2 28 A3 B1 71 F3 D5 31 B1 2D FD B3 60 4B
57 F8 C1 46 C7 89 B7 95 F4 7D AE 54 F2 EA 11 98
F7 61 93 30 50 D9 24 19 BF 7F 06 19 DB 97 01 06
8B 20 D7 7A 5E 1A FA 76 9A 0E 27 46 AB FF 25 3C
74 61 E2 9B 3E CE A5 F9 58 40 70 15 94 F2 58 3E
DB E4 90 91 3C 50 B0 24 8F C7 A7 55 EB E3 59 A7
5D 01 19 29 4F F9 F9 E6 EB 78 D1 93 14 61 E4 5C
36 D7 E7 82 58 E7 C5 60 21 F3 1E 5A D4 49 C6 D1
The RSA modulus is a positive number, and ASN.1 integers are all signed.
So if the leading 0x00 wasn't there, this byte encoding would represent a negative number because the first byte would have the high bit set (0xB7 >= 0x80). As a consequence, the 0x00 gets inserted into the DER data stream.
.NET's representation is based on the Windows CAPI representation. CAPI uses domain knowledge to know that the values are all unsigned integers, and then omits the leading 0x00 bytes. So it's up to whoever translates between the DER data and the .NET/CAPI data to add or remove the bytes as needed.
These values are encoded as INTEGER ASN type which uses two-complement notation. That is if most significant bit is set to 1, then the number is negative. However, all numbers (modulus, exponents, primes) in key are positive numbers and prepended with extra leading zero octet to denote positive integer. If the most significant bit is already set to 0, then no extra bytes are added.

How to read .gif image file in matlab ? I am reading it with imread command but it is not showing the same color image which is the original [duplicate]

This is my original image:
But when I load it up on MATLAB and use imshow() on it, this is how I see it:
This is the code I'm using:
I=imread('D:\Matty\pout.gif')
imshow(I)
Forget what I said earlier. It has to do with the colormap. The image seems to have a funky colormap. Generally you should be able to read the colormap with [X, map] = imread(...), but there is some clipping of the data that I don't fully understand.
I copied the colormap manually out of the raw data from a hexeditor and saved it as gif_colormap.txt
B1 B1 B1 AF AF AF AB AB AB A9 A9 A9 A7 A7 A7 A3 A3 A3 A1 A1 A1 9F 9F
9F 9D 9D 9D 9B 9B 9B 99 99 99 97 97 97 95 95 95 93 93 93 91 91 91 8F
8F 8F 8B 8B 8B 89 89 89 85 85 85 83 83 83 7F 7F 7F 7D 7D 7D 7B 7B 7B
79 79 79 77 77 77 75 75 75 71 71 71 6D 6D 6D 6B 6B 6B 69 69 69 67 67
67 65 65 65 63 63 63 61 61 61 5F 5F 5F 5D 5D 5D 5B 5B 5B 59 59 59 57
57 57 53 53 53 4D 4D 4D 4B 4B 4B E0 E0 E0 DC DC DC DA DA DA D6 D6 D6
D4 D4 D4 D2 D2 D2 D0 D0 D0 CE CE CE CC CC CC CA CA CA C8 C8 C8 C4 C4
C4 C2 C2 C2 C0 C0 C0 BE BE BE BA BA BA B8 B8 B8 B6 B6 B6 B4 B4 B4 B2
B2 B2 B0 B0 B0 AE AE AE AC AC AC AA AA AA A6 A6 A6 A4 A4 A4 A2 A2 A2
A0 A0 A0 9E 9E 9E 9C 9C 9C 9A 9A 9A 96 96 96 94 94 94 92 92 92 90 90
90 8E 8E 8E 8A 8A 8A 88 88 88 86 86 86 84 84 84 82 82 82 80 80 80 7E
7E 7E 7A 7A 7A 78 78 78 74 74 74 72 72 72 70 70 70 6E 6E 6E 6C 6C 6C
6A 6A 6A 66 66 66 62 62 62 5E 5E 5E 56 56 56 54 54 54 52 52 52 50 50
50 4E 4E 4E 4A 4A 4A DF DF DF DD DD DD DB DB DB D7 D7 D7 D5 D5 D5 D3
D3 D3 D1 D1 D1 CF CF CF CD CD CD C9 C9 C9 C7 C7 C7 C5 C5 C5 C3 C3 C3
C1 C1 C1 BD BD BD BB BB BB B9 B9 B9 B5 B5 B5 B3 B3 B3
Then I read in the new colormap and set it manually
fid = fopen('gif_colormap.txt', 'r')
A = fscanf(fid, '%x ');
fclose(fid);
my_map = reshape(A,3,121)'
im = imread('pout.gif');
%colormap has to be between 0 and 1
my_map = (my_map-min(my_map(:)))/max(my_map(:));
imshow(im,[])
%set colormap manually
colormap(my_map);
GIF is indexed format, and each image can have its own colormap. So you need to read the colormap together with the image:
[I, Imap] = imread('D:\Matty\pout.gif');
imshow(I,Imap)
I've tested it on your image and it works very well. i don't understand what was the problem #Lucas described in his answer.

Play WS, how to transmit non English symbols?

I do some requests with scala and play ws.
One of the requests has non English characters (Russian in my case)
Query looks like this:
val data = "query=" + "Россия, Москва, шоссе Энтузиастов, 21с1" + "&sr=" + Json.obj("wkid" -> 4326)
val futureResponse: Future[WSResponse] = WS.url("http://someservise.com/").post(data)
They query doesn't work, but looks like it must.
In this case, I sniff packages with wireshark. And they show me this request like this:
query=, , , 211&sr={"wkid":4326}
and in hex:
0000 71 75 65 72 79 3d d0 a0 d0 be d1 81 d1 81 d0 b8 query=..........
0010 d1 8f 2c 20 d0 9c d0 be d1 81 d0 ba d0 b2 d0 b0 .., ............
0020 2c 20 d1 88 d0 be d1 81 d1 81 d0 b5 20 d0 ad d0 , .......... ...
0030 bd d1 82 d1 83 d0 b7 d0 b8 d0 b0 d1 81 d1 82 d0 ................
0040 be d0 b2 2c 20 32 31 d1 81 31 26 73 72 3d 7b 22 ..., 21..1&sr={"
0050 77 6b 69 64 22 3a 34 33 32 36 7d wkid":4326}
What I need to do, to transmit this query right?

Why doesn't my image load properly in MATLAB?

This is my original image:
But when I load it up on MATLAB and use imshow() on it, this is how I see it:
This is the code I'm using:
I=imread('D:\Matty\pout.gif')
imshow(I)
Forget what I said earlier. It has to do with the colormap. The image seems to have a funky colormap. Generally you should be able to read the colormap with [X, map] = imread(...), but there is some clipping of the data that I don't fully understand.
I copied the colormap manually out of the raw data from a hexeditor and saved it as gif_colormap.txt
B1 B1 B1 AF AF AF AB AB AB A9 A9 A9 A7 A7 A7 A3 A3 A3 A1 A1 A1 9F 9F
9F 9D 9D 9D 9B 9B 9B 99 99 99 97 97 97 95 95 95 93 93 93 91 91 91 8F
8F 8F 8B 8B 8B 89 89 89 85 85 85 83 83 83 7F 7F 7F 7D 7D 7D 7B 7B 7B
79 79 79 77 77 77 75 75 75 71 71 71 6D 6D 6D 6B 6B 6B 69 69 69 67 67
67 65 65 65 63 63 63 61 61 61 5F 5F 5F 5D 5D 5D 5B 5B 5B 59 59 59 57
57 57 53 53 53 4D 4D 4D 4B 4B 4B E0 E0 E0 DC DC DC DA DA DA D6 D6 D6
D4 D4 D4 D2 D2 D2 D0 D0 D0 CE CE CE CC CC CC CA CA CA C8 C8 C8 C4 C4
C4 C2 C2 C2 C0 C0 C0 BE BE BE BA BA BA B8 B8 B8 B6 B6 B6 B4 B4 B4 B2
B2 B2 B0 B0 B0 AE AE AE AC AC AC AA AA AA A6 A6 A6 A4 A4 A4 A2 A2 A2
A0 A0 A0 9E 9E 9E 9C 9C 9C 9A 9A 9A 96 96 96 94 94 94 92 92 92 90 90
90 8E 8E 8E 8A 8A 8A 88 88 88 86 86 86 84 84 84 82 82 82 80 80 80 7E
7E 7E 7A 7A 7A 78 78 78 74 74 74 72 72 72 70 70 70 6E 6E 6E 6C 6C 6C
6A 6A 6A 66 66 66 62 62 62 5E 5E 5E 56 56 56 54 54 54 52 52 52 50 50
50 4E 4E 4E 4A 4A 4A DF DF DF DD DD DD DB DB DB D7 D7 D7 D5 D5 D5 D3
D3 D3 D1 D1 D1 CF CF CF CD CD CD C9 C9 C9 C7 C7 C7 C5 C5 C5 C3 C3 C3
C1 C1 C1 BD BD BD BB BB BB B9 B9 B9 B5 B5 B5 B3 B3 B3
Then I read in the new colormap and set it manually
fid = fopen('gif_colormap.txt', 'r')
A = fscanf(fid, '%x ');
fclose(fid);
my_map = reshape(A,3,121)'
im = imread('pout.gif');
%colormap has to be between 0 and 1
my_map = (my_map-min(my_map(:)))/max(my_map(:));
imshow(im,[])
%set colormap manually
colormap(my_map);
GIF is indexed format, and each image can have its own colormap. So you need to read the colormap together with the image:
[I, Imap] = imread('D:\Matty\pout.gif');
imshow(I,Imap)
I've tested it on your image and it works very well. i don't understand what was the problem #Lucas described in his answer.

S-Box used in AES 128 bit CFB mode of encryption

Can anybody give me the S-Box used for 128 bit AES CFB mode of encryption.
Will this S-Box be same for every 128 -bit AES CFB Implementation.
Thanks
Here it is:
| 0 1 2 3 4 5 6 7 8 9 a b c d e f
---|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|
00 |63 7c 77 7b f2 6b 6f c5 30 01 67 2b fe d7 ab 76
10 |ca 82 c9 7d fa 59 47 f0 ad d4 a2 af 9c a4 72 c0
20 |b7 fd 93 26 36 3f f7 cc 34 a5 e5 f1 71 d8 31 15
30 |04 c7 23 c3 18 96 05 9a 07 12 80 e2 eb 27 b2 75
40 |09 83 2c 1a 1b 6e 5a a0 52 3b d6 b3 29 e3 2f 84
50 |53 d1 00 ed 20 fc b1 5b 6a cb be 39 4a 4c 58 cf
60 |d0 ef aa fb 43 4d 33 85 45 f9 02 7f 50 3c 9f a8
70 |51 a3 40 8f 92 9d 38 f5 bc b6 da 21 10 ff f3 d2
80 |cd 0c 13 ec 5f 97 44 17 c4 a7 7e 3d 64 5d 19 73
90 |60 81 4f dc 22 2a 90 88 46 ee b8 14 de 5e 0b db
a0 |e0 32 3a 0a 49 06 24 5c c2 d3 ac 62 91 95 e4 79
b0 |e7 c8 37 6d 8d d5 4e a9 6c 56 f4 ea 65 7a ae 08
c0 |ba 78 25 2e 1c a6 b4 c6 e8 dd 74 1f 4b bd 8b 8a
d0 |70 3e b5 66 48 03 f6 0e 61 35 57 b9 86 c1 1d 9e
e0 |e1 f8 98 11 69 d9 8e 94 9b 1e 87 e9 ce 55 28 df
f0 |8c a1 89 0d bf e6 42 68 41 99 2d 0f b0 54 bb 16
It can also be found in FIPS Pub 197, the official standard.
And yes, it is exactly the same for every implementation of AES. Otherwise you wouldn't be able to encrypt something others could decrypt or vice-versa.