Skip to content

Commit

Permalink
Create quantum_crypto.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 1, 2024
1 parent 5f4d23f commit 68a305d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/security/quantum_crypto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
from qiskit import QuantumCircuit, execute, Aer

class QuantumCrypto:
def __init__(self, key_size=256):
self.key_size = key_size
self.backend = Aer.get_backend('qasm_simulator')

def generate_key(self):
qc = QuantumCircuit(self.key_size)
for i in range(self.key_size):
qc.h(i)
job = execute(qc, self.backend, shots=1)
result = job.result()
counts = result.get_counts(qc)
key = list(counts.keys())[0]
return key

def encrypt(self, message, key):
encrypted_message = ""
for i in range(len(message)):
encrypted_message += chr(ord(message[i]) ^ ord(key[i % self.key_size]))
return encrypted_message

def decrypt(self, encrypted_message, key):
decrypted_message = ""
for i in range(len(encrypted_message)):
decrypted_message += chr(ord(encrypted_message[i]) ^ ord(key[i % self.key_size]))
return decrypted_message

# Example usage
if __name__ == "__main__":
qc = QuantumCrypto(key_size=256)
key = qc.generate_key()
message = "Hello, Quantum Crypto!"
encrypted_message = qc.encrypt(message, key)
decrypted_message = qc.decrypt(encrypted_message, key)
print(f"Original Message: {message}")
print(f"Encrypted Message: {encrypted_message}")
print(f"Decrypted Message: {decrypted_message}")

0 comments on commit 68a305d

Please sign in to comment.