java 加密之RSA算法加密与解密的实例详解

转载出处:http://www.jb51.net/article/118479.htm

前言:

  RSA是第一个比较完善的公开密钥算法,它既能用于加密,也能用于数字签名。RSA以它的三个发明者Ron Rivest, Adi Shamir, Leonard Adleman的名字首字母命名,这个算法经受住了多年深入的密码分析,虽然密码分析者既不能证明也不能否定RSA的安全性,但这恰恰说明该算法有一定的可信性,目前它已经成为最流行的公开密钥算法。

       RSA的安全基于大数分解的难度。其公钥和私钥是一对大素数(100到200位十进制数或更大)的函数。从一个公钥和密文恢复出明文的难度,等价于分解两个大素数之积(这是公认的数学难题)。

RSA加密与解密

RSA算法的密钥由公钥和私钥组成,公钥用于加密,私钥用于解密。顾名思义,公钥就是可以进行公开的密钥,一般可以公开给你的合作伙伴;私钥就是私有的,也就是只有你知道的,你的合作伙伴通过你提供的公钥进行加密的内容只有你能进行解密,这样也就只有你知道他发的是什么内容。用于加密的公钥和私钥是配对的。这样的一对密钥在Java中由

java.security.KeyPairGenerator来产生。以下是一个生成密钥对的示例,该示例中还将生成的密钥对分别保存到了文件中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private static final String ALGORITHM = "RSA" ;
private static final String PRIVATE_KEY_PATH = "D:\\rsa_private.isa" ;
private static final String PUBLIC_KEY_PATH = "D:\\rsa_public.isa" ;
 
/**
* 生成公钥和私钥并存储到文件中
* @throws Exception
*/
@Test
public void geneKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize( 1024 );
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate(); //私钥
PublicKey publicKey = keyPair.getPublic(); //公钥
byte [] privateKeyBytes = privateKey.getEncoded(); //私钥对应的字节数组
byte [] publicKeyBytes = publicKey.getEncoded(); //公钥对应的字节数组
Files.write(Paths.get(PRIVATE_KEY_PATH), privateKeyBytes);
Files.write(Paths.get(PUBLIC_KEY_PATH), publicKeyBytes);
}

加密

加密的过程跟使用AES算法进行加密的过程类似,唯一需要注意的是使用存储起来的公钥时需要使用X509EncodedKeySpec进行封装,然后通过KeyFactory.generatePublic(KeySpec)进行生成。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Test
  public void testEncrypt() throws Exception {
  this .encrypt( "Hello RSA" );
  }
  
  /**
  * 公钥加密
  * @param value
  * @return
  * @throws Exception
  */
  private byte [] encrypt(String value) throws Exception {
  Cipher cipher = Cipher.getInstance(ALGORITHM);
  //读取公钥对应的字节数组
  byte [] publicKeyCode = Files.readAllBytes(Paths.get(PUBLIC_KEY_PATH));
  //构造公钥,存储起来的公钥需要使用X509EncodedKeySpec进行读取
  X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyCode);
  KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
  //根据已有的KeySpec生成对应的公钥
  PublicKey publicKey = keyFactory.generatePublic(keySpec);
  cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  byte [] result = cipher.doFinal(value.getBytes());
  System.out.println(Base64.getEncoder().encodeToString(result));
  return result;
  }

解密

解密是使用的密钥对中的私钥,其使用方法跟AES算法进行解密类似。 存储起来的私钥需要通过PKCS8EncodedKeySpec加载,然后通过KeyFactory.generatePrivate(KeySpec)生成私钥。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 私钥解密
* @throws Exception
*/
@Test
public void testDecrypt() throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
byte [] privateKeyCode = Files.readAllBytes(Paths.get(PRIVATE_KEY_PATH));
//私钥需要通过PKCS8EncodedKeySpec来读取
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyCode);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
//生成私钥
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
String content = "Java Program" ;
byte [] input = this .encrypt( "Java Program" );
byte [] result = cipher.doFinal(input);
Assert.assertTrue(content.equals( new String(result)));
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值