rsa 2048加密算法安全吗 (rsa非对称加密)

rsa2048加密算法安全吗,rsa的加密和解密原理

加密

最近公司将整个项目做了安全漏洞扫描,1万多个漏洞,简直崩溃,其中也包括非对称加密的问题。项目要求非对称加密使用算法RSA,大小2048,不推荐使用1024。这两天一直在纠结,如果使用2048,那加解密性能不是要下降一节。非对称加密随着密钥变长,安全性上升的同时性能也会有所下降。所以赶紧动起手来,测试以下性能jiu'j究竟怎么样?

1024位加解密耗时:

rsa2048加密算法安全吗,rsa的加密和解密原理

1024位加解密耗时

2048位加解密耗时:

rsa2048加密算法安全吗,rsa的加密和解密原理

2048位加解密耗时

总结:1024位加解密耗时差不都,2048位加解密耗时相差有点大,主要是在解密耗时。不过从中可以看出2048位解密所耗时间还是可以接受。当然安全,性能也要在具体的业务场景下进行取舍。

末尾附上测试源码:

@Test
public void testRSA(){
 try{
 // 生成密钥对
 KeyPair keyPair = RSAUtil.getKeyPair();
 String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
 String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
 System.out.println("私钥:" + privateKey);
 System.out.println("公钥:" + publicKey);
 // RSA加密
 String data = "您好啊 RSA!";
 System.out.println("加密前内容:" + data);
 //开始计时
 long startTime = System.currentTimeMillis() ;
 List<String> list = new ArrayList<>() ;
 for(int i=0; i<10000;i++){
 String encryptData = RSAUtil.encrypt(data+":"+i, RSAUtil.getPublicKey(publicKey));
 list.add(encryptData) ;
 System.out.println("加密后内容:" + encryptData);
 }
 //10000条数据加密消耗时间
 long encryptTime = System.currentTimeMillis() - startTime ;
 startTime = System.currentTimeMillis() ;
 // RSA解密
 for(String content:list){
 String decryptData = RSAUtil.decrypt(content, RSAUtil.getPrivateKey(privateKey));
 System.out.println("解密后内容:" + decryptData);
 }
 //10000条数据解密消耗时间
 long decryptTime = System.currentTimeMillis() - startTime ;
 System.out.println("10000条数据加密消耗时间:"+
 (encryptTime)+" 毫秒!");
 System.out.println("10000条数据解密消耗时间:"+
 (decryptTime)+" 毫秒!");
 }catch (Exception e){
 e.printStackTrace();
 }
}