当前位置: 移动技术网 > IT编程>开发语言>Java > java 加密之RSA算法加密与解密的实例详解

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

前言:

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

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

rsa加密与解密

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

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

 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)进行生成。

@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)生成私钥。

 /**
 * 私钥解密
 * @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)));
 }

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网