RSA加密和解密的具体实现
时间: 2023-06-03 09:04:27 浏览: 79
RSA密和解密的具体实现可以参考以下步骤:
1. 选择两个大素数p和q,并计算n=p*q。
2. 计算欧拉函数φ(n)=(p-1)*(q-1)。
3. 随机选择一个小于φ(n)的整数e,使得e与φ(n)互质,并计算出d满足ed ≡ 1 mod φ(n)。
4. 公钥为(n, e),私钥为(n, d)。
5. 加密明文M可以使用公钥进行加密,计算出密文C = M^e mod n。
6. 解密密文C可以使用私钥进行解密,计算出明文M = C^d mod n。
需要注意的是,RSA算法的安全性依赖于大素数p和q的难以分解性,因此选择大素数是非常关键的。此外,为了保证加密解密的速度,通常会选择较小的公钥指数e。
相关问题
c语言实现rsa加密和解密算法
要实现RSA加密和解密算法的C语言代码,你需要以下几个步骤:
1. 生成RSA密钥对:
- 随机选择两个大素数 p 和 q。
- 计算 n = p * q。
- 计算 φ(n) = (p-1) * (q-1),其中 φ(n) 表示欧拉函数。
- 选择一个整数 e,满足 1 < e < φ(n),且 e 和 φ(n) 互质。
- 计算 d,使得 (d * e) ≡ 1 (mod φ(n)),即 d * e 除以 φ(n) 的余数为 1。
- 公钥为 (e, n),私钥为 (d, n)。
2. 加密:
- 将明文转换为整数 M。
- 使用公钥 (e, n) 进行加密,计算密文 C = M^e % n。
3. 解密:
- 使用私钥 (d, n) 进行解密,计算明文 M = C^d % n。
下面是一个简单的示例代码:
```c
#include <stdio.h>
// 求最大公约数
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// 求模逆元
int mod_inverse(int a, int m) {
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1)
return 0;
while (a > 1) {
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0)
x1 += m0;
return x1;
}
// 加密
int encrypt(int message, int e, int n) {
int ciphertext = 1;
for (int i = 0; i < e; i++) {
ciphertext = (ciphertext * message) % n;
}
return ciphertext;
}
// 解密
int decrypt(int ciphertext, int d, int n) {
int message = 1;
for (int i = 0; i < d; i++) {
message = (message * ciphertext) % n;
}
return message;
}
int main() {
int p = 61; // 第一个大素数
int q = 53; // 第二个大素数
int n = p * q;
int phi_n = (p - 1) * (q - 1);
int e = 17; // 选择一个与 phi(n) 互质的数,常用公钥指数
int d = mod_inverse(e, phi_n); // 计算私钥指数
// 明文和密文
int message = 123;
int ciphertext = encrypt(message, e, n);
int decrypted_message = decrypt(ciphertext, d, n);
printf("明文: %d\n", message);
printf("密文: %d\n", ciphertext);
printf("解密后的明文: %d\n", decrypted_message);
return 0;
}
```
用Java实现RSA加密和解密
RSA加密和解密的实现步骤如下:
1. 生成RSA密钥对
```java
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
```
2. 获取公钥和私钥
```java
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
```
3. 使用公钥加密
```java
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(plainText.getBytes());
```
4. 使用私钥解密
```java
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedText = cipher.doFinal(cipherText);
```
完整代码如下:
```java
import javax.crypto.Cipher;
import java.security.*;
public class RSAUtil {
/**
* 生成RSA密钥对
*
* @return KeyPair
* @throws NoSuchAlgorithmException
*/
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
return keyPairGen.generateKeyPair();
}
/**
* 使用公钥加密
*
* @param plainText 明文
* @param publicKey 公钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte[] encrypt(String plainText, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plainText.getBytes());
}
/**
* 使用私钥解密
*
* @param cipherText 密文
* @param privateKey 私钥
* @return String 解密后的数据
* @throws Exception
*/
public static String decrypt(byte[] cipherText, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedText = cipher.doFinal(cipherText);
return new String(decryptedText);
}
public static void main(String[] args) throws Exception {
String plainText = "Hello World!";
// 生成密钥对
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// 加密
byte[] cipherText = encrypt(plainText, publicKey);
// 解密
String decryptedText = decrypt(cipherText, privateKey);
System.out.println("明文: " + plainText);
System.out.println("密文: " + new String(cipherText));
System.out.println("解密后的明文: " + decryptedText);
}
}
```