星空网 > 软件开发 > ASP.net

java RSA公钥加密,私钥解密算法例子

"非对称加密算法"。   (1)乙方生成两把密钥(公钥和私钥)。公钥是公开的,任何人都可以获得,私钥则是保密的。   (2)甲方获取乙方的公钥,然后用它对信息加密。   (3)乙方得到加密后的信息,用私钥解密。 如果公钥加密的信息只有私钥解得开,那么只要私钥不泄漏,通信就是安全的。公钥用于加密, 私钥用于解密. RSA 是一种非对称加密算法,一般很难破解,因此一些要求比较高的系统通常会采用rsa加密算法,一般来说用RSA加密有如下几个步骤. 1. 生成公钥与私钥 2. 用公钥对需要加密的字符串等进行加密 3. 在需要解密的地方,用私钥进行解密 下面对上面的几个部分贴出代码. 1. 生成公钥与私钥 Java代码 复制代码 收藏代码1.package com.rsa; 2.import java.io.FileOutputStream;  3.import java.security.KeyPair;  4.import java.security.KeyPairGenerator;  5.import java.security.SecureRandom;  6.import java.util.Date; 7. 8.public class GenKeys { 9.  public static void main(String[] args) throws Exception { 10.    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");  11.    SecureRandom secureRandom = new SecureRandom(new Date().toString().getBytes()); 12.    keyPairGenerator.initialize(1024, secureRandom); 13.    KeyPair keyPair = keyPairGenerator.genKeyPair(); 14.    String publicKeyFilename = "D:/publicKeyFile"; 15.    byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); 16.    FileOutputStream fos = new FileOutputStream(publicKeyFilename);  17.    fos.write(publicKeyBytes);  18.    fos.close(); 19.    String privateKeyFilename = "D:/privateKeyFile";  20.    byte[] privateKeyBytes = keyPair.getPrivate().getEncoded(); 21.    fos = new FileOutputStream(privateKeyFilename);  22.    fos.write(privateKeyBytes);  23.    fos.close(); 24.  } 25.} 2. 读取公钥方法 Java代码 复制代码 收藏代码1.package com.rsa; 2. 3.import java.io.DataInputStream; 4.import java.io.File; 5.import java.io.FileInputStream; 6.import java.security.PublicKey; 7.import java.security.spec.X509EncodedKeySpec; 8.import java.security.KeyFactory; 9. 10.public class PublicKeyReader {   11.  public static PublicKey get(String filename) throws Exception { 12.    File f = new File(filename); 13.    FileInputStream fis = new FileInputStream(f);  14.    DataInputStream dis = new DataInputStream(fis); 15.    byte[] keyBytes = new byte[(int)f.length()];  16.    dis.readFully(keyBytes);  17.    dis.close(); 18.    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); 19.    KeyFactory kf = KeyFactory.getInstance("RSA");  20.    return kf.generatePublic(spec); 21.  } 22. 23. 24.} 3.读取私钥方法 Java代码 复制代码 收藏代码1.package com.rsa; 2. 3.import java.io.DataInputStream; 4.import java.io.File; 5.import java.io.FileInputStream; 6.import java.io.IOException; 7.import java.security.KeyFactory; 8.import java.security.PrivateKey; 9.import java.security.spec.InvalidKeySpecException; 10.import java.security.spec.PKCS8EncodedKeySpec; 11.public class PrivateKeyReader {   12.  public static PrivateKey get(String filename)throws Exception { 13.    File f = new File(filename); 14.    FileInputStream fis = new FileInputStream(f); 15.    DataInputStream dis = new DataInputStream(fis); 16.    byte[] keyBytes = new byte[(int)f.length()]; 17.    dis.readFully(keyBytes); 18.    dis.close(); 19.    PKCS8EncodedKeySpec spec =new PKCS8EncodedKeySpec(keyBytes); 20.    KeyFactory kf = KeyFactory.getInstance("RSA"); 21.    return kf.generatePrivate(spec); 22.   } 23. 24.   25.  public static void main(String[] args) throws Exception, InvalidKeySpecException, IOException { 26.    PrivateKeyReader.get("d:/privateKeyFile"); 27.  } 28.} 4. 测试公钥加密,私钥解密 Java代码 复制代码 收藏代码1.package com.rsa; 2. 3.import java.security.interfaces.RSAPrivateKey; 4.import java.security.interfaces.RSAPublicKey; 5.import javax.crypto.Cipher; 6. 7.public class TestEncryptAndDecrypt {   8.  public static void main(String[] args) throws Exception { 9.    String input = "thisIsMyPassword$7788"; 10.    Cipher cipher = Cipher.getInstance("RSA");     11.    RSAPublicKey pubKey = (RSAPublicKey) PublicKeyReader.get("d:/publicKeyFile"); 12.    RSAPrivateKey privKey = (RSAPrivateKey) PrivateKeyReader.get("d:/privateKeyFile"); 13.    cipher.init(Cipher.ENCRYPT_MODE, pubKey); 14.    byte[] cipherText = cipher.doFinal(input.getBytes()); 15.    //加密后的东西 16.    System.out.println("cipher: " + new String(cipherText));     17.    //开始解密 18.    cipher.init(Cipher.DECRYPT_MODE, privKey);  19.    byte[] plainText = cipher.doFinal(cipherText); 20.    System.out.println("plain : " + new String(plainText)); 21.  } 22. 23.} 查看结果: 程序代码 程序代码 cipher: J��� �nE��J�b9��CO��I�?�g[B{�w��u0����}�r6 �Q��Xa��ٝ�͊��N}n�]�@��_9!D��_�|k�ͪ&g�^��ɿ�XTa$�7��*�{7�R���v�S plain : thisIsMyPassword$7788 说明加密解密成功。 备注,这里记录的只是测试方法,当然在实际使用过程中,可能还需要 对加密后的 byte[] 采用 base64 编码,转换成字符串存储起来,在解密的时候,先通过 base64 还原成 byte, 然后在解密,这样会更好。详细的方法, 

获取【下载地址】   java后台框架 springmvc整合mybatis框架源码 bootstrap html5 mysql oracle




原标题:java RSA公钥加密,私钥解密算法例子

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

正在崛起的墨西哥新零售“La Comer ”:https://www.kjdsnews.com/a/768440.html
Reverb官网网址,Reverb是什么平台?:https://www.kjdsnews.com/a/768441.html
Houzz官网网址是什么?Houzz平台好不好?:https://www.kjdsnews.com/a/768442.html
eBay选品:2022年厨电高潜品类整理:https://www.kjdsnews.com/a/768443.html
马来西亚电商平台排名,马来西亚电商有哪些:https://www.kjdsnews.com/a/768444.html
快自查!71款热门图案起诉版权侵权,大批账号遭冻结罚款:https://www.kjdsnews.com/a/768445.html
桂林酒店销售多少钱 桂林旅游宾馆价格:https://www.vstour.cn/a/410227.html
十里银滩旅游攻略玩什么住哪里怎么去?:https://www.vstour.cn/a/410228.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流