|: |. 8 4 $ >: +: i.32 57 49 41 33 25 17 9 1 59 51 43 35 27 19 11 3 61 53 45 37 29 21 13 5 63 55 47 39 31 23 15 7
i.32
produces 32 consecutive numbers, starting with zero;+:
doubles its operand;>:
increases the operand by one;8 4 $
converts the operand into an 8x4 matrix;|.
inverts the main axis of the matrix, i.e. rearranges the lines in reverse order;|:
transposes the matrix. i.32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 +: i.32 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 >: +: i.32 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 8 4 $ >: +: i.32 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 |. 8 4 $ >: +: i.32 57 59 61 63 49 51 53 55 41 43 45 47 33 35 37 39 25 27 29 31 17 19 21 23 9 11 13 15 1 3 5 7 |: |. 8 4 $ >: +: i.32 57 49 41 33 25 17 9 1 59 51 43 35 27 19 11 3 61 53 45 37 29 21 13 5 63 55 47 39 31 23 15 7
<: |: |. 8 4 $ >: +: i.32 56 48 40 32 24 16 8 0 58 50 42 34 26 18 10 2 60 52 44 36 28 20 12 4 62 54 46 38 30 22 14 6
<:
decrements operand value by one.
(] , <:) |: |. 8 4 $ >: +: i.32 57 49 41 33 25 17 9 1 59 51 43 35 27 19 11 3 61 53 45 37 29 21 13 5 63 55 47 39 31 23 15 7 56 48 40 32 24 16 8 0 58 50 42 34 26 18 10 2 60 52 44 36 28 20 12 4 62 54 46 38 30 22 14 6
(fgh) y <-> (fy) g (hy)
(] |: |. 8 4 $ >: +: i.32) , (<: |: |. 8 4 $ >: +: i.32)
]
simply returns its right operand.
,
“straightens” the matrix into an array, which we use when we obtain the final formula for the initial permutation matrix: P =: , (] , <:) |: |. 8 4 $ >: +: i.32
x (fgh) y <-> (xfy) g (xhy)
(i.64) ([ * =/~) P
/
and ~
are used: the first applies a verb to each element of the array, and the second changes the operands in places, so
xf~ y <-> yfx
u/ y
, where u
is a dyadic verb, J will substitute it between all elements of y:
+/ 1 2 3 <-> 1 + 2 + 3
iP =: +/ (i.64) ([ * =/~) P
}: |: i. 8 8 0 8 16 24 32 40 48 56 1 9 17 25 33 41 49 57 2 10 18 26 34 42 50 58 3 11 19 27 35 43 51 59 4 12 20 28 36 44 52 60 5 13 21 29 37 45 53 61 6 14 22 30 38 46 54 62
i. 8 8
i. 8 8
instead of an array of numbers immediately creates a matrix of 8x8 consecutive numbers.}:
throws out the last element of the array / row of the matrix.|.
then swap the whole string. In our case, you need to change the rank of the verb so that it works with each individual line:
|."1 }: |: i. 8 8 56 48 40 32 24 16 8 0 57 49 41 33 25 17 9 1 58 50 42 34 26 18 10 2 59 51 43 35 27 19 11 3 60 52 44 36 28 20 12 4 61 53 45 37 29 21 13 5 62 54 46 38 30 22 14 6
"
changes the rank of a verb. A rank of 0 means that the verb is applied to each element of the operand, 1 to the row and 2 to the whole matrix.
G
- it will be more convenient.
4 14 $ (28 {. , G), (24 {. , |. G), (27 19 11 3) 56 48 40 32 24 16 8 0 57 49 41 33 25 17 9 1 58 50 42 34 26 18 10 2 59 51 43 35 62 54 46 38 30 22 14 6 61 53 45 37 29 21 13 5 60 52 44 36 28 20 12 4 27 19 11 3
N {. y
N {. y
pulls the first N
elements out of y
.
G =: |."1 }: |: i. 8 8 G =: (28 {. , G), (24 {. , |. G), (27 19 11 3)
s =:1, (14 $ 1, 6 $ 2), 1
i. 8 4
i. 8 4
: the left column is cyclically shifted down and put to the matrix on the right, the right - move up and put on the left.
E =: |: i. 8 4 E =: , |: (_1 |. _1 { E) , E , (1 |. 0 { E)
x |. y
x |. y
performs a cyclic shift of the array.
f =: dyad : '|. #: S {~ < x, ((5{y)++:0{y), (+/(2^i.4)*}:}.y)' " 0 1 fS =: monad : '}. |."1 (4 $ 1), (i.8) f 8 6 $, y' " 2
i.8
. Then a row of four units is added to the result, all rows of the matrix are inverted and the first row is deleted.
{
- take from array by index. Operator #:
translates the result into a binary numeral system, which is inverted by the next step.
key
already defined):
binkey =. chr2bin key prmkey =. G { binkey
C =. (28 {. prmkey) |.~"1 0 +/\s
\
: it applies a verb to each prefix of the operand. s 1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1 +/\s 1 2 4 6 8 10 12 14 15 17 19 21 23 25 27 28
D =. (28 }. prmkey) |.~"1 0 +/\s
N }. y
N }. y
drops the first N
elements of y
.
K =. KP {"1 C ," 1 1 D
binkey =. chr2bin key prmkey =. G { binkey C =. (28 {. prmkey) |.~"1 0 +/\s D =. (28 }. prmkey) |.~"1 0 +/\s K =. KP {"1 C ," 1 1 D
bin =. chr2bin plain prm =. P { bin L =. 32 {. prm R =. 32 }. prm
>
"Opens" the box, returning its contents. K =. > 0 { y L =. > 1 { y R =. > 2 { y
L ~: P2 { , fS (0 { K) ~: E { R
step =: monad define K =. > 0 { y L =. > 1 { y R =. > 2 { y (}. K); R ; L ~: P2 { , fS (0 { K) ~: E { R )
result =. step^:(#K) K; L; R
u^:N
, which performs the verb u N times, that is, for example,
u^:4 y <-> uuuuy
R =. > 1 { result L =. > 2 { result bin2chr iP { ,L,R
encode =: dyad define key =. x binkey =. chr2bin key prmkey =. G { binkey C =. (28 {. prmkey) |.~"1 0 +/\s D =. (28 }. prmkey) |.~"1 0 +/\s K =. KP {"1 C ," 1 1 D plain =. y bin =. chr2bin plain prm =. P { bin L =. 32 {. prm R =. 32 }. prm result =. step^:(#K) K; L; R R =. > 1 { result L =. > 2 { result bin2chr iP { ,L,R )
Lorem ipsum dolor sit amet consectetur adipiscing elit.
1!:1
reads the contents of the file plaintext =: 1!:1 < 'in.txt'
' ',~^:(8 - 8| # plaintext) plaintext
plaintext =: (8,~ >. 8 %~ #plaintext) $, ' ',~^:(8 - 8| # plaintext) plaintext
plaintext =: 1!:1 < 'in.txt' plaintext =: (8,~ >. 8 %~ #plaintext) $, ' ',~^:(8 - 8| # plaintext) plaintext
1!:21
opens the file, 1!:22
closes, and 1!:2
writes to the file out =: 1!:21 < 'out.txt' ('habrhabr' encode"1 1 plaintext) 1!:2 out 1!:22 out
4acc c8ad 32dd 96a1 ae9c 2fdc 2025 e3d0 968c 97c0 5544 0944 2d20 2069 f943 f0d4 e98d bdea 71f9 c516 8a0a b034 5641 b0b5 53cc 2355 2fb1 4bde
cipher =: 1!:1 < 'out.txt' cipher =: (8,~ >. 8 %~ #cipher) $, cipher , 'habrhabr' decode"1 1 cipher Lorem ipsum dolor sit amet consectetur adipiscing elit.
If you have any questions, suggestions or corrections to the code, welcome to the comments.
Source: https://habr.com/ru/post/202822/