Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
67 changes: 67 additions & 0 deletions Assignment_2- Affine cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import math

def gcd(a, b):
while b != 0:
a, b = b, a % b
return a

def mod_inverse(a, m):
a = a % m
for x in range(1, m):
if (a * x) % m == 1:
return x
return None # No inverse exists

def affine_encrypt(text, a, b):
result = []
for char in text:
if char.isalpha():
base = ord('A') if char.isupper() else ord('a')
x = ord(char) - base
y = (a * x + b) % 26
result.append(chr(y + base))
else:
result.append(char) # Non-alphabetic characters unchanged
return ''.join(result)

def affine_decrypt(text, a, b):
a_inv = mod_inverse(a, 26)
if a_inv is None:
raise ValueError("Key 'a' has no modular inverse. Choose a different key.")

result = []
for char in text:
if char.isalpha():
base = ord('A') if char.isupper() else ord('a')
y = ord(char) - base
x = (a_inv * (y - b)) % 26
result.append(chr(x + base))
else:
result.append(char) # Non-alphabetic characters unchanged
return ''.join(result)

def main():
print("Affine Cipher")
choice = input("1. Encrypt\n2. Decrypt\nEnter choice (1/2): ")
text = input("Enter text: ")
a = int(input("Enter key 'a' (must be coprime with 26): "))
b = int(input("Enter key 'b': "))

if gcd(a, 26) != 1:
print("Error: 'a' must be coprime with 26 (valid keys: 1,3,5,7,9,11,15,17,19,21,23,25).")
return

if choice == '1':
ciphertext = affine_encrypt(text, a, b)
print(f"Encrypted: {ciphertext}")
elif choice == '2':
try:
plaintext = affine_decrypt(text, a, b)
print(f"Decrypted: {plaintext}")
except ValueError as e:
print(e)
else:
print("Invalid choice.")

if __name__ == "__main__":
main()
51 changes: 51 additions & 0 deletions cool_amur1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// C++ code
//
const int pot1Pin = A0;
const int pot2Pin = A1;
const int buzzerPins[3] = {3, 4, 5}; // Buzzer 1, 2, 3
const int ledPins[5] = {6, 7, 8, 9, 10}; // LED sequence
void setup()
{
for (int i = 0; i < 3; i++) {
pinMode(buzzerPins[i], OUTPUT);
}

for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
}
}

void loop()
{
// --- POT1: Buzzer Control ---
int pot1Value = analogRead(pot1Pin);

// Turn off all buzzers first
for (int i = 0; i < 3; i++) {
digitalWrite(buzzerPins[i], LOW);
}

// Activate buzzer based on value range
if (pot1Value <= 341) {
digitalWrite(buzzerPins[0], HIGH);
} else if (pot1Value <= 682) {
digitalWrite(buzzerPins[1], HIGH);
} else {
digitalWrite(buzzerPins[2], HIGH);
}


// --- POT2: LED Sequence Speed Control ---
int pot2Value = analogRead(pot2Pin);

// Map analog value to delay (e.g., 50 to 1000 ms)
int delayTime = map(pot2Value, 0, 1023, 50, 1000);

// Light up one LED at a time
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
digitalWrite(ledPins[j], (i == j) ? HIGH : LOW);
}
delay(delayTime);
}
}
Loading