aes、rsa、ecc、des、3des、rabin、elgamal、diffie-hellman加密代码
时间: 2023-11-10 12:03:20 浏览: 239
AES,DES,3DES,RSA加密工具类
AES(高级加密标准)加密代码:
```python
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
```
RSA(Rivest-Shamir-Adleman)加密代码:
```python
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
key = RSA.generate(2048)
cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.encrypt(data)
```
ECC(椭圆曲线加密)加密代码:
```python
from tinyec import registry
curve = registry.get_curve('secp256r1')
public_key = curve.g * private_key
shared_key = public_key * remote_private_key
```
DES(数据加密标准)加密代码:
```python
from Crypto.Cipher import DES
key = b'abcdefgh'
cipher = DES.new(key, DES.MODE_ECB)
ciphertext = cipher.encrypt(data)
```
3DES(Triple DES)加密代码:
```python
from Crypto.Cipher import DES3
key = b'abcdefghabcdefghabcdefgh'
cipher = DES3.new(key, DES3.MODE_ECB)
ciphertext = cipher.encrypt(data)
```
Rabin加密代码:
```python
from Crypto.PublicKey import Rabin
from Crypto.Cipher import PKCS1_v1_5
key = Rabin.generate(2048)
cipher = PKCS1_v1_5.new(key)
ciphertext = cipher.encrypt(data)
```
ElGamal加密代码:
```python
from Crypto.PublicKey import ElGamal
from Crypto.Cipher import ElGamal
from Crypto import Random
key = ElGamal.generate(2048, Random.new().read)
cipher = ElGamal.new(key)
ciphertext = cipher.encrypt(data, 0)
```
Diffie-Hellman加密代码:
```python
from Crypto.PublicKey import DH
from Crypto.Hash import SHA256
key = DH.generate(2048)
public_key = key.publickey()
```
以上代码仅用于演示目的,实际使用时应该仔细考虑安全性和性能等因素。
阅读全文