From df44199a02067e80b9da07ee187cece5165f0561 Mon Sep 17 00:00:00 2001 From: sherlock Date: Tue, 7 Oct 2025 03:32:17 +0530 Subject: [PATCH] IS Lab --- IS/Lab/Lab7/HomomorphicRSA.py | 50 +++++++++ IS/Lab/Lab7/Pallier.py | 103 +++++++++++++++++ IS/Lab/Lab8/PKSE/documents/doc1.md | 5 + IS/Lab/Lab8/PKSE/documents/doc10.md | 5 + IS/Lab/Lab8/PKSE/documents/doc2.md | 5 + IS/Lab/Lab8/PKSE/documents/doc3.md | 5 + IS/Lab/Lab8/PKSE/documents/doc4.md | 5 + IS/Lab/Lab8/PKSE/documents/doc5.md | 5 + IS/Lab/Lab8/PKSE/documents/doc6.md | 5 + IS/Lab/Lab8/PKSE/documents/doc7.md | 5 + IS/Lab/Lab8/PKSE/documents/doc8.md | 5 + IS/Lab/Lab8/PKSE/documents/doc9.md | 5 + IS/Lab/Lab8/PKSE/encrypted_index.pkl | Bin 0 -> 18227 bytes IS/Lab/Lab8/PKSE/pkse.py | 161 +++++++++++++++++++++++++++ IS/Lab/Lab8/SSE/documents/doc1.md | 5 + IS/Lab/Lab8/SSE/documents/doc10.md | 5 + IS/Lab/Lab8/SSE/documents/doc2.md | 5 + IS/Lab/Lab8/SSE/documents/doc3.md | 5 + IS/Lab/Lab8/SSE/documents/doc4.md | 5 + IS/Lab/Lab8/SSE/documents/doc5.md | 5 + IS/Lab/Lab8/SSE/documents/doc6.md | 5 + IS/Lab/Lab8/SSE/documents/doc7.md | 5 + IS/Lab/Lab8/SSE/documents/doc8.md | 5 + IS/Lab/Lab8/SSE/documents/doc9.md | 5 + IS/Lab/Lab8/SSE/sse.py | 130 +++++++++++++++++++++ 25 files changed, 544 insertions(+) create mode 100644 IS/Lab/Lab7/HomomorphicRSA.py create mode 100644 IS/Lab/Lab7/Pallier.py create mode 100644 IS/Lab/Lab8/PKSE/documents/doc1.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc10.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc2.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc3.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc4.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc5.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc6.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc7.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc8.md create mode 100644 IS/Lab/Lab8/PKSE/documents/doc9.md create mode 100644 IS/Lab/Lab8/PKSE/encrypted_index.pkl create mode 100644 IS/Lab/Lab8/PKSE/pkse.py create mode 100644 IS/Lab/Lab8/SSE/documents/doc1.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc10.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc2.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc3.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc4.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc5.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc6.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc7.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc8.md create mode 100644 IS/Lab/Lab8/SSE/documents/doc9.md create mode 100644 IS/Lab/Lab8/SSE/sse.py diff --git a/IS/Lab/Lab7/HomomorphicRSA.py b/IS/Lab/Lab7/HomomorphicRSA.py new file mode 100644 index 0000000..c4dd073 --- /dev/null +++ b/IS/Lab/Lab7/HomomorphicRSA.py @@ -0,0 +1,50 @@ +from Crypto.PublicKey import RSA +from Crypto.Util.number import bytes_to_long, long_to_bytes + + +def generate_keys(): + key = RSA.generate(2048) + return key + + +def encrypt(message, public_key): + n = public_key.n + e = public_key.e + ciphertext = pow(message, e, n) + return ciphertext + + +def decrypt(ciphertext, private_key): + n = private_key.n + d = private_key.d + plaintext = pow(ciphertext, d, n) + return plaintext + + +# Generate keys +key = generate_keys() +public_key = key.publickey() +private_key = key + +# Original values +m1 = 7 +m2 = 3 + +# Encrypt +c1 = encrypt(m1, public_key) +c2 = encrypt(m2, public_key) +print(f"Ciphertext 1: {c1}") +print(f"Ciphertext 2: {c2}") + +# Homomorphic multiplication +c_product = (c1 * c2) % public_key.n +print(f"Encrypted product: {c_product}") + +# Decrypt result +decrypted_product = decrypt(c_product, private_key) +print(f"Decrypted product: {decrypted_product}") + +# Verify +expected_product = m1 * m2 +print(f"Expected product: {expected_product}") +print(f"Match: {decrypted_product == expected_product}") diff --git a/IS/Lab/Lab7/Pallier.py b/IS/Lab/Lab7/Pallier.py new file mode 100644 index 0000000..c233b23 --- /dev/null +++ b/IS/Lab/Lab7/Pallier.py @@ -0,0 +1,103 @@ +import random +from math import gcd +from sympy import isprime, lcm + + +def generate_prime(bits=512): + while True: + num = random.getrandbits(bits) + if isprime(num): + return num + + +def generate_keypair(bits=512): + p = generate_prime(bits) + q = generate_prime(bits) + + n = p * q + n_squared = n * n + lambda_n = lcm(p - 1, q - 1) + + g = n + 1 + + def L(x): + return (x - 1) // n + + mu = pow(L(pow(g, lambda_n, n_squared)), -1, n) + + public_key = (n, g) + private_key = (lambda_n, mu) + + return public_key, private_key + + +def encrypt(public_key, plaintext): + n, g = public_key + n_squared = n * n + + while True: + r = random.randint(1, n - 1) + if gcd(r, n) == 1: + break + + ciphertext = (pow(g, plaintext, n_squared) * pow(r, n, n_squared)) % n_squared + + return ciphertext + + +def decrypt(public_key, private_key, ciphertext): + n, g = public_key + lambda_n, mu = private_key + n_squared = n * n + + def L(x): + return (x - 1) // n + + plaintext = (L(pow(ciphertext, lambda_n, n_squared)) * mu) % n + + return plaintext + + +def homomorphic_add(public_key, ciphertext1, ciphertext2): + n, g = public_key + n_squared = n * n + + result = (ciphertext1 * ciphertext2) % n_squared + + return result + + +def main(): + print("Paillier Encryption Scheme Implementation\n") + + print("Generating keypair...") + public_key, private_key = generate_keypair(bits=512) + print("Keys generated successfully.\n") + + m1 = 15 + m2 = 25 + print(f"Original integers: {m1} and {m2}") + print(f"Expected sum: {m1 + m2}\n") + + print("Encrypting integers...") + c1 = encrypt(public_key, m1) + c2 = encrypt(public_key, m2) + print(f"Ciphertext of {m1}: {c1}") + print(f"Ciphertext of {m2}: {c2}\n") + + print("Performing homomorphic addition on encrypted values...") + c_sum = homomorphic_add(public_key, c1, c2) + print(f"Encrypted sum: {c_sum}\n") + + print("Decrypting the result...") + decrypted_sum = decrypt(public_key, private_key, c_sum) + print(f"Decrypted sum: {decrypted_sum}\n") + + if decrypted_sum == m1 + m2: + print("✓ Verification successful! The decrypted sum matches the original sum.") + else: + print("✗ Verification failed! The decrypted sum does not match.") + + +if __name__ == "__main__": + main() diff --git a/IS/Lab/Lab8/PKSE/documents/doc1.md b/IS/Lab/Lab8/PKSE/documents/doc1.md new file mode 100644 index 0000000..2643df4 --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc1.md @@ -0,0 +1,5 @@ +# Introduction to Cryptography + +Cryptography is the practice and study of techniques for secure communication. +It involves encryption, decryption, and various security protocols. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc10.md b/IS/Lab/Lab8/PKSE/documents/doc10.md new file mode 100644 index 0000000..f519336 --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc10.md @@ -0,0 +1,5 @@ +# Blockchain and Cryptography + +Blockchain technology relies heavily on cryptographic hash functions. +Bitcoin and other cryptocurrencies use encryption for security. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc2.md b/IS/Lab/Lab8/PKSE/documents/doc2.md new file mode 100644 index 0000000..fae8fba --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc2.md @@ -0,0 +1,5 @@ +# Symmetric Encryption + +Symmetric encryption uses the same key for encryption and decryption. +AES is a popular symmetric encryption algorithm used worldwide. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc3.md b/IS/Lab/Lab8/PKSE/documents/doc3.md new file mode 100644 index 0000000..00c1383 --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc3.md @@ -0,0 +1,5 @@ +# Asymmetric Encryption + +Asymmetric encryption uses a pair of keys: public and private. +RSA and ECC are examples of asymmetric encryption algorithms. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc4.md b/IS/Lab/Lab8/PKSE/documents/doc4.md new file mode 100644 index 0000000..49ac46d --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc4.md @@ -0,0 +1,5 @@ +# Hash Functions + +Hash functions create fixed-size outputs from variable-size inputs. +SHA-256 and MD5 are commonly used hash functions in cryptography. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc5.md b/IS/Lab/Lab8/PKSE/documents/doc5.md new file mode 100644 index 0000000..ef14b12 --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc5.md @@ -0,0 +1,5 @@ +# Digital Signatures + +Digital signatures provide authentication and non-repudiation. +They use asymmetric encryption to verify the sender's identity. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc6.md b/IS/Lab/Lab8/PKSE/documents/doc6.md new file mode 100644 index 0000000..1ccf6ae --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc6.md @@ -0,0 +1,5 @@ +# Paillier Cryptosystem + +Paillier is a probabilistic asymmetric algorithm for public key cryptography. +It provides homomorphic encryption properties for secure computation. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc7.md b/IS/Lab/Lab8/PKSE/documents/doc7.md new file mode 100644 index 0000000..6118304 --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc7.md @@ -0,0 +1,5 @@ +# Public Key Infrastructure + +PKI manages digital certificates and public-key encryption. +It provides a framework for secure communication over networks. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc8.md b/IS/Lab/Lab8/PKSE/documents/doc8.md new file mode 100644 index 0000000..14a5c5c --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc8.md @@ -0,0 +1,5 @@ +# Cryptographic Protocols + +Protocols like TLS and SSL ensure secure communication. +They combine encryption, authentication, and data integrity. + diff --git a/IS/Lab/Lab8/PKSE/documents/doc9.md b/IS/Lab/Lab8/PKSE/documents/doc9.md new file mode 100644 index 0000000..27be380 --- /dev/null +++ b/IS/Lab/Lab8/PKSE/documents/doc9.md @@ -0,0 +1,5 @@ +# Quantum Cryptography + +Quantum cryptography uses quantum mechanics for secure communication. +It provides theoretically unbreakable encryption using quantum key distribution. + diff --git a/IS/Lab/Lab8/PKSE/encrypted_index.pkl b/IS/Lab/Lab8/PKSE/encrypted_index.pkl new file mode 100644 index 0000000000000000000000000000000000000000..d503487213d0aa0ca7adb76acd5cff60102686b4 GIT binary patch literal 18227 zcmbunWn5KJx5i6%w}60jcXxM6N(h_Wo7!|ENOz|+64Kouh|&TAlF|**DSg5BocDK8 zIrqbrFN=*E{_EkHV~#QASop#u(#k;m{^y-Q8vz3ZI06$MuF%YEO&r|qoXkuOf4>%i zVh1#1wgZ5ytwClE2@&X0|NC`iXB*@DSK<@s-u`|klAW`$HOR!!%FI0h2or&+`akbc z{r$4S{j2}|uaFiAVPYLJbvb0SHr>$N^UUzYMc2q16q3+(#1UtNbxcNOsHG%*3B^B@ zX%~q1or{P2`0%&C>}sPJEs_~im-rV*R+d9VK-%7aoA&?T-v-{+(cT&0V3rW(`_`AO zTff!jc*pY=N=8h}{c`rztoV)NOH|m?re+*EH6$hp&~0q+)d*GRXIvc{m-;c>m?@~k z;?S(%uXyQkbg_%^Dl|Iqm_7^t`YDy05?OBty4mn=obD4myz7?PD$&DDO41%#;e?V zJjidar>_w-Fq6|~mqB6m#r>`$Zc%>w;4c?2XJcl96t|K#!nuz$s&+lv;L2UTP zZ_Cio1a$xVbZ|0ryZ;fs*(9tfLBe})djP=@;5o??0($YN6M5^1uT-Cau0>p1iMWd8 zb-f9ofeIy`H@JeTIJJC~pS%dKxJGugluVh9u#Z+y{0MeX>PgKRZUu#hNQFKe4A^ma zCi^V}ANv<4XLU6(2)|dDEs5sSj{=Oo4^=!ahsMA0yCcV1uS*K&k3cYUvjf|j-QTYQ zL-=Au(7$bqY2>NkO?98w*+qrHZcU5Iwlz0n-$^y6E;?68`Ff}uX8?JnK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ZxJtvYoQ%pn8nD;>1Af7GENs*0BGs8Z>(UD3FY z)kb`u^JW&H@#v}gr_H1*UoZ-3@NE%$&??%5rR{3oy-P);%PI)k8Ftcp)@Xa_L!WTK zyk8-t0_+BueZdSlj(p*AtNO|HkNM&UV)*+%`e5ti{KVmMVOfgs8L9Q_2(=bpei0A9 zoNs7T>^9}ZAL-KS`$nR5c=FD~!f^PKa7}gWOm$h6$AXQ^BhIIrwOr|Jh{g}| zufLlS%jhvTb*+u%UN*y$K%DD;J>-ieYB_*Esi-=n$QPfmYaZ|0xWrcxlZUfDAsW|clxME>s!2jO%J6#Gji$m4Rkz0E9@SH7o$hCHwHIC=uH~DlJENhjo z?2AD}cb#~LMV|PGl8~7sW8Wh?qeFdC2r6BLsg4e<NBLaktH{gJ`hl*p3vnyeozJYpfHN*{Tzw|$k_SmC){|iAXhEoseH6xU_-45)u&d> z+>_V_!u3M~T@W&)$D%dkuF8|v)HpJOejIE4vX(gXQBFttX>D@BwL(ScRTP9+7EALQ z>fDM6A9+fbsUsTppiI?_Y^TN3y$mzSj~BAJXl~U+TyCOEZ+~8+&uaad|2MJWm_a&T R+KKlYFcWHjN$kx0{{WV1T-pEt literal 0 HcmV?d00001 diff --git a/IS/Lab/Lab8/PKSE/pkse.py b/IS/Lab/Lab8/PKSE/pkse.py new file mode 100644 index 0000000..38a8a50 --- /dev/null +++ b/IS/Lab/Lab8/PKSE/pkse.py @@ -0,0 +1,161 @@ +import os +import json +import pickle +from collections import defaultdict +from phe import paillier + +# global keys +public_key = None +private_key = None + + +def generate_keys(): + global public_key, private_key + public_key, private_key = paillier.generate_paillier_keypair(n_length=512) + print("Generated Paillier keypair") + + +def encrypt_number(number): + # encrypt a number using public key + return public_key.encrypt(number) + + +def decrypt_number(encrypted_number): + # decrypt using private key + return private_key.decrypt(encrypted_number) + + +def load_documents(docs_dir): + documents = {} + for filename in os.listdir(docs_dir): + if filename.endswith(".md"): + filepath = os.path.join(docs_dir, filename) + with open(filepath, "r") as f: + documents[filename] = f.read() + print(f"Loaded {len(documents)} documents") + return documents + + +def build_inverted_index(documents): + # word -> list of doc IDs + inverted_index = defaultdict(set) + + for doc_id, content in documents.items(): + words = content.lower().replace('\n', ' ').split() + words = [''.join(c for c in word if c.isalnum()) for word in words] + words = [w for w in words if w] + + for word in words: + inverted_index[word].add(doc_id) + + inverted_index = {word: list(doc_ids) for word, doc_ids in inverted_index.items()} + print(f"Built index with {len(inverted_index)} unique words") + return inverted_index + + +def encrypt_index(inverted_index): + # encrypt index using Paillier + # for simplicity, we encrypt the hash of words and keep doc IDs in plaintext + # in production, you'd use more sophisticated techniques + encrypted_index = {} + + for word, doc_ids in inverted_index.items(): + # create a numeric representation of the word + word_hash = hash(word) % (10**6) # keep it manageable + encrypted_word = encrypt_number(word_hash) + encrypted_index[word] = { + 'encrypted_hash': encrypted_word, + 'doc_ids': doc_ids + } + + # save to file + with open("encrypted_index.pkl", "wb") as f: + pickle.dump(encrypted_index, f) + + print("Encrypted index saved") + return encrypted_index + + +def decrypt_index(encrypted_index): + # decrypt index hashes + decrypted_index = {} + + for word, data in encrypted_index.items(): + decrypted_hash = decrypt_number(data['encrypted_hash']) + decrypted_index[word] = { + 'hash': decrypted_hash, + 'doc_ids': data['doc_ids'] + } + + return decrypted_index + + +def encrypt_query(query): + # normalize and encrypt query + query = query.lower().strip() + query = ''.join(c for c in query if c.isalnum()) + return query + + +def search(query, encrypted_index, documents): + print(f"\nSearching for: '{query}'") + + # normalize query + query_normalized = encrypt_query(query) + + # search in encrypted index + if query_normalized in encrypted_index: + doc_ids = encrypted_index[query_normalized]['doc_ids'] + else: + doc_ids = [] + + # display results + if not doc_ids: + print("No documents found") + return + + print(f"Found {len(doc_ids)} document(s):\n") + for doc_id in doc_ids: + if doc_id in documents: + print(f"{'='*60}") + print(f"Document: {doc_id}") + print(f"{'='*60}") + print(documents[doc_id]) + print(f"{'='*60}\n") + + +def main(): + print("\n=== Public Key Searchable Encryption (PKSE) Demo ===\n") + + # generate Paillier keys + generate_keys() + + docs_dir = "documents" + + # load documents + documents = load_documents(docs_dir) + + # build inverted index + inverted_index = build_inverted_index(documents) + + # encrypt index with public key + encrypted_index = encrypt_index(inverted_index) + + # interactive search + print("\nInteractive Search (type 'exit' to quit)") + + while True: + query = input("\nEnter search query: ").strip() + + if query.lower() == 'exit': + break + + if query: + search(query, encrypted_index, documents) + + print("\nDemo Complete\n") + + +if __name__ == "__main__": + main() + diff --git a/IS/Lab/Lab8/SSE/documents/doc1.md b/IS/Lab/Lab8/SSE/documents/doc1.md new file mode 100644 index 0000000..2643df4 --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc1.md @@ -0,0 +1,5 @@ +# Introduction to Cryptography + +Cryptography is the practice and study of techniques for secure communication. +It involves encryption, decryption, and various security protocols. + diff --git a/IS/Lab/Lab8/SSE/documents/doc10.md b/IS/Lab/Lab8/SSE/documents/doc10.md new file mode 100644 index 0000000..f519336 --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc10.md @@ -0,0 +1,5 @@ +# Blockchain and Cryptography + +Blockchain technology relies heavily on cryptographic hash functions. +Bitcoin and other cryptocurrencies use encryption for security. + diff --git a/IS/Lab/Lab8/SSE/documents/doc2.md b/IS/Lab/Lab8/SSE/documents/doc2.md new file mode 100644 index 0000000..fae8fba --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc2.md @@ -0,0 +1,5 @@ +# Symmetric Encryption + +Symmetric encryption uses the same key for encryption and decryption. +AES is a popular symmetric encryption algorithm used worldwide. + diff --git a/IS/Lab/Lab8/SSE/documents/doc3.md b/IS/Lab/Lab8/SSE/documents/doc3.md new file mode 100644 index 0000000..00c1383 --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc3.md @@ -0,0 +1,5 @@ +# Asymmetric Encryption + +Asymmetric encryption uses a pair of keys: public and private. +RSA and ECC are examples of asymmetric encryption algorithms. + diff --git a/IS/Lab/Lab8/SSE/documents/doc4.md b/IS/Lab/Lab8/SSE/documents/doc4.md new file mode 100644 index 0000000..49ac46d --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc4.md @@ -0,0 +1,5 @@ +# Hash Functions + +Hash functions create fixed-size outputs from variable-size inputs. +SHA-256 and MD5 are commonly used hash functions in cryptography. + diff --git a/IS/Lab/Lab8/SSE/documents/doc5.md b/IS/Lab/Lab8/SSE/documents/doc5.md new file mode 100644 index 0000000..ef14b12 --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc5.md @@ -0,0 +1,5 @@ +# Digital Signatures + +Digital signatures provide authentication and non-repudiation. +They use asymmetric encryption to verify the sender's identity. + diff --git a/IS/Lab/Lab8/SSE/documents/doc6.md b/IS/Lab/Lab8/SSE/documents/doc6.md new file mode 100644 index 0000000..169f546 --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc6.md @@ -0,0 +1,5 @@ +# AES Encryption Standard + +AES stands for Advanced Encryption Standard. +It supports key sizes of 128, 192, and 256 bits for encryption. + diff --git a/IS/Lab/Lab8/SSE/documents/doc7.md b/IS/Lab/Lab8/SSE/documents/doc7.md new file mode 100644 index 0000000..6118304 --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc7.md @@ -0,0 +1,5 @@ +# Public Key Infrastructure + +PKI manages digital certificates and public-key encryption. +It provides a framework for secure communication over networks. + diff --git a/IS/Lab/Lab8/SSE/documents/doc8.md b/IS/Lab/Lab8/SSE/documents/doc8.md new file mode 100644 index 0000000..14a5c5c --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc8.md @@ -0,0 +1,5 @@ +# Cryptographic Protocols + +Protocols like TLS and SSL ensure secure communication. +They combine encryption, authentication, and data integrity. + diff --git a/IS/Lab/Lab8/SSE/documents/doc9.md b/IS/Lab/Lab8/SSE/documents/doc9.md new file mode 100644 index 0000000..27be380 --- /dev/null +++ b/IS/Lab/Lab8/SSE/documents/doc9.md @@ -0,0 +1,5 @@ +# Quantum Cryptography + +Quantum cryptography uses quantum mechanics for secure communication. +It provides theoretically unbreakable encryption using quantum key distribution. + diff --git a/IS/Lab/Lab8/SSE/sse.py b/IS/Lab/Lab8/SSE/sse.py new file mode 100644 index 0000000..1d4ade9 --- /dev/null +++ b/IS/Lab/Lab8/SSE/sse.py @@ -0,0 +1,130 @@ +import os +import json +from collections import defaultdict +from Crypto.Cipher import AES +from Crypto.Random import get_random_bytes +from Crypto.Util.Padding import pad, unpad + +AES_KEY = get_random_bytes(32) # 256-bit key + + +def encrypt_data(data): + cipher = AES.new(AES_KEY, AES.MODE_CBC) + iv = cipher.iv + if isinstance(data, str): + data = data.encode('utf-8') + encrypted = cipher.encrypt(pad(data, AES.block_size)) + return iv + encrypted + + +def decrypt_data(encrypted_data): + iv = encrypted_data[:16] + encrypted = encrypted_data[16:] + cipher = AES.new(AES_KEY, AES.MODE_CBC, iv) + decrypted = unpad(cipher.decrypt(encrypted), AES.block_size) + return decrypted + + +def load_documents(docs_dir): + documents = {} + for filename in os.listdir(docs_dir): + if filename.endswith(".md"): + filepath = os.path.join(docs_dir, filename) + with open(filepath, "r") as f: + documents[filename] = f.read() + print(f"Loaded {len(documents)} documents") + return documents + + +def build_inverted_index(documents): + # word -> list of doc IDs + inverted_index = defaultdict(set) + + for doc_id, content in documents.items(): + words = content.lower().replace('\n', ' ').split() + words = [''.join(c for c in word if c.isalnum()) for word in words] + words = [w for w in words if w] + + for word in words: + inverted_index[word].add(doc_id) + + inverted_index = {word: list(doc_ids) for word, doc_ids in inverted_index.items()} + print(f"Built index with {len(inverted_index)} unique words") + return inverted_index + + +def encrypt_index(inverted_index): + # serialize and encrypt + serialized = json.dumps(inverted_index).encode('utf-8') + encrypted = encrypt_data(serialized) + with open("encrypted_index.bin", "wb") as f: + f.write(encrypted) + print("Encrypted index saved") + return encrypted + + +def decrypt_index(encrypted_index): + decrypted = decrypt_data(encrypted_index) + inverted_index = json.loads(decrypted.decode('utf-8')) + return inverted_index + + +def search(query, encrypted_index_data, documents): + print(f"\nSearching for: '{query}'") + + # decrypt index + inverted_index = decrypt_index(encrypted_index_data) + + # normalize query + query_normalized = query.lower().strip() + query_normalized = ''.join(c for c in query_normalized if c.isalnum()) + + # search + doc_ids = inverted_index.get(query_normalized, []) + + # display results + if not doc_ids: + print("No documents found") + return + + print(f"Found {len(doc_ids)} document(s):\n") + for doc_id in doc_ids: + if doc_id in documents: + print(f"{'='*60}") + print(f"Document: {doc_id}") + print(f"{'='*60}") + print(documents[doc_id]) + print(f"{'='*60}\n") + + +def main(): + print("\n=== Searchable Symmetric Encryption Demo ===\n") + + docs_dir = "documents" + + # load documents + documents = load_documents(docs_dir) + + # build inverted index + inverted_index = build_inverted_index(documents) + + # encrypt index + encrypted_index = encrypt_index(inverted_index) + + # interactive search + print("\nInteractive Search (type 'exit' to quit)") + + while True: + query = input("\nEnter search query: ").strip() + + if query.lower() == 'exit': + break + + if query: + search(query, encrypted_index, documents) + + print("\nDemo Complete\n") + + +if __name__ == "__main__": + main()