Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <string.h>
- #include <stdio.h>
- int ehill(const char *msg, int sz_msg, const char *key, int sz_key, int *out) {
- int sz_block = sqrt(sz_key);
- int sz_zeros;
- int i, j;
- if(sz_block * sz_block != sz_key) {
- printf("Incorrect key size\n");
- return -1;
- }
- if(sz_msg % sz_block == 0)
- sz_zeros = 0;
- else
- sz_zeros = sz_block - sz_msg % sz_block;
- memset(out, 0, (sz_msg + sz_zeros)*sizeof(int));
- printf(">>%d\n", (sz_msg + sz_zeros));
- for(i=0; i<sz_msg + sz_zeros; i++) {
- char symb;
- if(i < sz_msg)
- symb = msg[i];
- else
- symb = ' ';
- for(j=0; j<sz_block; j++) {
- /* printf("symb '%c' in %d from %d\n", symb, sz_block*(i/sz_block) + j, (i%sz_block) + j*sz_block); */
- out[sz_block*(i/sz_block) + j] += (symb * key[(i%sz_block) + j*sz_block]);
- out[sz_block*(i/sz_block) + j] = out[sz_block*(i/sz_block) + j] % 257;
- }
- }
- printf(">>%d\n", sz_block*((i-1)/sz_block) + j);
- return sz_msg + sz_zeros - 1; //sz_block*((i-1)/sz_block) + j;
- }
- int main(void) {
- char msg[] = "Hello world\n";
- char key[] = "1111222233334444";
- int out[100];
- int r = ehill(msg, sizeof(msg)-1, key, sizeof(key)-1, out);
- for(; r!=-1; r--)
- printf("%d ", (unsigned int)out[r]);
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement