python ARC4
时间: 2023-10-15 16:27:41 浏览: 45
ARC4(也称为RC4)是一种对称密钥流密码算法,它常用于数据加密和解密。在Python中,你可以使用`pycryptodome`库来实现ARC4算法的功能。以下是一个示例代码:
```python
from Crypto.Cipher import ARC4
def encrypt(key, plaintext):
cipher = ARC4.new(key)
ciphertext = cipher.encrypt(plaintext)
return ciphertext
def decrypt(key, ciphertext):
cipher = ARC4.new(key)
plaintext = cipher.decrypt(ciphertext)
return plaintext
# 使用示例
key = b'example_key'
plaintext = b'Hello, world!'
ciphertext = encrypt(key, plaintext)
decrypted_text = decrypt(key, ciphertext)
print("Encrypted: ", ciphertext)
print("Decrypted: ", decrypted_text)
```
在示例代码中,我们首先导入了`ARC4`类。然后定义了`encrypt()`和`decrypt()`函数来执行加密和解密操作。最后,我们使用示例密钥和明文进行加密和解密,并打印结果。
请注意,这里的密钥和明文都需要使用字节数组(bytes)形式进行传递。