-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutationencryption.c
65 lines (53 loc) · 1.16 KB
/
permutationencryption.c
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
#include "stdlib.h"
#include "stdio.h"
#include "stdbool.h"
#include "string.h"
#include "assert.h"
void encrypt(char* T, char* S, int* K, int n) {
while (*S) {
for (int i=0; i<n; i++)
T[i] = S[K[i]-1];
T+=n; S+=n;
}
*T = *S;
assert(*T == '\0');
}
void test() {
char* A[] = {"applesauce", "dog", "hill", "happy", "hilarious",
"yassine", "keyboard", "pictures", "actors", "unit",
"bootstrap", "Pochhammer", "Ramanujan"};
}
void production() {
int n;
int K[21];
char* S = malloc(2<<26);
char* T = malloc(2<<26);
while (scanf("%d", &n) > 0 && n > 0) {
//read in key
for (int i=0; i<n; i++)
scanf("%d", &K[i]);
fgets(S, 8, stdin); // consume past newline
fgets(S, 2<<26, stdin);
// chomp
int m = strlen(S);
S[m-1] = '\0';
m = strlen(S);
//pad things out
if (m%n > 0) {
int defect = n-(m%n);
for (int i=m; i< m+defect; i++)
S[i] = ' ';
S[m+defect] = '\0';
}
assert(strlen(S)%n == 0);
//encrypt
encrypt(T, S, K, n);
printf("'%s'\n", T);
}
free(S); free(T);
}
int main() {
production();
/* test(); */
return 0;
}