Joseph Haugh
University of New Mexico
What is the output of the following snippet of code?
int x = 10, y = 20;
int *ptr = &x;
*ptr += 5;
ptr = &y;
*ptr *= 2;
printf("%d %d\n", x, y);
15 40
What is the output of the following snippet of code?
char str[] = "Hello, world!";
char *ptr = str;
while (*ptr != '\0') {
if (*ptr == 'o') *ptr = 'O';
ptr++;
}
printf("%s\n", str);
HellO, wOrld!
What is the output of the following snippet of code?
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += *(ptr + i);
}
printf("sum: %d\n", sum);
sum: 15
What is the output of the following snippet of code?
int x = 5;
int *ptr1 = &x;
int **ptr2 = &ptr1;
**ptr2 = 10;
printf("%d\n", x);
10
flee at once. we are discovered!
Enciphers to
SIAA ZQ LKBA. VA ZOA RFPBLUAOAR!
OENp(ENTE#X@EN#zNp(ENCL]pEnN7p-pE;8N]LN}
dnEdNp#Nz#duN-Nu#dENXEdzE9pNCL]#L8NE;p-b
@];(N0G;p]9E8N]L;GdENn#uE;p]9Nld-L/G]@]p
_8NXd#|]nENz#dNp(EN9#uu#LNnEzEL;E8NXd#u#
pENp(ENQELEd-@NOE@z-dE8N-LnN;E9GdENp(EN^
@E;;]LQ;N#zN<]bEdp_Np#N#Gd;E@|E;N-LnN#Gd
NT#;pEd]p_8Nn#N#dn-]LN-LnNE;p-b@];(Np(];
N5#L;p]pGp]#LNz#dNp(ENCL]pEnN7p-pE;N#zN)
uEd]9-D
In English,
.We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defense, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.
=51
e=39
t=28
o=24
[ N] [!%] ["Z] [#1] [$f] [%=] [&r] ['I] [( ] [)U] [*,]
[+a] [,8] [-m] [.D] [/y] [0P] [1'] [2\] [33] [4h] [5?]
[6t] [7K] [8"] [9W] [:.] [;c] [<:] [=o] [>F] [?{] [@R]
[A)] [B^] [C5] [Dj] [EA] [Fv] [GM] [H$] [IY] [J0] [Ke]
[L<] [Mq] [NH] [O}] [PT] [Q+] [R`] [S7] [Tl] [UC] [Vx]
[WO] [X&] [Y[] [Z2] [[g] [\>] []s] [^J] [_!] [`V] [a-]
[bb] [c9] [dn] [eE] [fz] [gQ] [h(] [i]] [j4] [ki] [l@]
[mu] [nL] [o#] [pX] [q/] [rd] [s;] [tp] [uG] [v|] [wS]
[x*] [y_] [z6] [{k] [|B] [}w]
#define ASCII_START 32
#define ASCII_END 126
#define MAP_SIZE 94
char map[MAP_SIZE];
void buildMap(char seed) {
int i;
int m = 94;
int a = 189;
int c = 53;
int n = seed;
for (i = 0; i < MAP_SIZE; i++) {
n = (a * n + c) % m;
map[i] = n + ASCII_START;
}
}
void printMap() {
int i;
for (i = 0; i < MAP_SIZE; i++) {
printf("[%c%c] ", i + ASCII_START, map[i]);
if ((i % 16) == 15) printf("\n");
}
printf("\n");
}
void main() {
char c = getchar();
buildMap(c);
printMap();
int i = 0;
while (c != EOF) {
if (c < ASCII_START) break;
if (c > ASCII_END) break;
if (i % 40 == 0) printf("\n");
printf("%c", map[c - ASCII_START]);
c = getchar();
i++;
}
printf("\n");
}
A book cipher is a cipher in which the key is some aspect of a book or other piece of text.
A message is typically encoded by three numbers for each letter:
Where the encoded letter is the first letter of the specified word.
It is typically essential that both correspondents not only have the same book, but the same edition.
A Linear Congruential Generator is one of the oldest and best known pseudorandom number generator algorithms:
Xn + 1 = (aXn + c) mod m
where Xn is the sequence of pseudorandom values, and
Example:
Xn + 1 = (7Xn + 11) mod 18, X0 = 0.
X1 = (7(0) + 11) mod 18 = 11
X2 = (7(11) + 11) mod 18 = 16
X3 = (7(16) + 11) mod 18 = 15
X4 = (7(15) + 11) mod 18 = 8
Xn + 1 = (aXn + c) mod m
An LCG will have a full period if and only if:
Source | m | a | c |
---|---|---|---|
Numerical Recipes | 232 | 1664525 | 1013904223 |
Borland C/C++ | 232 | 22695477 | 1 |
glibc (used by GCC) | 231 | 1103515245 | 12345 |
ANSI C: Watcom, Digital Mars, CodeWarrior, IBM VisualAge C/C++ | 231 | 1103515245 | 12345 |
Note: LCG’s do not always return all of the bits in the values they produce.
Example: Java produces 48 bits, but only returns the 32 most significant.
void main () {
unsigned long m = 1 << 31;
unsigned long a = 1103515245;
unsigned long c = 12345;
unsigned long x = 123456;
int i;
for (i = 1; i <= 100; i++) {
x = (a * x + c) % m;
printf("%20lu\n", x);
}
}
136235578099065
15519887047211639486
13838650204930187039
3346717808905085548
3049859088482533429
7398829542172257482
15952872801263783483
9897407542008592728
12436645181455133361
8967167257519066006
15551416184959193879
14392011937321128708
7012146815830884589
837494824685357858
9089108537697995187
4486637365379619696
10706175307392302825
3333036225721595758
15798734497693109775
1184521088365346460
762097951296315557
14053214745497771130
13752776674615655467
4036971838066272392
13168213029112046113
2176075275402511942
11251131393832824839
9338128740141843764
7951965562090585949
void main () {
int m = 18;
int a = 7;
int c = 1;
int x = 0;
int i;
for (i = 1; i <= 28; i++) {
printf("%4d\n", x);
x = (a * x + c) % m;
}
}
0
1
8
3
4
11
6
7
14
9
10
17
12
13
2
15
16
5
0
1
8
3
4
11
6
7
14
9
void main() {
int m = 18;
int a = 6;
int c = 1;
int x = 0;
int i;
for (i = 1; i <= 14; i++)
{
printf("%4d\n", x);
x = (a * x + c) % m;
}
}
This LCG does NOT have a full period.
How can this be known before computing it?
(6 × 7 + 1) mod 18
(42 + 1) mod 18
43 mod 18
7
0
1
7
7
7
7
7
7
7
7
7
7
7
7
void main() {
int m = 10;
int a = 2;
int c = 1;
int x = 7;
int i;
for (i = 1; i <= 4; i++) {
printf("%d ", x);
x = (a * x + c) % m;
}
printf("\n");
}
The output is:
void main() {
int m = 10;
int a = 2;
int c = 1;
int x = 7;
int i;
for (i = 1; i <= 4; i++) {
printf("%d ", x);
x = (a * x + c) % m;
}
printf("\n");
}
The output is: