使用私钥解密文件。(RSA)

时间:2014-09-24 11:30:11

标签: java android encryption

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

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应用程序中:sun.security.rsa.RSAPrivateKeyImpl@4dd8f 为什么它如此不同。以及如何修复

2 个答案:

答案 0 :(得分: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;
    }

我解密了128bytes /次。因为数据解析时间太长。它在android中运行正常。 readPrivateKeyFromFile方法返回私钥的其他值(与android中的相同,尽管我使用相同的代码)。

答案 1 :(得分:-1)

我尝试了很多并且发现了。如果您在移动设备中加密,则只能在移动设备上解密,而在PC中也是如此。因为环境不同这是真的吗?