forked from c3c/E-Safenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cplex_coa.mod
71 lines (52 loc) · 2.09 KB
/
cplex_coa.mod
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
CPLEX model for maximizing printable characters
Representation as a binary integer programming problem
Upside: additional constraints can be specified eg. checksum validity.
Downside: slow. (perhaps other solvers perform better for BIPs?)
Copyright (C) 2014 Jan Laan, Cedric Van Bockhaven
----
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file LICENSE. if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Data
int checksum = ...; // integer checksum
int encrypted_len = ...;
range crange = 0..encrypted_len-1;
int encrypted[crange] = ...; // integer representation of encrypted data
// Decision variables
dvar boolean ckey[0..511][0..7];
dvar boolean plain[crange][0..7];
dvar boolean cipher[crange][0..7];
// Decision expressions
dexpr int plains[i in crange] = sum(b in 0..7) plain[i,b]*ftoi(pow(2,b));
// Objective
maximize sum(i in crange) ((plains[i] >= 32 && plains[i] <= 126)
|| plains[i] == 9 || plains[i] == 10 || plains[i] == 13);
// Subject to
constraints{
forall (i in crange, b in 0..7) {
(cipher[i,b] != ckey[i%512,b]) == plain[i,b];
cipher[i,b] == (encrypted[i] div ftoi(pow(2,b))) % 2;
}
sum(i in 0..511) plains[i] == checksum;
}
// Post-processing
execute {
var out = ""
for (var i = 0; i < encrypted_len; i++) {
if (plains[i] >= 32 && plains[i] <= 126) {
out += String.fromCharCode(plains[i])
} else { out+= "?"}
if (i % 512 == 511) out+="\n";
}
writeln("Res: " + out);
}