diff --git a/IS/Lab/Lab1/q1_monosub.py b/IS/Lab/Lab1/q1_monosub.py index b488b55..f7ad074 100644 --- a/IS/Lab/Lab1/q1_monosub.py +++ b/IS/Lab/Lab1/q1_monosub.py @@ -1,6 +1,7 @@ ## ptext = plaintext +## ctext = ciphertext +## mk = multiplicative key ## ak = additive key -from collections import defaultdict def add_cipher_en(ptext, ak): result = "" @@ -76,6 +77,10 @@ def affine_de(ctext, ak, mk): result += chr((((ord(ch) - 97 - ak) * inverse) % 26) + 97) return result +def mult_inverse(mk): + inverse = pow(mk, -1, 26) + return inverse + def operator(argument,ptext,ak,mk): match argument: case '1': @@ -91,6 +96,7 @@ def operator(argument,ptext,ak,mk): print("Multiplicative Key: ", mk) ctext = mult_cipher_en(ptext, mk) print("Ciphertext: ", ctext) + print("Multiplicative Inverse: ", mult_inverse(mk)) print("Decrypted Text: ", mult_cipher_de(ctext, mk)) case '3': print("Affine Cipher") @@ -99,6 +105,7 @@ def operator(argument,ptext,ak,mk): print("Multiplicative Key: ", mk) ctext = affine_en(ptext, ak, mk) print("Ciphertext: ", ctext) + print("Affine Inverse: ", mult_inverse(mk)) print("Decrypted Text: ", affine_de(ctext, ak, mk)) case '4': print("Goodbye") @@ -122,6 +129,5 @@ def main(): op = input("Enter your choice of operation: ") operator(op, ptext, ak, mk) - if __name__ == '__main__': main() diff --git a/IS/Lab/Lab1/q4_hill.py b/IS/Lab/Lab1/q4_hill.py index 9f5c540..f344e5c 100644 --- a/IS/Lab/Lab1/q4_hill.py +++ b/IS/Lab/Lab1/q4_hill.py @@ -1,18 +1,50 @@ -def hill_en(ptext,hk): +import numpy as np -def hill_de(ptext,hk): +def hill_en(ptext, hk): + # all letters to uppercase + ptext = ''.join(c.upper() for c in ptext if c.isalpha()) + + # matrix size + n = int(len(hk)**0.5) + + # key matrix + key = np.array([ord(c) - 65 for c in hk]).reshape(n, n) + + # padding + ptext += 'X' * (-len(ptext) % n) + + # block operation + result = "" + for i in range(0, len(ptext), n): + block = np.array([ord(c) - 65 for c in ptext[i:i+n]]) + encrypted_block = (key @ block) % 26 + result += ''.join(chr(val + 65) for val in encrypted_block) + + return result + +def hill_de(ctext, hk): + # matrix size + n = int(len(hk)**0.5) + + # key matrix and its inverse + key = np.array([ord(c) - 65 for c in hk]).reshape(n, n) + inv_key = np.linalg.inv(key).astype(int) % 26 + + # block operation + result = "" + for i in range(0, len(ctext), n): + block = np.array([ord(c) - 65 for c in ctext[i:i+n]]) + decrypted_block = (inv_key @ block) % 26 + result += ''.join(chr(val + 65) for val in decrypted_block) + + return result def main(): - ptext = input("Kindly enter your desired plaintext: ") - hk = input("Kindly enter the Hill Key: ") - - print("Welcome to the Hill cipher.") - print("Plaintext: ", ptext) - print("Hill Key: ", hk) + ptext = input("Plaintext: ") + hk = input("Hill Key: ") ctext = hill_en(ptext, hk) - print("Ciphertext: ", ctext) - decrypted_text = hill_de(ctext, hk) - print("Decrypted Text: ", decrypted_text) + print(f"Ciphertext: {ctext}") + print(f"Decrypted: {hill_de(ctext, hk)}") if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/IS/Lab/Lab1/q5_john.py b/IS/Lab/Lab1/q5_john.py index e69de29..7384387 100644 --- a/IS/Lab/Lab1/q5_john.py +++ b/IS/Lab/Lab1/q5_john.py @@ -0,0 +1,21 @@ +def main(): + shift = (ord('C') - ord('y')) % 26 + + ctext = "XVIEWYWI" + plaintext = "" + + for char in ctext: + if char.isalpha(): + shifted = ord(char.lower()) - shift + if shifted < ord('a'): + shifted += 26 + plaintext += chr(shifted) + else: + plaintext += char + + print(f"Attack type: Known plaintext attack") + print(f"Ciphertext: {ctext}") + print(f"Decrypted: {plaintext}") + +if __name__ == '__main__': + main() diff --git a/IS/Lab/Lab1/q6_affine_bf.py b/IS/Lab/Lab1/q6_affine_bf.py index e69de29..d743ae5 100644 --- a/IS/Lab/Lab1/q6_affine_bf.py +++ b/IS/Lab/Lab1/q6_affine_bf.py @@ -0,0 +1,47 @@ +def main(): + # Affine cipher: E(x) = (ax + b) mod 26 + # Given: "ab" -> "GL" + # a=0, b=1 -> G=6, L=11 + # So: 6 = (a*0 + b) mod 26 -> b = 6 + # And: 11 = (a*1 + b) mod 26 -> 11 = (a + 6) mod 26 -> a = 5 + + ciphertext = "XPALASXYFGFUKPXUSOGEUTKCDGEXANMGNVS" + + # Try all possible values of a and b for affine cipher + for a in range(1, 26): + # a must be coprime to 26 + if gcd(a, 26) != 1: + continue + + for b in range(26): + # Check if this key produces "ab" -> "GL" + if (a * 0 + b) % 26 == 6 and (a * 1 + b) % 26 == 11: + # Found the key, now decrypt the message + a_inv = mod_inverse(a, 26) + decrypted = "" + for char in ciphertext: + if char.isalpha(): + y = ord(char.upper()) - ord('A') + x = (a_inv * (y - b)) % 26 + decrypted += chr(x + ord('A')) + else: + decrypted += char + + print(f"Key found: a={a}, b={b}") + print(f"Ciphertext: {ciphertext}") + print(f"Decrypted: {decrypted}") + return + +def gcd(a, b): + while b: + a, b = b, a % b + return a + +def mod_inverse(a, m): + for i in range(1, m): + if (a * i) % m == 1: + return i + return None + +if __name__ == '__main__': + main() diff --git a/OS/C/Week11/qq1 b/OS/C/Week11/qq1 new file mode 100755 index 0000000..7a5edc6 Binary files /dev/null and b/OS/C/Week11/qq1 differ diff --git a/OS/C/Week11/qq1.c b/OS/C/Week11/qq1.c index 53d3daa..7142bd8 100644 --- a/OS/C/Week11/qq1.c +++ b/OS/C/Week11/qq1.c @@ -1,234 +1,11 @@ #include #include -#include - -void sstf(int requests[], int n, int head) { - int total_seek = 0; - int completed = 0; - int visited[100] = {0}; - int current = head; - - printf("\nSSTF Disk Scheduling\n"); - printf("Seek Sequence: %d", head); - - while (completed < n) { - int min_distance = INT_MAX; - int min_index = -1; - - for (int i = 0; i < n; i++) { - if (!visited[i]) { - int distance = abs(requests[i] - current); - if (distance < min_distance) { - min_distance = distance; - min_index = i; - } - } - } - - visited[min_index] = 1; - current = requests[min_index]; - total_seek += min_distance; - completed++; - printf(" -> %d", current); - } - - printf("\nTotal Seek Time: %d\n", total_seek); -} - -void scan(int requests[], int n, int head, int disk_size) { - int total_seek = 0; - int direction = 1; // 1 for moving right, 0 for moving left - int current = head; - - printf("\nSCAN Disk Scheduling\n"); - printf("Seek Sequence: %d", head); - - // Sort requests - for (int i = 0; i < n; i++) { - for (int j = 0; j < n - i - 1; j++) { - if (requests[j] > requests[j + 1]) { - int temp = requests[j]; - requests[j] = requests[j + 1]; - requests[j + 1] = temp; - } - } - } - - // Find position of head in sorted array - int index; - for (index = 0; index < n; index++) { - if (requests[index] >= head) - break; - } - - // Move right - for (int i = index; i < n; i++) { - current = requests[i]; - printf(" -> %d", current); - total_seek += abs(current - head); - head = current; - } - - // Move to the end of disk - printf(" -> %d", disk_size - 1); - total_seek += abs(disk_size - 1 - head); - head = disk_size - 1; - - // Move left - for (int i = index - 1; i >= 0; i--) { - current = requests[i]; - printf(" -> %d", current); - total_seek += abs(current - head); - head = current; - } - - printf("\nTotal Seek Time: %d\n", total_seek); -} - -void cscan(int requests[], int n, int head, int disk_size) { - int total_seek = 0; - int current = head; - - printf("\nC-SCAN Disk Scheduling\n"); - printf("Seek Sequence: %d", head); - - // Sort requests - for (int i = 0; i < n; i++) { - for (int j = 0; j < n - i - 1; j++) { - if (requests[j] > requests[j + 1]) { - int temp = requests[j]; - requests[j] = requests[j + 1]; - requests[j + 1] = temp; - } - } - } - - // Find position of head in sorted array - int index; - for (index = 0; index < n; index++) { - if (requests[index] >= head) - break; - } - - // Move right - for (int i = index; i < n; i++) { - current = requests[i]; - printf(" -> %d", current); - total_seek += abs(current - head); - head = current; - } - - // Move to the end of disk - printf(" -> %d", disk_size - 1); - total_seek += abs(disk_size - 1 - head); - - // Move to the beginning - printf(" -> 0"); - total_seek += disk_size - 1; - head = 0; - - // Move right again - for (int i = 0; i < index; i++) { - current = requests[i]; - printf(" -> %d", current); - total_seek += abs(current - head); - head = current; - } - - printf("\nTotal Seek Time: %d\n", total_seek); -} - -void clook(int requests[], int n, int head) { - int total_seek = 0; - int current = head; - - printf("\nC-LOOK Disk Scheduling\n"); - printf("Seek Sequence: %d", head); - - // Sort requests - for (int i = 0; i < n; i++) { - for (int j = 0; j < n - i - 1; j++) { - if (requests[j] > requests[j + 1]) { - int temp = requests[j]; - requests[j] = requests[j + 1]; - requests[j + 1] = temp; - } - } - } - - // Find position of head in sorted array - int index; - for (index = 0; index < n; index++) { - if (requests[index] >= head) - break; - } - - // Move right - for (int i = index; i < n; i++) { - current = requests[i]; - printf(" -> %d", current); - total_seek += abs(current - head); - head = current; - } - - // Move to first request - for (int i = 0; i < index; i++) { - current = requests[i]; - printf(" -> %d", current); - total_seek += abs(current - head); - head = current; - } - - printf("\nTotal Seek Time: %d\n", total_seek); -} - -int main() { - int requests[100], n, head, disk_size, choice; - - printf("Enter the number of disk requests: "); - scanf("%d", &n); - - printf("Enter the disk requests: "); - for (int i = 0; i < n; i++) { - scanf("%d", &requests[i]); - } - - printf("Enter the initial head position: "); - scanf("%d", &head); - - printf("Enter the disk size (0 to size-1): "); - scanf("%d", &disk_size); - - do { - printf("\n\nDisk Scheduling Algorithms\n"); - printf("1. SSTF (Shortest Seek Time First)\n"); - printf("2. SCAN\n"); - printf("3. C-SCAN\n"); - printf("4. C-LOOK\n"); - printf("5. Exit\n"); - printf("Enter your choice: "); - scanf("%d", &choice); - - switch (choice) { - case 1: - sstf(requests, n, head); - break; - case 2: - scan(requests, n, head, disk_size); - break; - case 3: - cscan(requests, n, head, disk_size); - break; - case 4: - clook(requests, n, head); - break; - case 5: - printf("Exiting program...\n"); - break; - default: - printf("Invalid choice!\n"); - } - } while (choice != 5); - - return 0; -} +#include +#define MAX 100 +void sstf(int a[],int n,int h){int v[MAX]={0},t=0,c=h;printf("\nSSTF:\n%d ",c);for(int i=0;i%d ",c);}printf("\nTotal:%d\n",t);} +void sortAsc(int a[],int n){for(int i=0;ia[j]){int t=a[i];a[i]=a[j];a[j]=t;}} +void sortDesc(int a[],int n){for(int i=0;i%d ",c);}if(c){t+=c;c=0;printf("->%d ",c);}for(int i=0;i%d ",c);}printf("\nTotal:%d\n",t);} +void cscan(int a[],int n,int h,int d){int l[MAX],r[MAX],x=0,y=0;for(int i=0;i%d ",c);}if(c!=d-1){t+=abs(d-1-c);c=d-1;printf("->%d ",c);}t+=d-1;c=0;printf("->%d ",c);for(int i=0;i%d ",c);}printf("\nTotal:%d\n",t);} +void clook(int a[],int n,int h){int l[MAX],r[MAX],x=0,y=0;for(int i=0;i%d ",c);}if(x){t+=abs(c-l[0]);c=l[0];printf("->%d ",c);for(int i=1;i%d ",c);}}printf("\nTotal:%d\n",t);} +int main(){int c,n,h,d,a[MAX];printf("1.SSTF\n2.SCAN\n3.CSCAN\n4.CLOOK\nChoice:");scanf("%d",&c);printf("Requests:");scanf("%d",&n);printf("Queue:");for(int i=0;i - -// Define Task structure (simplified for memorization) typedef struct { - int id; // Task ID - int period; // Period (also deadline for simplicity) - int execution_time; // Worst-case execution time (WCET) - // --- Simulation State --- - int remaining_execution; // Remaining execution time for current instance - int absolute_deadline; // Absolute deadline for current instance - int time_to_arrival; // Time until the next instance arrives/is released + int id,p,et,re,ad,ta; } Task; -// --- Global Variables --- -// Define the tasks for the simulation (Example Set) -// Format: {id, Period, ExecutionTime, 0, 0, 0} <-- Initial state values -Task tasks[] = { - {1, 5, 2, 0, 0, 0}, // Task 1: Period=5, Exec Time=2 - {2, 8, 3, 0, 0, 0} // Task 2: Period=8, Exec Time=3 - // Add more tasks here if needed -}; -// Calculate number of tasks automatically -int num_tasks = sizeof(tasks) / sizeof(Task); -// Set simulation duration (e.g., Hyperperiod or a fixed time) -// LCM(5, 8) = 40 -int simulation_time = 40; +Task tasks[]={{1,5,2,0,0,0},{2,8,3,0,0,0}}; +int nt=sizeof(tasks)/sizeof(Task); +int st=40; -// --- Rate Monotonic (RM) Simulation --- -void simulate_rm() { - printf("--- Rate Monotonic Scheduling ---\n"); - // Reset task states for the simulation run - for (int i = 0; i < num_tasks; i++) { - tasks[i].remaining_execution = 0; - tasks[i].absolute_deadline = 0; - tasks[i].time_to_arrival = 0; // All tasks start at time 0 - } - - // Main simulation loop - for (int time = 0; time < simulation_time; time++) { - // 1. Check for task arrivals (release time) - for (int i = 0; i < num_tasks; i++) { - if (tasks[i].time_to_arrival == 0) { - // Check if the previous instance of this task missed its deadline - if (tasks[i].remaining_execution > 0) { - printf("!!! Time %d: Task %d MISSED DEADLINE !!!\n", time, tasks[i].id); - // Simple handling: Continue with the new instance, old one is lost - } - // Release new instance of the task - tasks[i].remaining_execution = tasks[i].execution_time; - tasks[i].absolute_deadline = time + tasks[i].period; // Deadline = Period - tasks[i].time_to_arrival = tasks[i].period; // Set timer for the *next* arrival +void simulate_rm(){ + printf("--- RM ---\n"); + for(int i=0;i0)printf("!T%d:Task%d missed!\n",t,tasks[i].id); + tasks[i].re=tasks[i].et; + tasks[i].ad=t+tasks[i].p; + tasks[i].ta=tasks[i].p; } - tasks[i].time_to_arrival--; // Decrement time until the next arrival for all tasks - } - - // 2. Select highest priority task to run (RM: Shortest Period has highest priority) - int task_to_run = -1; // -1 indicates CPU Idle - int highest_priority = 10000; // Initialize with a low priority (large period) - - for (int i = 0; i < num_tasks; i++) { - // Check if task is ready (has arrived and needs execution) - if (tasks[i].remaining_execution > 0) { - // RM priority check: Lower period value means higher priority - if (tasks[i].period < highest_priority) { - highest_priority = tasks[i].period; - task_to_run = i; // Select this task - } - } - } - - // 3. Execute the selected task (or remain idle) - if (task_to_run != -1) { - // Task selected to run - printf("Time %d: Task %d running\n", time, tasks[task_to_run].id); - tasks[task_to_run].remaining_execution--; // Execute for one time unit - - // Optional: Check if task just finished - // if (tasks[task_to_run].remaining_execution == 0) { - // printf("Time %d: Task %d finished\n", time + 1, tasks[task_to_run].id); - // } - } else { - // No task ready to run - printf("Time %d: CPU Idle\n", time); + tasks[i].ta--; } + int r=-1,hp=10000; + for(int i=0;i0&&tasks[i].p=0){printf("T%d:Task%d run\n",t,tasks[r].id);tasks[r].re--;} + else printf("T%d:Idle\n",t); } - printf("--- RM Simulation Complete ---\n"); } -// --- Earliest Deadline First (EDF) Simulation --- -void simulate_edf() { - printf("\n--- Earliest Deadline First Scheduling ---\n"); - // Reset task states - for (int i = 0; i < num_tasks; i++) { - tasks[i].remaining_execution = 0; - tasks[i].absolute_deadline = 0; - tasks[i].time_to_arrival = 0; - } - - // Main simulation loop - for (int time = 0; time < simulation_time; time++) { - // 1. Check for task arrivals (same as RM) - for (int i = 0; i < num_tasks; i++) { - if (tasks[i].time_to_arrival == 0) { - if (tasks[i].remaining_execution > 0) { - printf("!!! Time %d: Task %d MISSED DEADLINE !!!\n", time, tasks[i].id); - } - tasks[i].remaining_execution = tasks[i].execution_time; - tasks[i].absolute_deadline = time + tasks[i].period; - tasks[i].time_to_arrival = tasks[i].period; +void simulate_edf(){ + printf("\n--- EDF ---\n"); + for(int i=0;i0)printf("!T%d:Task%d missed!\n",t,tasks[i].id); + tasks[i].re=tasks[i].et; + tasks[i].ad=t+tasks[i].p; + tasks[i].ta=tasks[i].p; } - tasks[i].time_to_arrival--; - } - - // 2. Select highest priority task to run (EDF: Earliest Absolute Deadline has highest priority) - int task_to_run = -1; - int earliest_deadline = 10000; // Initialize with a late deadline - - for (int i = 0; i < num_tasks; i++) { - // Check if task is ready - if (tasks[i].remaining_execution > 0) { - // EDF priority check: Lower deadline value means higher priority (earlier deadline) - if (tasks[i].absolute_deadline < earliest_deadline) { - earliest_deadline = tasks[i].absolute_deadline; - task_to_run = i; // Select this task - } - } - } - - // 3. Execute the selected task (same as RM) - if (task_to_run != -1) { - printf("Time %d: Task %d running\n", time, tasks[task_to_run].id); - tasks[task_to_run].remaining_execution--; - // Optional: Check finish - // if (tasks[task_to_run].remaining_execution == 0) { - // printf("Time %d: Task %d finished\n", time + 1, tasks[task_to_run].id); - // } - } else { - printf("Time %d: CPU Idle\n", time); + tasks[i].ta--; } + int r=-1,ed=10000; + for(int i=0;i0&&tasks[i].ad=0){printf("T%d:Task%d run\n",t,tasks[r].id);tasks[r].re--;} + else printf("T%d:Idle\n",t); } - printf("--- EDF Simulation Complete ---\n"); } -// --- Main Function --- -int main() { - // Run Rate Monotonic simulation - simulate_rm(); - - // Run Earliest Deadline First simulation - simulate_edf(); - - return 0; // Indicate successful execution -} +int main(){simulate_rm();simulate_edf();return 0;}