2014-09-24 263 views
-2

我需要某人的帮助。 在我的android手机中,我创建了公钥(用于加密文件)和私钥来解密。我将它们保存在2个文件中。当我通过android解密时没关系。但是,当我复制私钥文件,并用它在我的电脑(JAVA)解密,我得到异常:使用私钥解密文件(RSA)

at sun.security.rsa.RSAPadding.unpadV15(Unknown Source) 
    at sun.security.rsa.RSAPadding.unpad(Unknown Source) 
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356) 
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382) 
    at javax.crypto.Cipher.doFinal(Cipher.java:2087) 
    at com.molisy.decryptfile.Main.RSADecrypt(Main.java:193) 
    at com.molisy.decryptfile.Main$2.actionPerformed(Main.java:309) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$200(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

我不知道如何解决。但是,私钥文件的价值是不一样的,如果我打印它在我的android和我的电脑。 在我的Android应用程序中:OpenSSLRSAPrivateKey{modulus=a9a141d6ef0050e27f00ec381f2fcfeb47781dcfb14f9b3eb378bd361b92b6da7feb8f2be7466314794d7543eb99c818d260d48b898c9995db3a3af76b23013ff935f77b8a89edcbd9d16a583b60591e55f7cb8271fa4cfae53fd759f3d8e1b522485e2cf29e89034223329322b4357c84fc848348b004136d6d360f8c9a70cb,privateExponent=7eaff2ee455da50b23f35a78a7c21bb50a9189223eb8c7a7527ed04182e2563265eb55e862384d73530d28916b7a54d944f610878e5935b39821ab3c720598be28d747de099ff8fac6558f235b983815efc61cbc574be39d97dc7ac57e6cf82161f4301dfe777c3c33c58d7c75f581de5cc0db83b079de7d79864a6189667171, 和我的java应用程序:[email protected] 为什么它是如此的不同。以及如何修复

+3

不要在公共场所发布您的私钥模数。 – hexafraction 2014-09-24 11:32:14

+1

@hexafraction更准确地说,不要将你的私人指数放在公共场所。输出不同的原因是因为'OpenSSLRSAPrivateKey'覆盖了Object.toString()和'RSAPrivateKeyImpl'。 – 2014-09-24 11:35:41

+0

如果pc和android上的key不一样,那么你知道理由。找出为什么不一样。 – 2014-09-24 11:35:58

回答

0

这里是我的代码:

public void RSADecrypt(String inFileName, String outFileName) { 
     try { 
      /* Get the encrypted message from file. */ 
      FileInputStream cipherfile = new FileInputStream(inFileName); 

      byte[] ciphertext = new byte[cipherfile.available()]; 
      cipherfile.read(ciphertext); 
      cipherfile.close();   
      PrivateKey privatekey =readPrivateKeyFromFile("D:\\Private.key"); 

      /* Create cipher for decryption. */ 
      Cipher decrypt_cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
      decrypt_cipher.init(Cipher.DECRYPT_MODE, privatekey); 
      FileOutputStream plainfile = new FileOutputStream(outFileName); 
      int n = ciphertext.length/128; 
      System.out.println("len: " + n); 
      byte[] data1 = new byte[128]; 
      for (int i = 0; i < n; i++) { 
       for (int j = 0; j < 128; j++) { 
        data1[j] = ciphertext[128 * i + j]; 
       } 
       byte[] descryptedData = decrypt_cipher.doFinal(data1); 
       plainfile.write(descryptedData); 

      } 

      plainfile.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
public PrivateKey readPrivateKeyFromFile(String fileName) 
      throws IOException { 
     FileInputStream fis = null; 
     ObjectInputStream ois = null; 
     try { 
      fis = new FileInputStream(new File(fileName)); 
      ois = new ObjectInputStream(fis); 

      BigInteger modulus = (BigInteger) ois.readObject(); 
      BigInteger exponent = (BigInteger) ois.readObject(); 

      // Get Private Key 
      RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
        modulus, exponent); 
      KeyFactory fact = KeyFactory.getInstance("RSA"); 
      PrivateKey privateKey = fact.generatePrivate(rsaPrivateKeySpec); 
      System.out.println("get key ok: " + privateKey.toString()); 
      return privateKey; 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (ois != null) { 
       ois.close(); 
       if (fis != null) { 
        fis.close(); 
       } 
      } 
     } 
     return null; 
    } 

我解密128字节/时间。因为数据太长而无法解密。它在Android中正常工作。 readPrivateKeyFromFile方法返回私钥的其他值(与android中不同,尽管我使用相同的代码)。

-1

我尝试了很多,找出答案。如果你在moblie中加密,你只能在手机上解密,而在PC上也是如此。因为不同的环境是真的吗?