Upload complex_backend_security_with_encrypted_waves_and_anomaly_detection.py
Browse files
complex_backend_security_with_encrypted_waves_and_anomaly_detection.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Complex backend security with encrypted waves and anomaly detection
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1iCPhdZ1rZxxWmNqFPIUGGrtgVUfgcPcI
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from cryptography.fernet import Fernet
|
| 11 |
+
|
| 12 |
+
# Generate a key for encryption
|
| 13 |
+
key = Fernet.generate_key()
|
| 14 |
+
cipher = Fernet(key)
|
| 15 |
+
|
| 16 |
+
# Example wave data (as a list of floats)
|
| 17 |
+
wave_data = [0.1, 0.5, 0.3, 0.4, 0.9]
|
| 18 |
+
|
| 19 |
+
# Convert wave data to bytes and encrypt
|
| 20 |
+
wave_data_bytes = bytes(str(wave_data), 'utf-8')
|
| 21 |
+
encrypted_wave_data = cipher.encrypt(wave_data_bytes)
|
| 22 |
+
|
| 23 |
+
# Decrypting wave data
|
| 24 |
+
decrypted_wave_data_bytes = cipher.decrypt(encrypted_wave_data)
|
| 25 |
+
decrypted_wave_data = eval(decrypted_wave_data_bytes.decode('utf-8'))
|
| 26 |
+
|
| 27 |
+
print("Original Wave Data:", wave_data)
|
| 28 |
+
print("Encrypted Wave Data:", encrypted_wave_data)
|
| 29 |
+
print("Decrypted Wave Data:", decrypted_wave_data)
|
| 30 |
+
|
| 31 |
+
import torch
|
| 32 |
+
import torch.nn as nn
|
| 33 |
+
import torch.optim as optim
|
| 34 |
+
import numpy as np
|
| 35 |
+
|
| 36 |
+
class Autoencoder(nn.Module):
|
| 37 |
+
def __init__(self):
|
| 38 |
+
super(Autoencoder, self).__init__()
|
| 39 |
+
self.encoder = nn.Sequential(
|
| 40 |
+
nn.Linear(5, 3),
|
| 41 |
+
nn.ReLU(),
|
| 42 |
+
nn.Linear(3, 2),
|
| 43 |
+
nn.ReLU()
|
| 44 |
+
)
|
| 45 |
+
self.decoder = nn.Sequential(
|
| 46 |
+
nn.Linear(2, 3),
|
| 47 |
+
nn.ReLU(),
|
| 48 |
+
nn.Linear(3, 5),
|
| 49 |
+
nn.Sigmoid()
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
def forward(self, x):
|
| 53 |
+
x = self.encoder(x)
|
| 54 |
+
x = self.decoder(x)
|
| 55 |
+
return x
|
| 56 |
+
|
| 57 |
+
# Initialize the model, loss function, and optimizer
|
| 58 |
+
model = Autoencoder()
|
| 59 |
+
criterion = nn.MSELoss()
|
| 60 |
+
optimizer = optim.Adam(model.parameters(), lr=0.01)
|
| 61 |
+
|
| 62 |
+
normal_wave_data = torch.tensor([
|
| 63 |
+
[0.1, 0.2, 0.3, 0.4, 0.5],
|
| 64 |
+
[0.2, 0.3, 0.4, 0.5, 0.6],
|
| 65 |
+
[0.3, 0.4, 0.5, 0.6, 0.7]
|
| 66 |
+
], dtype=torch.float32)
|
| 67 |
+
|
| 68 |
+
# Training the model
|
| 69 |
+
for epoch in range(1000): # Example training loop
|
| 70 |
+
optimizer.zero_grad()
|
| 71 |
+
outputs = model(normal_wave_data)
|
| 72 |
+
loss = criterion(outputs, normal_wave_data)
|
| 73 |
+
loss.backward()
|
| 74 |
+
optimizer.step()
|
| 75 |
+
|
| 76 |
+
if (epoch+1) % 100 == 0:
|
| 77 |
+
print(f'Epoch [{epoch+1}/1000], Loss: {loss.item():.4f}')
|
| 78 |
+
|
| 79 |
+
# New wave data to check for anomalies
|
| 80 |
+
new_wave_data = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5], dtype=torch.float32)
|
| 81 |
+
|
| 82 |
+
# Reshape for single input
|
| 83 |
+
new_wave_data = new_wave_data.unsqueeze(0)
|
| 84 |
+
|
| 85 |
+
# Pass through the model
|
| 86 |
+
reconstructed_data = model(new_wave_data)
|
| 87 |
+
loss = criterion(reconstructed_data, new_wave_data)
|
| 88 |
+
|
| 89 |
+
# Set a threshold for anomaly detection
|
| 90 |
+
anomaly_threshold = 0.01
|
| 91 |
+
|
| 92 |
+
if loss.item() > anomaly_threshold:
|
| 93 |
+
print("Anomaly detected!")
|
| 94 |
+
else:
|
| 95 |
+
print("Data is normal.")
|