RSA算法Java实现
public static void decrypt() throws Exception { // 读取密文 BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream("encrypt.dat"))); String ctext = in.readLine(); BigInteger c = new BigInteger(ctext); // 读取私钥 FileInputStream f = new FileInputStream("privatekey.dat"); ObjectInputStream b = new ObjectInputStream(f); RSAPrivateKey prk = (RSAPrivateKey) b.readObject(); BigInteger d = prk.getPrivateExponent(); // 获取私钥参数及解密 BigInteger n = prk.getModulus(); System.out.println("d= " + d); System.out.println("n= " + n); BigInteger m = c.modPow(d, n); // 显示解密结果 System.out.println("密文m= " + m); PrintWriter out =new PrintWriter("E:\\miwen.txt","UTF-8"); FileInputStream f = new FileInputStream("privatekey.dat"); ObjectInputStream b = new ObjectInputStream(f); RSAPrivateKey prk = (RSAPrivateKey) b.readObject(); BigInteger d = prk.getPrivateExponent(); // 获取私钥参数及解密 BigInteger n = prk.getModulus(); System.out.println("d= " + d); System.out.println("n= " + n); BigInteger m = c.modPow(d, n); // 显示解密结果 System.out.println("密文m= " + m); PrintWriter out =new PrintWriter("E:\\miwen.txt","UTF-8");
用户评论