Complexbackendsecuritywithencryptedwavesandanomalydetection
/
complex_backend_security_with_encrypted_waves_and_anomaly_detection.py
from cryptography.fernet import Fernet | |
# Generate a key for encryption | |
key = Fernet.generate_key() | |
cipher = Fernet(key) | |
# Example wave data (as a list of floats) | |
wave_data = [0.1, 0.5, 0.3, 0.4, 0.9] | |
# Convert wave data to bytes and encrypt | |
wave_data_bytes = bytes(str(wave_data), 'utf-8') | |
encrypted_wave_data = cipher.encrypt(wave_data_bytes) | |
# Decrypting wave data | |
decrypted_wave_data_bytes = cipher.decrypt(encrypted_wave_data) | |
decrypted_wave_data = eval(decrypted_wave_data_bytes.decode('utf-8')) | |
print("Original Wave Data:", wave_data) | |
print("Encrypted Wave Data:", encrypted_wave_data) | |
print("Decrypted Wave Data:", decrypted_wave_data) | |
import torch | |
import torch.nn as nn | |
import torch.optim as optim | |
import numpy as np | |
class Autoencoder(nn.Module): | |
def __init__(self): | |
super(Autoencoder, self).__init__() | |
self.encoder = nn.Sequential( | |
nn.Linear(5, 3), | |
nn.ReLU(), | |
nn.Linear(3, 2), | |
nn.ReLU() | |
) | |
self.decoder = nn.Sequential( | |
nn.Linear(2, 3), | |
nn.ReLU(), | |
nn.Linear(3, 5), | |
nn.Sigmoid() | |
) | |
def forward(self, x): | |
x = self.encoder(x) | |
x = self.decoder(x) | |
return x | |
# Initialize the model, loss function, and optimizer | |
model = Autoencoder() | |
criterion = nn.MSELoss() | |
optimizer = optim.Adam(model.parameters(), lr=0.01) | |
normal_wave_data = torch.tensor([ | |
[0.1, 0.2, 0.3, 0.4, 0.5], | |
[0.2, 0.3, 0.4, 0.5, 0.6], | |
[0.3, 0.4, 0.5, 0.6, 0.7] | |
], dtype=torch.float32) | |
# Training the model | |
for epoch in range(1000): # Example training loop | |
optimizer.zero_grad() | |
outputs = model(normal_wave_data) | |
loss = criterion(outputs, normal_wave_data) | |
loss.backward() | |
optimizer.step() | |
if (epoch+1) % 100 == 0: | |
print(f'Epoch [{epoch+1}/1000], Loss: {loss.item():.4f}') | |
# New wave data to check for anomalies | |
new_wave_data = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5], dtype=torch.float32) | |
# Reshape for single input | |
new_wave_data = new_wave_data.unsqueeze(0) | |
# Pass through the model | |
reconstructed_data = model(new_wave_data) | |
loss = criterion(reconstructed_data, new_wave_data) | |
# Set a threshold for anomaly detection | |
anomaly_threshold = 0.01 | |
if loss.item() > anomaly_threshold: | |
print("Anomaly detected!") | |
else: | |
print("Data is normal.") |