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

Java 使用私钥解密文件。(RSA),java,android,encryption,Java,Android,Encryption,我需要有人帮忙。 在我的android手机中,我创建公钥(加密文件)和私钥解密。我把它们保存在2个文件中。当我用安卓解密的时候就可以了。但当我复制私钥文件并用它在我的PC(java)中解密时,我得到了一个例外: at sun.security.rsa.RSAPadding.unpadV15(Unknown Source) at sun.security.rsa.RSAPadding.unpad(Unknown Source) at com.sun.crypto.provider.

我需要有人帮忙。 在我的android手机中,我创建公钥(加密文件)和私钥解密。我把它们保存在2个文件中。当我用安卓解密的时候就可以了。但当我复制私钥文件并用它在我的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和pc上打印私钥文件,它的值就不一样了。 在我的android应用程序中:
OpenSSLRSAPrivateKey{模数=A9A141D6EF0050E27F00EC381FCFEB47781DCBF14F9B3EB378BD361B92B6DA7FEB8F2BE7466314794D7543EB99C818D260D48B898C9995DB3AF76B23013FF935F77B8A89EDCBD9D16A583B60591E5F7CB827FA4CFAE5FD759F3D8E1B5228285E2CF29E8903422333232B7C84FC8483480401367B757EB827E,私人指数=FFC2555F77B757B757B757B757B757EB82827EB55E862384D73530D28916B7A54D944F610878E5935B39821AB3C720598BE28D747DE099FF8FAC658F235B983815EFC61CBC574BE39D97DC7AC57E6CF82161F4301FE777C3C33C58D7C75F581DE5CC0DB83B079DE7D79864A6189667171,
在我的java应用程序中:
sun.security.rsa。RSAPrivateKeyImpl@4dd8f
为什么会有如此大的差异。以及如何修复

以下是我的代码:

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;
    }
public void RSADecrypt(字符串填充名、字符串输出名){
试一试{
/*从文件中获取加密消息*/
FileInputStream cipherfile=新的FileInputStream(填充名);
byte[]ciphertext=新字节[cipherfile.available()];
cipherfile.read(密文);
cipherfile.close();
PrivateKey PrivateKey=readPrivateKeyFromFile(“D:\\Private.key”);
/*创建用于解密的密码*/
Cipher decrypt_Cipher=Cipher.getInstance(“RSA/ECB/PKCS1Padding”);
decrypt_cipher.init(cipher.decrypt_模式,privatekey);
FileOutputStream plainfile=新的FileOutputStream(outFileName);
int n=密文长度/128;
System.out.println(“len:+n”);
字节[]数据1=新字节[128];
对于(int i=0;i

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

我试了很多次才发现。如果你用moblie加密,你只能在手机上解密,在PC上也一样。因为不同的环境,这是真的吗?

不要在公共场所发布私钥模数。@hexafraction更准确地说,不要在公共场所发布私钥指数。输出不同的原因是
OpenSSLRSAPrivateKey
覆盖
Object.toString()
RSAPrivateKeyImpl
不覆盖。如果pc和android上的密钥不相同,那么您知道原因。找出原因。请告诉我们异常是什么(您从堆栈跟踪中遗漏了这一点)。请同时向我们展示您用于解密的代码。@hexafraction:私钥模数与公钥模数相同。发布它不会有任何伤害。privateExponent应该是机密的。