On 24 Sep 2007 at 21:06, Roger Hui said:

> Suppose
> 
> c=: ?256
> d=: 16+?16
> e=: a.{~ c (d b.) i.256
> NB. e is used as:   map=: e {~ a. i. ]
> 
> Can you recover c and d given e?  c and d are not
> necessarily unique so it suffices to compute c1 and d1
> such that   e -: a.{~ c1 (d1 b.) i.256 .

Using "c,d" to denote the original values and "C,D" to denote possible 
outputs from a "decode" function,

If (d e. 16 19 28 31) then (#~.e = 1). A suitable (D,C) is then (19, 
a.i.{.e).

If (d e. 21 22 25 26) then (#~.e = 256) (and e will be either a. or 
|.a.). A suitable (D,C) is (22, a.i.{.e).

If (c e. 0 255), #~.e is again either 1 or 256 and the same solutions 
apply.

Other values of d fall into pairs such that, in each pair, a given c with 
one d produces the same e as the bit-complement of that c with the other 
d. These pairs are (17,20),(18,24),(23,29) and (27,30). Examination of 
the bit patterns of these numbers will reveal the reason.

Furthermore, c and d may be deduced from (({.,{:)e). One of these will be 
equal to (c{a.) (or to its bit-complement) and the other to either (0{a.) 
or (255{a.). The value of (0 255 i. a.i. ({.,{:)e) maps onto suitable d 
values as follows:
 0 2 -> 17
 2 0 -> 18
 2 1 -> 23
 1 2 -> 27

Putting these together:
----
decode =: 3 : 0
NB. returns (d,c)
select. # ~. y
NB. (d e. 16 19 28 31): all items of e identical
 case. 1 do. 19, a. i. {.y
NB. (d e. 21 22 25 26): all different
 case. 256 do. 22, a. i. {.y
NB. (c e. 0 255) covered by one of the above
NB. remaining cases
 case. do. cc =. a.i.({.,{:)y NB. candidates for c
  c =. cc -. 0 255
NB. ghastly kludge coming up ...
  signature =. 0 255 i. cc
  d =. (3 #. signature) { _ _ 17 _ _ 27 18 23
  d,c
end.
)

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to