引起:java.security.KeyException:错误的数据

时间:2013-12-10 09:45:06

标签: java applet cryptography cryptoapi

以下代码可以很好地加密数据,但在尝试解密数据时遇到错误。

public static void cryptoFunction() throws Exception
        {
            KeyStore store = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
            store.load(null);
            String alias = "alias";
            Certificate cert = store.getCertificate(alias);
            PublicKey pubKey = (PublicKey) cert.getPublicKey();
            PrivateKey privKey = (PrivateKey) store.getKey(alias, "123456".toCharArray());
            Cipher ecipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            Cipher dcipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

            ecipher.init(Cipher.ENCRYPT_MODE, pubKey);

            File userDir = new File("C:\\TestCryptoFiles");
            userDir.mkdir();

            File tmpdestFile = new File(userDir, "outFile.txt");
            File sourceFile = new File(userDir, "InFile.txt");

            int cipherMode = Cipher.ENCRYPT_MODE; //Cipher.DECRYPT_MODE

            byte[] buf = cipherMode == Cipher.ENCRYPT_MODE ? new byte[100]: new byte[128];
            int bufl;

            FileOutputStream outputWriter = new FileOutputStream(tmpdestFile);
            FileInputStream inputReader = new FileInputStream(sourceFile);         
            if(cipherMode == Cipher.ENCRYPT_MODE){
                while ((bufl = inputReader.read(buf)) != -1) {
                    byte[] encText = null;
                    encText = ecipher.doFinal(copyBytes(buf, bufl));
                    System.out.println(new String(encText));
                //  encText = dcipher.doFinal(encText);  // works well...
                    outputWriter.write(encText);
                }
            }else{
                while ((bufl = inputReader.read(buf)) != -1) {
                    byte[] encText = null;
                    encText = dcipher.doFinal(copyBytes(buf, bufl)); // throws exception Bad data...
                    System.out.println(new String(encText));
                    outputWriter.write(encText);
                }
            }
        }
     public static byte[] copyBytes(byte[] arr, int length) {
            byte[] newArr = null;
            if (arr.length == length)
                newArr = arr;
            else {
                newArr = new byte[length];
                for (int i = 0; i < length; i++) {
                    newArr[i] = (byte) arr[i];
                }
            }
            return newArr;
        }

我的堆栈跟踪:

java.security.ProviderException: java.security.KeyException: Bad Data.    
        at sun.security.mscapi.RSACipher.doFinal(RSACipher.java:277)
        at sun.security.mscapi.RSACipher.engineDoFinal(RSACipher.java:301)
        at javax.crypto.Cipher.doFinal(DashoA13*..)
        at FileUploader.decrypt(FileUploader.java:223)
        at FileUploader$4.run(FileUploader.java:414)
        at java.security.AccessController.doPrivileged(Native Method)
        at FileUploader.encryptDecryptFile(FileUploader.java:371)
        at FileUploader.decryptFile(FileUploader.java:362)
        at FileUploader.openFileChooser(FileUploader.java:157)
        at FileUploader.<init>(FileUploader.java:115)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at sun.applet.AppletPanel.createApplet(Unknown Source)
        at sun.applet.AppletPanel.runLoader(Unknown Source)
        at sun.applet.AppletPanel.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.security.KeyException: Bad Data.

        at sun.security.mscapi.RSACipher.encryptDecrypt(Native Method)
        at sun.security.mscapi.RSACipher.doFinal(RSACipher.java:269)
        ... 19 more

查看注释代码以便更好地理解。请帮我,我错了。

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方案,实际上我有一个RSA 2048位密钥并使用256个字节因此,我只是修改我的代码安静,如:

byte[] buf = cipherMode == Cipher.ENCRYPT_MODE ? new byte[100]: new byte[128];

替换为:

byte[] buf = cipherMode == Cipher.ENCRYPT_MODE ? new byte[100]: new byte[256];

128位字节由RSA 1024位密钥生成,对该密钥很有用。