void key_sched(char* key, char dest[NUM_ROUNDS][SUBKEY_SIZE]){ int i; char k[KEY_BITS]; // Apply PC-1 // i figured we should map bits as bytes so it is easier to address them in the shifts and PC-2 for(i = 0; i < KEY_BITS; i++){ int byte_index = (PC1[i]-1) / 8; int bit_offset = 7 - ((PC1[i]-1) % 8); k[i] = (key[byte_index] & (1 << bit_offset)) ? 1 : 0; } for(i = 0; i < NUM_ROUNDS; i++){ // no need to split key block into halves because we can just do pointer arithmetic when we shift // shift once rotate_array(k, HALF_BITS); rotate_array(k + HALF_BITS, HALF_BITS); // shift twice in all rounds except 1,2,9,16 if(! (i == 0 || i == 1 || i == 8 || i == 15)){ rotate_array(k, HALF_BITS); rotate_array(k + HALF_BITS, HALF_BITS); } // copy key and apply PC-2 to it, saving as subkey // first zero out subkey so we can do bit manipulation without worrying about garbage int j; for(j = 0; j < SUBKEY_SIZE; j++) dest[i][j] = 0; // copy bits into subkey using PC-2 mapping (using real bits again instead of bytes) for(j = 0; j < SUBKEY_BITS; j++){ int byte_index = j / 8; int bit_offset = 7 - (j % 8); dest[i][byte_index] = dest[i][byte_index] | (k[PC2[j]-1] << bit_offset); } } }
int main(){ int n = 7; char array[n]; int k = 3; int i = 0; for(i = 0; i < n; i++) { array[i] = 'a' + i; } print_array(array, n); rotate_array(array, n, k); //rotate_str(array, n, k); print_array(array, n); return 0; }
int main() { int **arr; int i = 0, j = 0; int c = 1; int size = 4; arr = (int **)malloc(sizeof(int*) * size); for (i = 0; i < size; i++) { arr[i] = (int *)malloc(sizeof(int) * size); } for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) arr[i][j] = c++; print_array(arr, 4); rotate_array(arr, 4); printf("\n\n"); print_array(arr, 4); }
int main(int argc, char** argv) { int cnt=20; if (argc>1) { cnt = atoi(argv[1]); } srand(time(NULL)); for(int n=0; n<=cnt; n++) { printf("--------------------------------------\n"); int *a = new int[cnt]; for(int i=0; i<cnt; i++){ a[i]=i*2; } //printArray(a, cnt); int rotate = random() % cnt; //rotate=2; //printf("rotate=%d\n", rotate); rotate_array(a, cnt, rotate); printArray(a, cnt); int target = random() % (2*cnt); //target=6; printf("target=%d\n", target); int idx = search(a, cnt, target); if ( idx<0 ){ printf("not found!\n"); }else{ printf("a[%d] = %d\n", idx, a[idx]); } delete[] a; } return 0; }