Python实现XOR算法
时间: 2023-11-19 12:43:29 浏览: 37
Python中可以使用位运算符^来实现XOR算法。下面是一个简单的Python代码示例:
```python
def xor_encrypt_decrypt(plaintext, key):
ciphertext = ""
for i in range(len(plaintext)):
ciphertext += chr(ord(plaintext[i]) ^ ord(key[i % len(key)]))
return ciphertext
# 测试
plaintext = "Hello, world!"
key = "secret_key"
ciphertext = xor_encrypt_decrypt(plaintext, key)
decrypted_text = xor_encrypt_decrypt(ciphertext, key)
print("加密后的密文:", ciphertext)
print("解密后的明文:", decrypted_text)
```
相关问题
python实现mlp算法代码
MLP(多层感知机)是一种常用的神网络模型,用于解决分类和回归问题。下面是一个简单的Python代码示例,用于实现MLP算法:
```python
import numpy as np
class MLP:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# 初始化权重
self.W1 = np.random.randn(self.input_size, self.hidden_size)
self.b1 = np.zeros((1, self.hidden_size))
self.W2 = np.random.randn(self.hidden_size, self.output_size)
self.b2 = np.zeros((1, self.output_size))
def forward(self, X):
# 前向传播
self.z1 = np.dot(X, self.W1) + self.b1
self.a1 = np.tanh(self.z1)
self.z2 = np.dot(self.a1, self.W2) + self.b2
self.a2 = self.sigmoid(self.z2)
return self.a2
def backward(self, X, y, learning_rate):
# 反向传播
m = X.shape[0]
# 计算输出层的误差
delta2 = self.a2 - y
# 计算隐藏层的误差
delta1 = np.dot(delta2, self.W2.T) * (1 - np.power(self.a1, 2))
# 更新权重和偏置
dW2 = np.dot(self.a1.T, delta2) / m
db2 = np.sum(delta2, axis=0) / m
dW1 = np.dot(X.T, delta1) / m
db1 = np.sum(delta1, axis=0) / m
self.W2 -= learning_rate * dW2
self.b2 -= learning_rate * db2
self.W1 -= learning_rate * dW1
self.b1 -= learning_rate * db1
def train(self, X, y, epochs, learning_rate):
for epoch in range(epochs):
# 前向传播
output = self.forward(X)
# 反向传播
self.backward(X, y, learning_rate)
# 计算损失函数
loss = self.loss_function(output, y)
if epoch % 100 == 0:
print(f"Epoch {epoch}, Loss: {loss}")
def predict(self, X):
# 预测
output = self.forward(X)
predictions = np.round(output)
return predictions
def loss_function(self, y_pred, y_true):
# 损失函数(交叉熵)
loss = -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))
return loss
def sigmoid(self, x):
# sigmoid激活函数
return 1 / (1 + np.exp(-x))
```
使用示例:
```python
# 创建MLP对象
mlp = MLP(input_size=2, hidden_size=4, output_size=1)
# 训练数据
X_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_train = np.array([[0], [1], [1], [0]])
# 训练模型
mlp.train(X_train, y_train, epochs=1000, learning_rate=0.1)
# 预测数据
X_test = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
predictions = mlp.predict(X_test)
print(predictions)
```
这段代码实现了一个简单的MLP算法,用于解决逻辑门问题(XOR)。你可以根据自己的需求进行修改和扩展。希望对你有帮助!
python换位加密算法_移位XOR的加密算法还原(python 实现)
移位 XOR 加密算法是一种简单的加密算法,它将明文中的每个字符与一个密钥进行异或运算,然后将结果转换成 ASCII 码对应的字符,得到密文。而解密则是将密文中的每个字符再次与密钥进行异或运算,得到明文。
下面是一个 Python 实现移位 XOR 加密算法的代码:
```python
def xor_encrypt(message, key):
cipher = ''
for char in message:
cipher += chr(ord(char) ^ key)
return cipher
def xor_decrypt(cipher, key):
message = ''
for char in cipher:
message += chr(ord(char) ^ key)
return message
```
其中,`xor_encrypt` 函数接收明文和密钥作为参数,返回加密后的密文;`xor_decrypt` 函数接收密文和密钥作为参数,返回解密后的明文。在函数中,使用了 Python 内置的 `ord` 和 `chr` 函数,分别将字符转换成 ASCII 码和将 ASCII 码转换成字符。
以下是一个使用示例:
```python
message = 'hello world'
key = 42
cipher = xor_encrypt(message, key)
print('Cipher:', cipher)
decrypted_message = xor_decrypt(cipher, key)
print('Decrypted message:', decrypted_message)
```
输出结果为:
```
Cipher: ]c^cZ^W^bc^Y^]c^b
Decrypted message: hello world
```
需要注意的是,移位 XOR 加密算法的安全性非常低,不适合用于真实世界的加密场景,仅适用于教学和学术研究。