举一个很简单的例子,实现非对称加密
时间: 2024-02-26 12:52:32 浏览: 21
一个很简单的非对称加密例子是RSA算法。首先,我们需要生成一对公钥和私钥,公钥可以随意公开,而私钥必须保密。具体实现步骤如下:
1. 生成公钥和私钥:
选择两个大素数 p 和 q,计算 N = p * q 以及 φ(N) = (p-1) * (q-1)。然后选择一个整数 e(1 < e < φ(N)),使得 e 与 φ(N) 互质。接下来,计算 d,使得 d * e ≡ 1 (mod φ(N)),即 d 是 e 在模 φ(N) 意义下的乘法逆元。此时,公钥为 (N, e),私钥为 (N, d)。
2. 加密和解密过程:
假设明文为 M,要加密成密文 C。发送方使用接收方的公钥 (N, e) 对明文进行加密,计算出 C = M^e (mod N)。接收方收到密文 C 后,使用自己的私钥 (N, d) 对密文进行解密,计算出 M = C^d (mod N)。
这个例子中,加密和解密使用了不同的密钥,因此实现了非对称加密。RSA算法的安全性基于大整数分解的困难性,即对于一个极大的合数 N,将其分解成质因数相当困难,因此可以保证密钥的安全性。RSA算法被广泛应用于数字签名、数据加密、身份验证等领域。
相关问题
用PYTHON 写一个类,实现非对称加密
在 Python 中,可以使用 `cryptography` 库来实现非对称加密。这里是一个简单的例子,展示了如何使用该库来实现非对称加密:
```python
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
# 生成密钥对
private_key = rsa.generate_private_key(
public_exponent=65537, key_size=2048
)
public_key = private_key.public_key()
# 序列化公钥
public_key_bytes = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
# 使用公钥加密数据
data = b"Hello, World!"
ciphertext = public_key.encrypt(
data,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
# 使用私钥解密数据
plaintext = private_key.decrypt(
ciphertext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print(plaintext) # 输出:b'Hello, World!'
```
在这个例子中,我们使用了 RSA 算法来生成密钥对,并使用公钥加密数据,使用私钥解密数据。需要注意的是,非对称加密的数据一般都要经过填充,这里我们使用了 OAEP 填充。
如果你想将这些代码封装成一个类,可以这样做:
```python
class AsymmetricEncryption:
def __init__(self):
# 生成密钥对
self.private_key = rsa.generate_private_key(
public_exponent=65537, key_size=2048
)
self.public_key = self.private_key
提供一个安全可靠的非对称加密算法java实现
关于非对称加密算法,常用的有RSA、ECC等算法,这里提供一个基于RSA算法的Java实现:
```java
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
public class RSAEncryption {
public static String encrypt(String plainText, KeyPair keyPair) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, KeyPair keyPair) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, "UTF-8");
}
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
KeyPair keyPair = keyPairGen.genKeyPair();
String plainText = "Hello, world!";
String encryptedText = encrypt(plainText, keyPair);
String decryptedText = decrypt(encryptedText, keyPair);
System.out.println("Plain text: " + plainText);
System.out.println("Encrypted text: " + encryptedText);
System.out.println("Decrypted text: " + decryptedText);
}
}
```
以上代码生成一对RSA密钥对,使用公钥加密明文,私钥解密密文。注意在实际应用中,需要将公钥和私钥存储在安全的地方,以保证加解密的安全性。