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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Secure Pulse
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1tY1tdoA1k0yP7_rFc5ckKLIYm-50jvYJ
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
import torch.nn.functional as F
|
| 13 |
+
|
| 14 |
+
class FrequencyModulation(nn.Module):
|
| 15 |
+
def __init__(self, frequency):
|
| 16 |
+
super(FrequencyModulation, self).__init__()
|
| 17 |
+
self.frequency = frequency
|
| 18 |
+
|
| 19 |
+
def forward(self, x):
|
| 20 |
+
# Apply a sine function for modulation
|
| 21 |
+
return torch.sin(2 * torch.pi * self.frequency * x)
|
| 22 |
+
|
| 23 |
+
class EncryptionLayer(nn.Module):
|
| 24 |
+
def __init__(self, key_frequency):
|
| 25 |
+
super(EncryptionLayer, self).__init__()
|
| 26 |
+
self.key_frequency = key_frequency
|
| 27 |
+
|
| 28 |
+
def forward(self, x):
|
| 29 |
+
# Apply frequency shift as a form of encryption
|
| 30 |
+
return torch.sin(2 * torch.pi * (self.key_frequency + x))
|
| 31 |
+
|
| 32 |
+
class FrequencyHopping(nn.Module):
|
| 33 |
+
def __init__(self, frequencies):
|
| 34 |
+
super(FrequencyHopping, self).__init__()
|
| 35 |
+
self.frequencies = frequencies
|
| 36 |
+
|
| 37 |
+
def forward(self, x):
|
| 38 |
+
# Apply frequency hopping
|
| 39 |
+
for freq in self.frequencies:
|
| 40 |
+
x = torch.sin(2 * torch.pi * freq * x)
|
| 41 |
+
return x
|
| 42 |
+
|
| 43 |
+
class DecryptionLayer(nn.Module):
|
| 44 |
+
def __init__(self, key_frequency):
|
| 45 |
+
super(DecryptionLayer, self).__init__()
|
| 46 |
+
self.key_frequency = key_frequency
|
| 47 |
+
|
| 48 |
+
def forward(self, x):
|
| 49 |
+
# Inverse of the encryption process
|
| 50 |
+
return torch.asin(x) / (2 * torch.pi * self.key_frequency)
|
| 51 |
+
|
| 52 |
+
class FrequencyVPN(nn.Module):
|
| 53 |
+
def __init__(self, frequency, key_frequency, hopping_frequencies):
|
| 54 |
+
super(FrequencyVPN, self).__init__()
|
| 55 |
+
self.modulation = FrequencyModulation(frequency)
|
| 56 |
+
self.encryption = EncryptionLayer(key_frequency)
|
| 57 |
+
self.hopping = FrequencyHopping(hopping_frequencies)
|
| 58 |
+
self.decryption = DecryptionLayer(key_frequency)
|
| 59 |
+
|
| 60 |
+
def forward(self, x):
|
| 61 |
+
x = self.modulation(x)
|
| 62 |
+
x = self.encryption(x)
|
| 63 |
+
x = self.hopping(x)
|
| 64 |
+
return self.decryption(x)
|
| 65 |
+
|
| 66 |
+
# Example usage
|
| 67 |
+
model = FrequencyVPN(frequency=5, key_frequency=10, hopping_frequencies=[15, 20, 25])
|
| 68 |
+
data = torch.tensor([1.0, 0.5, 0.3]) # Example data
|
| 69 |
+
encrypted_data = model(data)
|
| 70 |
+
|
| 71 |
+
!pip install matplotlib
|
| 72 |
+
|
| 73 |
+
import torch
|
| 74 |
+
import torch.nn as nn
|
| 75 |
+
import matplotlib.pyplot as plt
|
| 76 |
+
import numpy as np
|
| 77 |
+
|
| 78 |
+
# Define the Frequency Modulation Layer
|
| 79 |
+
class FrequencyModulation(nn.Module):
|
| 80 |
+
def __init__(self, frequency):
|
| 81 |
+
super(FrequencyModulation, self).__init__()
|
| 82 |
+
self.frequency = frequency
|
| 83 |
+
|
| 84 |
+
def forward(self, x):
|
| 85 |
+
return torch.sin(2 * torch.pi * self.frequency * x)
|
| 86 |
+
|
| 87 |
+
# Define the Encryption Layer
|
| 88 |
+
class EncryptionLayer(nn.Module):
|
| 89 |
+
def __init__(self, key_frequency):
|
| 90 |
+
super(EncryptionLayer, self).__init__()
|
| 91 |
+
self.key_frequency = key_frequency
|
| 92 |
+
|
| 93 |
+
def forward(self, x):
|
| 94 |
+
return torch.sin(2 * torch.pi * (self.key_frequency + x))
|
| 95 |
+
|
| 96 |
+
# Define the Frequency Hopping Layer
|
| 97 |
+
class FrequencyHopping(nn.Module):
|
| 98 |
+
def __init__(self, frequencies):
|
| 99 |
+
super(FrequencyHopping, self).__init__()
|
| 100 |
+
self.frequencies = frequencies
|
| 101 |
+
|
| 102 |
+
def forward(self, x):
|
| 103 |
+
for freq in self.frequencies:
|
| 104 |
+
x = torch.sin(2 * torch.pi * freq * x)
|
| 105 |
+
return x
|
| 106 |
+
|
| 107 |
+
# Define the Decryption Layer
|
| 108 |
+
class DecryptionLayer(nn.Module):
|
| 109 |
+
def __init__(self, key_frequency):
|
| 110 |
+
super(DecryptionLayer, self).__init__()
|
| 111 |
+
self.key_frequency = key_frequency
|
| 112 |
+
|
| 113 |
+
def forward(self, x):
|
| 114 |
+
return torch.asin(x) / (2 * torch.pi * self.key_frequency)
|
| 115 |
+
|
| 116 |
+
# Define the FrequencyVPN Model
|
| 117 |
+
class FrequencyVPN(nn.Module):
|
| 118 |
+
def __init__(self, frequency, key_frequency, hopping_frequencies):
|
| 119 |
+
super(FrequencyVPN, self).__init__()
|
| 120 |
+
self.modulation = FrequencyModulation(frequency)
|
| 121 |
+
self.encryption = EncryptionLayer(key_frequency)
|
| 122 |
+
self.hopping = FrequencyHopping(hopping_frequencies)
|
| 123 |
+
self.decryption = DecryptionLayer(key_frequency)
|
| 124 |
+
|
| 125 |
+
def forward(self, x):
|
| 126 |
+
x_modulated = self.modulation(x)
|
| 127 |
+
x_encrypted = self.encryption(x_modulated)
|
| 128 |
+
x_hopped = self.hopping(x_encrypted)
|
| 129 |
+
x_decrypted = self.decryption(x_hopped)
|
| 130 |
+
return x_modulated, x_encrypted, x_hopped, x_decrypted
|
| 131 |
+
|
| 132 |
+
# Set the parameters for the model
|
| 133 |
+
frequency = 5 # Modulation frequency
|
| 134 |
+
key_frequency = 10 # Encryption key frequency
|
| 135 |
+
hopping_frequencies = [15, 20, 25] # Frequencies for hopping
|
| 136 |
+
|
| 137 |
+
# Create the FrequencyVPN model
|
| 138 |
+
model = FrequencyVPN(frequency, key_frequency, hopping_frequencies)
|
| 139 |
+
|
| 140 |
+
# Example data (simulating a data packet)
|
| 141 |
+
data = torch.linspace(0, 1, 100) # 100 data points between 0 and 1
|
| 142 |
+
|
| 143 |
+
# Pass the data through the model
|
| 144 |
+
x_modulated, x_encrypted, x_hopped, x_decrypted = model(data)
|
| 145 |
+
|
| 146 |
+
# Convert the torch tensors to numpy arrays for plotting
|
| 147 |
+
data_np = data.numpy()
|
| 148 |
+
x_modulated_np = x_modulated.detach().numpy()
|
| 149 |
+
x_encrypted_np = x_encrypted.detach().numpy()
|
| 150 |
+
x_hopped_np = x_hopped.detach().numpy()
|
| 151 |
+
x_decrypted_np = x_decrypted.detach().numpy()
|
| 152 |
+
|
| 153 |
+
# Plot the data at each stage
|
| 154 |
+
plt.figure(figsize=(12, 8))
|
| 155 |
+
|
| 156 |
+
plt.subplot(4, 1, 1)
|
| 157 |
+
plt.plot(data_np, x_modulated_np, label='Modulated Data', color='blue')
|
| 158 |
+
plt.title('Modulated Data')
|
| 159 |
+
plt.grid(True)
|
| 160 |
+
|
| 161 |
+
plt.subplot(4, 1, 2)
|
| 162 |
+
plt.plot(data_np, x_encrypted_np, label='Encrypted Data', color='green')
|
| 163 |
+
plt.title('Encrypted Data')
|
| 164 |
+
plt.grid(True)
|
| 165 |
+
|
| 166 |
+
plt.subplot(4, 1, 3)
|
| 167 |
+
plt.plot(data_np, x_hopped_np, label='Frequency Hopped Data', color='red')
|
| 168 |
+
plt.title('Frequency Hopped Data')
|
| 169 |
+
plt.grid(True)
|
| 170 |
+
|
| 171 |
+
plt.subplot(4, 1, 4)
|
| 172 |
+
plt.plot(data_np, x_decrypted_np, label='Decrypted Data', color='purple')
|
| 173 |
+
plt.title('Decrypted Data')
|
| 174 |
+
plt.grid(True)
|
| 175 |
+
|
| 176 |
+
plt.tight_layout()
|
| 177 |
+
plt.show()
|