exponent modulus 根据_RSA算法使用介绍

http://www.cnblogs.com/AloneSword/p/3326750.html

RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥,即公钥,而两个大素数组合成私钥。公钥是可发布的供任何人使用,私钥则为自己所有,供解密之用。

解密者拥有私钥,并且将由私钥计算生成的公钥发布给加密者。加密都使用公钥进行加密,并将密文发送到解密者,解密者用私钥解密将密文解码为明文。

以甲要把信息发给乙为例,首先确定角色:甲为加密者,乙为解密者。首先由乙随机确定一个KEY,称之为密匙,将这个KEY始终保存在机器B中而不发出来;然后,由这个 KEY计算出另一个KEY,称之为公匙。这个公钥的特性是几乎不可能通过它自身计算出生成它的私钥。接下来通过网络把这个公钥传给甲,甲收到公钥后,利用公钥对信息加密,并把密文通过网络发送到乙,最后乙利用已知的私钥,就对密文进行解码了。以上就是RSA算法的工作流程。

算法实现过程为:

1. 随意选择两个大的质数p和q,p不等于q,计算N=pq。

2. 根据欧拉函数,不大于N且与N互质的整数個数為(p-1)(q-1)。

3. 选择一个整数e与(p-1)(q-1)互质,并且e小于(p-1)(q-1)。

4. 用以下这个公式计算d:d× e ≡ 1 (mod (p-1)(q-1))。

5. 将p和q的记录销毁。

以上内容中,(N,e)是公钥,(N,d)是私钥。

下面讲解RSA算法的应用。

RSA的公钥和私钥是由KeyPairGenerator生成的,获取KeyPairGenerator的实例后还需要设置其密钥位数。设置密钥位数越高,加密过程越安全,一般使用1024位。如下代码:

[代码]java代码:

1

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(RSA);

2

// 密钥位数

3

keyPairGen.initialize(1024);

公钥和私钥可以通过KeyPairGenerator执行generateKeyPair()后生成密钥对KeyPair,通过KeyPair.getPublic()和KeyPair.getPrivate()来获取。如下代码:

[代码]java代码:

1

// 动态生成密钥对,这是当前最耗时的操作,一般要2s以上。

2

KeyPair keyPair = keyPairGen.generateKeyPair();

3

// 公钥

4

PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

5

// 私钥

6

PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

7

byte[] publicKeyData = publicKey.getEncoded();

8

byte[] privateKeyData = publicKey.getEncoded();

公钥和私钥都有它们自己独特的比特编码,可以通过getEncoded()方法获取,返回类型为byte[]。通过byte[]可以再度将公钥或私钥还原出来。具体代码如下:

[代码]java代码:

01

// 通过公钥byte[]将公钥还原,适用于RSA算法

02

public static PublicKey getPublicKey(byte[] keyBytes)throws

03

NoSuchAlgorithmException,InvalidKeySpecException {

04

X509EncodedKeySpec keySpec =new X509EncodedKeySpec(keyBytes);

05

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

06

PublicKey publicKey = keyFactory.generatePublic(keySpec);

07

return publicKey;

08

}

09

// 通过私钥byte[]将公钥还原,适用于RSA算法

10

public static PrivateKey getPrivateKey(byte[] keyBytes)throws

11

NoSuchAlgorithmException,InvalidKeySpecException {

12

PKCS8EncodedKeySpec keySpec =new PKCS8EncodedKeySpec(keyBytes);

13

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

14

PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

15

return privateKey;

16

}

在上文讲到的RSA算法实现过程中提到(N,e)是公钥,(N,d)是私钥。既然已经获取到了PublicKey和PrivateKey了,那如何取到N、e、d这三个值呢。要取到这三个值,首先要将PublicKey和PrivateKey强制转换成RSAPublicKey和RSAPrivateKey。共同的N值可以通过getModulus()获取。执行RSAPublicKey.getPublicExponent()可以获取到公钥中的e值,执行RSAPrivateKey.getPrivateExponent()可以获取私钥中的d值。这三者返回类型都是BigInteger。代码如下:

[代码]java代码:

01

// 打印公钥信息

02

public static void printPublicKeyInfo(PublicKey key){

03

RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;

04

Log.d(MainActivity.TAG,"RSAPublicKey:");

05

Log.d(MainActivity.TAG,"Modulus.length=" +

06

rsaPublicKey.getModulus().bitLength());

07

Log.d(MainActivity.TAG,"Modulus=" +

08

rsaPublicKey.getModulus().toString());

09

Log.d(MainActivity.TAG,"PublicExponent.length=" +

10

rsaPublicKey.getPublicExponent().bitLength());

11

Log.d(MainActivity.TAG,"PublicExponent=" +

12

rsaPublicKey.getPublicExponent().toString());

13

}

14

15

// 打印私钥信息

16

public static void printPublicKeyInfo(PrivateKey key){

17

RSAPrivateKey rsaPublicKey = (RSAPrivateKey) privateKey;

18

Log.d(MainActivity.TAG,"RSAPrivateKey:");

19

Log.d(MainActivity.TAG,"Modulus.length=" +

20

rsaPrivateKey.getModulus().bitLength());

21

Log.d(MainActivity.TAG,"Modulus=" +

22

rsaPrivateKey.getModulus().toString());

23

Log.d(MainActivity.TAG,"PublicExponent.length=" +

24

rsaPrivateKey.getPrivateExponent().bitLength());

25

Log.d(MainActivity.TAG,"PublicExponent=" +

26

rsaPrivateKey.getPrivateExponent().toString());

27

}

由于程序中动态生成KeyPair对明文加密后生成的密文是不可测的,所以在实际开发中通常在生成一个KeyPair后将公钥和私钥的N、e、d这三个特征值记录下来,在真实的开发中使用这三个特征值再去将PublicKey和PrivateKey还原出来。还原方法如下:

[代码]java代码:

01

// 使用N、e值还原公钥

02

public static PublicKey getPublicKey(String modulus, String

03

publicExponent)

04

throws NoSuchAlgorithmException, InvalidKeySpecException {

05

BigInteger bigIntModulus =new BigInteger(modulus);

06

BigInteger bigIntPrivateExponent =new BigInteger(publicExponent);

07

RSAPublicKeySpec keySpec =new RSAPublicKeySpec(bigIntModulus,

08

bigIntPrivateExponent);

09

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

10

PublicKey publicKey = keyFactory.generatePublic(keySpec);

11

return publicKey;

12

}

13

14

// 使用N、d值还原公钥

15

public static PrivateKey getPrivateKey(String modulus, String

16

privateExponent)

17

throws NoSuchAlgorithmException, InvalidKeySpecException {

18

BigInteger bigIntModulus =new BigInteger(modulus);

19

BigInteger bigIntPrivateExponent =new BigInteger(privateExponent);

20

RSAPrivateKeySpec keySpec =new RSAPrivateKeySpec(bigIntModulus,

21

bigIntPrivateExponent);

22

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

23

PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

24

return privateKey;

25

}

公钥和私钥都具备后,就可以使用加解密的工具类javax.crypto.Cipher对明文和密文进行处理了。与所有的引擎类一样,可以通过调用Cipher类中的getInstance(String transformation)静态工厂方法得到Cipher对象。该方法中的参数描述了由指定输入产生输出所进行的操作或操作集合,可以是下列两种形式之一:“algorithm/mode/padding”或“algorithm”。例如下面的例子就是有效的transformation形式:"DES/CBC/PKCS5Padding"或"DES"。如果没有指定模式或填充方式,就使用特定提供者指定的默认模式或默认填充方式。

Cipher的加密和解密方式所调用的方法和过程都一样,只是传参不同的区别。如下代码:

[代码]java代码:

1

// 编码前设定编码方式及密钥

2

cipher.init(mode, key);

3

// 传入编码数据并返回编码结果

4

byte[] dataResult = cipher.doFinal(input);

Cipher.init(mode, key)方法中MODE指加密或解密模式,值为Cipher.ENCRYPT_MODE或Cipher.DECRYPT_MODE,参数key在加密时传入PublicKey,在解密时以PrivateKey传入。Cipher.doFinal(byte[] data)则是将待编码数据传入后并返回编码结果。为了将编码结果转为可读字符串,通常最后还使用BASE 64算法对最终的byte[]数据编码后显示给开发者。

Demo运行效果如下图所示:

图17-4  使用动态生成的公钥和私钥进行RSA加密

图17-5  使用预设的N、e、d值进行RSA加密

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA算法是一种非常重要的公钥加密算法,它基于大整数的质因数分解难题。下面是用C语言实现RSA算法的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAX_LEN 1000 #define BLOCK_SIZE 2 typedef struct { int n; // modulus int e; // public exponent } PublicKey; typedef struct { int n; // modulus int d; // private exponent } PrivateKey; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int mod_pow(int base, int exp, int mod) { int result = 1; base %= mod; while (exp > 0) { if (exp & 1) result = (result * base) % mod; exp >>= 1; base = (base * base) % mod; } return result; } // 生成公钥和私钥 void generate_keys(PublicKey *pub, PrivateKey *priv) { int p, q, phi, e, d; // 生成两个大素数p和q do { p = rand() % (int) sqrt(MAX_LEN) + 1; } while (!is_prime(p)); do { q = rand() % (int) sqrt(MAX_LEN) + 1; } while (!is_prime(q)); // 计算n和phi(n) pub->n = p * q; phi = (p - 1) * (q - 1); // 选择公钥e do { e = rand() % phi + 1; } while (gcd(e, phi) != 1); // 计算私钥d d = mod_inverse(e, phi); pub->e = e; priv->n = pub->n; priv->d = d; } // 加密 char *encrypt(char *msg, PublicKey pub) { int len, i, j, num, encrypted; char *encrypted_msg; len = strlen(msg); encrypted_msg = (char *) malloc(len * BLOCK_SIZE + 1); memset(encrypted_msg, 0, len * BLOCK_SIZE + 1); for (i = 0, j = 0; i < len; i += BLOCK_SIZE, j += BLOCK_SIZE) { num = msg[i] * 256 + msg[i + 1]; encrypted = mod_pow(num, pub.e, pub.n); sprintf(&encrypted_msg[j], "%04d", encrypted); } return encrypted_msg; } // 解密 char *decrypt(char *encrypted_msg, PrivateKey priv) { int len, i, j, num, decrypted; char *msg; len = strlen(encrypted_msg); msg = (char *) malloc(len / BLOCK_SIZE + 1); memset(msg, 0, len / BLOCK_SIZE + 1); for (i = 0, j = 0; i < len; i += BLOCK_SIZE, j++) { num = (encrypted_msg[i] - '0') * 1000 + (encrypted_msg[i + 1] - '0') * 100 + (encrypted_msg[i + 2] - '0') * 10 + (encrypted_msg[i + 3] - '0'); decrypted = mod_pow(num, priv.d, priv.n); msg[j] = decrypted / 256; msg[j + 1] = decrypted % 256; } return msg; } int main() { PublicKey pub; PrivateKey priv; char *msg = "Hello World!"; char *encrypted_msg; char *decrypted_msg; srand(time(NULL)); generate_keys(&pub, &priv); printf("Public key: n=%d, e=%d\n", pub.n, pub.e); printf("Private key: n=%d, d=%d\n", priv.n, priv.d); encrypted_msg = encrypt(msg, pub); printf("Encrypted message: %s\n", encrypted_msg); decrypted_msg = decrypt(encrypted_msg, priv); printf("Decrypted message: %s\n", decrypted_msg); free(encrypted_msg); free(decrypted_msg); return 0; } ``` 需要注意的是,这里使用的是简化版的RSA算法,仅用于示例。在实际使用中,需要使用更加复杂的算法来确保安全性。此外,还需要注意对大整数的处理,可使用第三方库如GMP来进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值